silverstripe , Silverstripe 3 - GridFieldExtensions MultiClass Adding


Silverstripe 3 - GridFieldExtensions MultiClass Adding

Question:

Tag: silverstripe

I'm trying to use https://github.com/silverstripe-australia/silverstripe-gridfieldextensions/ to create a gridfield where I can add different types of dataobjects.

Sadly I can't figure out how to write the correct code for that on my class where I want the gridfield.

Could someone please point me in the right direction?

UPDATE: Based on your answers, I now have the following structure

class ModularPage extends Page {

   private static $has_many = array(
    'Sections' => 'MP_Section',
    'Galleries' => 'MP_Gallery',
    'Paragraphs' => 'MP_Paragraph'
    );

    public function getCMSFields() {
        ...

        $fields->addFieldToTab('Root.Main', $mutli_grid = GridField::create('Sections', 'Sektionen', $this->Sections(), MultiClassGrid::create(15)));

        ...
    }

}

class MP_Section extends DataObject {

    private static $has_one = array(
        'Section' => 'MP_Section',
        'ModularPage' => 'ModularPage'
    );

}

class MP_Gallery extends MP_Section {

    private static $has_one = array(
        'Section' => 'MP_Section',
        'ModularPage' => 'ModularPage'
    );

}

So far, so good? Is this right until now?

Cause If I want to add for example a gallery, I receive the following error

[User Error] Couldn't run query: SELECT DISTINCT "MP_Section"."ID", "MP_Section"."SortID" FROM "MP_Section" LEFT JOIN "MP_Gallery" ON "MP_Gallery"."ID" = "MP_Section"."ID" LEFT JOIN "MP_Paragraph" ON "MP_Paragraph"."ID" = "MP_Section"."ID" WHERE ("ModularPageID" = '13') ORDER BY "MP_Section"."SortID" ASC LIMIT 9223372036854775807 Column 'ModularPageID' in where clause is ambiguous


Answer:

Here is how I usually setup my GridField:

$c = GridFieldConfig_RelationEditor::create();
$c->removeComponentsByType('GridFieldAddNewButton')
  ->addComponent(new GridFieldAddNewMultiClass())
  ;

$c->getComponentByType('GridFieldAddNewMultiClass')
  ->setClasses(array(
    'SectionThemesBlock'    => SectionThemesBlock::get_section_type(),
    'SectionFeaturedCourse' => SectionFeaturedCourse::get_section_type(),
    'SectionCallForAction'  => SectionCallForAction::get_section_type(),
    'SectionContactSheet'   => SectionContactSheet::get_section_type()
    //....
  ));

$f = GridField::create('Sections', "Sections", $this->Sections(), $c);
$fields->addFieldToTab("Root.Sections", $f);

Based on the GridFieldConfig_RelationEditor, just remove GridFieldAddNewButton then add GridFieldAddNewMultiClass. Then configure the component to know which classes to have available in the dropdown to create. All those SectionThemesBlock, SectionFeaturedCourse etc extend a common Section dataObject as base. The get_section_type() function is a custom static function on the Section dataobject to get a nice looking name in the dropdown and not have to type it manually all the time....

The basics of the Section dataobject looks like so:

class Section extends DataObject {

  public static function get_section_type()
  {
    return trim(preg_replace('/([A-Z])/', ' $1', str_ireplace('Section', '', get_called_class())));
  }

  //...
}

And the page where that gridField goes and that has the relation defined on:

class Page extends SiteTree {
  //...
  private static $has_many = array(
    'Slides' => 'Slide'
  );
  //...
}

Related:


SilverStripe 3: How to group a sorted array by grandparent pages


php,sorting,grouping,silverstripe
I'm trying to loop through all my ProductPages while grouping them by their grandparent's title (as this is the product category). I would also like to sort the product pages under each group by their ProductReleaseDate descending. Finally if possible, any that do not have a ProductReleaseDate to be listed...

How to find /mysite/ folder - SilverStripe CMS


silverstripe
I'm having some trouble finding my /mysite/ folder - I'm trying to edit my _config.php file so I can add some schema to a website however, there is no /mysite/ folder. Is it possible it was renamed something else, or simply doesn't exist? Is there a way I can find...

Where does the value of $Form come from, when using a standard SilverStripe template?


forms,silverstripe
The Page Layout template for the "simple" theme (the default) contains: <% include SideBar %> <div class="content-container unit size3of4 lastUnit"> <article> <h1>$Title</h1> <div class="content">$Content</div> </article> $Form $CommentsForm </div> Where does the value of $Form come from? ie to make use of it, what do I need to do? Is it...

Silverstripe userform module, adding custom fields


php,silverstripe
I'm adding a custom drop down to the userform module and everything seemed to work, however: If I set the following: [Textfield One] - Dropdown option one [Textfield Two] - Dropdown option two It seems to only save the the setting under Textfield One, and ignores Textfield Two. If I...

Treat SVG as an image in Silverstripe


php,silverstripe
I'm trying to upload images in Silverstripe and have them treated as images rather than a document link. I've added SVG to the allowed filetypes and can upload an SVG file but when I hit insert the editor inserts the tag as <a href="/assets/uploads/somefile.svg">somefile.svg</a> I would like to treat SVG...

How can I translate the managed models tabs?


silverstripe
In my Silverstripe module I use $managed_models to create tabs on the right top, see here: My code: private static $managed_models = array( 'HomeRental', 'City', 'HomeType' ); How can I translate the name of this tabs? I use the Silverstripe-translate module as well. Thanks in advance!...

How to set a custom value for records in a Report?


silverstripe
The code below is a custom report I'm putting together, using SilverStripe 3.1. The Title and ClassName values are working fine, but though I can get the Status for each Page I'm not sure how to set the Status value against each Page in the DataList. How can I do...

Silverstripe Gridfield filters not working


silverstripe
I have a site which I've just updated from 2.4 -> 3.1. On my local environment everything works fine, but once uploaded to production the search filters on GridFields do not work. No errors that I can see, they just don't filter anything. Has anyone come across this? class Product...

Generate the password hash using php


php,encryption,passwords,sha1,silverstripe
i have a DB of users and their password in encrypted form. I want to let the users log in to the site using the username and password. For authentication i need to know what type of encryption is their so that i can match the entered password with the...

Storing a config setting against a class in SilverStripe


class,configuration,silverstripe
I have a class extending DataObject, let's call it ExtendedDataObject, that I want to have a single config setting for; in particular, an email address. ie I want to store one email address for the class, not one per instance. If I have multiple classes extending my ExtendedDataObject, then each...

Silverstripe: How to allow javascript in tinymce?


tinymce,silverstripe
Admins and editors on one of my sites wants to include some javascript embeds like twitter, facebook, instagram or whatever, on pages. I thought it would be really easy to just set the extended_valid_elements to allow the script tag in the html editor of tinyMCE, but it apparently doesn't work....

SilverStripe 3.1 GridField file link is re-written with HTML Entities


hyperlink,silverstripe
I'm very new to Silverstripe (3.1). I am using it to collect applications from users. Each user uploads a file which later in the CMS somebody can download. There is a has_one relationship for the file called 'Document.' I want to link to that file inside a GridField. So after...

Silverstripe Image Upload is changing name


php,silverstripe
I am uploading an image and while storing the image, I am setting the Filename like 'assets/Uploads/54f092af271b9.png' but after saving, the Filename fields loses some part. It becomes 'assets/54f092af271b9.png' losing the "Uploads/" directory altogether. Is it supposed to happen? Here's the codes: <?php $img = new Image(); $baseName = pathinfo($file,...

A simple AJAX example - Silverstripe


silverstripe
Would anyone be able to post a simple example of working with AJAX requests in silverstripe? Couldn't find much documentation on this. Let's say we create a page type 'AjaxPage' and have the controller output a form with a text box to enter your name and a submit button. I'd...

SilverStripe wysiwyg style constraints


css,tinymce,wysiwyg,silverstripe
Is there any way to prevent certain custom wysiwyg styles to be applied to certain HTML elements? Suppose I have two styles called "red" and "framed". One colours text red and the latter puts a frame around photos. Can I control which styles are available when highlighting text or an...

Is it possible to get Children of children in a SilverStripe menu?


navigation,silverstripe,template-variables
I'm putting together a 3 tier navigation menu in a SilverStripe 3.1 template, and have the following code in my template: <% loop $Menu(1) %> ... <% loop $Children %> ... <% loop $Children %> <li><a href="$Link">$Model</a></li> <% end_loop %> <% end_loop %> <% end_loop %> However I'm not getting...

Silverstripe - Different tabs and fields per page


silverstripe
In SilverStripe 3.1 is it possible to add different tabs and fields on the about page for example. And then different tabs and fields on a services page for example. About Page - Images Tab / Attachments Tab Services Page - Images Tab / Attachments Tab / Staff Person Tab...

Silverstripe PaginatedList - How to display record numbers on current page?


pagination,silverstripe
In silverstripe PaginatedList we can display Page numbers like 'Showing 1 of 5 Pages'. How to display record number on each page for example "Showing 1-10 record of 100"

silverstripe query does not work, best way to debug?


sql,silverstripe,datalist
I have this silverstripe query that does not work ( it outputs all messages and not the ones with the date range ) What would be the best way to tackle this query? Im fairly new to silverstripe and havent been able to find information on how to print the...

Silverstripe hash link rewriting putting unwanted slash in link


hash,hyperlink,rewrite,anchor,silverstripe
Ok so I am developing a site in Silverstripe CMS that is using anchor links on one of it's pages. Thing is that in Silverstripe there is a rewrite going on that puts slashes before the hash in your links. So in the docs it explains that you can put...

SilverStripe GridField Download Links without the Ajax


php,ajax,silverstripe
This is a continuation of an issue I was having earlier where a link needed to be inserted into a SilverStripe GridField for each item. Now the idea is that instead of a link there will be a custom action to initiate the download so there needs to be a...

Silverstripe Advancedwrokflow with DataObject not showing button


silverstripe
I am using SilverStripe (version 3.1.6) with advancedworkflow addon. My site is build mostly on data objects. I have assigned both object and model admin in the .yml file as described in the documentation: MyObject: extensions: - WorkflowApplicable MyObjectAdmin: extensions: - AdvancedWorkflowExtension I have also assign the workflow in the...

Angular - REST Service XML Links with $Resource


angularjs,rest,silverstripe
I am using Angular with SilverStripe for my backend. I am using this module https://github.com/silverstripe/silverstripe-restfulserver for my REST API instead of building it myself. When the API returns a list with a many_many relationship it looks like <Recipe href="http://localhost/groceryList/api/v1/Recipe/2.xml"> <Title>Chilli</Title> <ID>2</ID> <Ingredients linktype="many_many" href="http://localhost/groceryList/api/v1/Recipe/2/Ingredients.xml"> <Ingredient...

Separate home controller for subsite in silverstripe 3


silverstripe
I have successfully installed subsite module in silverstripe, I just wanted to know that, is it possible to create 2 separate HomePage controller for main site and subsite. I am trying to remove gallery field and tab from subsite home page not from main site home page.

Why is my custom SS3.1 report returning an error “[Notice] Object of class GridState_Data could not be converted to int”


php,report,silverstripe
As an exercise to help me learn about writing custom reports, I've written a very simple custom report to list pages by their page type. I wrote the code based on the standard report cms/code/reports/BrokenLinksReport.php (included as part of the CMS) but am getting an error: [Notice] Object of class...

Move metadata field in SilverStripe CMS?


content-management-system,silverstripe
I am trying to move the default Metadata field in the SilverStripe CMS but I can't seem to do so successfully. This is what I have tried: $fields->removeFieldFromTab("Root.Content.Main", "Metadata"); $fields->insertBefore(new FormField('Metadata', 'Metadata'), 'Content'); But it does not work. I can remove this field but adding it back in where I...

SilverStripe Overiding Template SS3.1.10


templates,silverstripe
I'm trying to override the template of page depending on image size. When I check the pipeline with ?showtemplate it says teh correct template is rendering but fact it's the default. My controller is bellow class Artwork_Controller extends Page_Controller { private static $allowed_actions = array ( ); public function init()...

How to sort and filter children in Silverstripe


silverstripe
I have this function: public function LatestArticles() { return $this->Children() ->filter('PublicationDate:LessThanOrEqual', SS_Datetime::now()) ->sort('PublicationDate','desc'); } It's not outputting anything at all. If I switch it to this: public function LatestArticles() { return ArticlePage::get() ->filter('PublicationDate:LessThanOrEqual', SS_Datetime::now()) ->sort('PublicationDate','desc'); } It outputs my ArticlePages, but that (obviously) includes all ArticlePages site wide, not just...

using has_one, has_many, and many_many in controller/to filter queries


silverstripe
I have watched lesson 7 - lesson 10 videos on the silverstripe.org website, have read all the articles on this page: http://docs.silverstripe.org/en/3.1/developer_guides/model/, been all up in the API and have googled silverstripe/stackoverflow forums for hours. I am really stuck trying to apply what I learned though. Here is what I'm...

Silverstripe loop


templates,silverstripe
<%loop $Video.Limit(4) %><% end_loop %> <%loop $Video.Limit(5,9) %><% end_loop %> Two divs are being used to display the data,the above code is not working. Any help is accepted....

Using the url query parameter with a controller


php,silverstripe,oembed
I am attempting to implement an oembed provider using the Silverstripe framework but have come across an issue. I have a controller routed from the url /omebed.json and it works fine if I call something like /omebed.json?mediaurl=mymovie.mp4. However the Oembed standard states it should be /omebed.json?url=mymovie.mp4 But Silverstripe internally checks...

Silverstripe - How to Allow Comments For A Blog Entry


comments,blogs,silverstripe
I'm setting up my first ever site in Silverstripe and I'm having trouble with the silverstripe blog module (https://github.com/silverstripe/silverstripe-blog/tree/0.3). I've installed it and the two supporting modules comments (https://github.com/silverstripe/silverstripe-comments) and grid-field-bulk-editing tools and they all seem to have installed without incident. Finally, I've checked the box in the setting for...

How can I add a blank value to a Silverstripe DropDownField and require a non-blank value to be selected?


select,silverstripe
How can I add a blank value to a Silverstripe DropDownField and require a non-blank value to be selected? Currently, my field is: $myField = new DropdownField( 'MyField', "So what'll it be, yes or no?", array( "N"=>"No", "Y"=>"Yes") ); Am using SilverStripe 3.1...

How can I restrict view access to a page type in SilverStripe to admin users only?


silverstripe,access-control
How can I restrict view access to a page type in SilverStripe to admin users only? I'd prefer to do it through the code itself, but through the CMS would be good to know too. I think this answer gets me most of the way there; I just need to...

SilverStripe 3.1.x How to Render a Page with a specific template from an Extension?


templates,module,controller,rendering,silverstripe
SilverStripe 3.1.x How to Render with a specific template from an Extension? In SilverStripe 3.1.x, How can I make a Page render with a specific template from a Module Extension? I'm working on a module that allows an administrator to change the display behaviour of a page at specific timeframes....

SilverStripe 3: Loop greatgrandchildren pages WITH OUT grouping by parent


sorting,loops,templates,silverstripe
In my template I'm looping my child pages as 2 separate lists and then their grandchild pages under these. But I want these greatgrandchild pages to be sorted as an entire list and not divided / grouped by their parents. Simply loop after loop does not work as all greatgrandchild...

SilverStripe Swipestripe - swipestripe-gallery module error


silverstripe,swipestripe
I've installed Swipestripe module. and added swipestripe-gallery module for product images. when I did dev/bulid?flush=1 it shows error Class 'Gallery_PageExtension' does not exists I have seen gallery.yml file in swipestripe-gallery/_config folder, which has Product: extensions: - 'Gallery_PageExtension' but this class is not there in Product.php file in swipestripe-gallery/code folder. After...

Silverstripe add dataobject to Siteconfig


php,silverstripe
I am trying to add a dataobject to the site config (Settings). I have managed to add a logo field and this works now I would like to add some repeater fields eg (social media fields). This doesn't work. I am getting an error. Code and error below. I have...

When creating a nav in SilverStripe, what is the number after $Menu?


navigation,silverstripe,template-variables
The below template code from one of the tutorials on the official SilverStripe website ("How to create a navigation menu") includes the template variable $Menu(1) What does the (1) mean? What happens if it gets changed to something else? <ul> <% loop $Menu(1) %> <li> <a href="$Link" title="Go to the...

How can I get which Page my function is used on?


php,controller,blogs,silverstripe
Silverstripe: I have a blog on my page with muliple pages, and 10 entries per page. I want my controller to know which page of the blog is currently being viewed. I have the following code: public function dependsOnPage() { $page = getPageHere; /*How can I get which page of...

Silverstripe UserForms - populate options from DataObject


php,forms,silverstripe
I'm very new to Silverstripe, On the CMS page I can add a dropdown form item, and I see that I can create options. BUT. What I want to do is populate the dropdown from an array. How can I do this? The array will look something like this: $dropdownFields...