Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Link Page with multiple pages with GridField


Reply

3 Posts   337 Views

Avatar
Allexx

10 October 2013 at 10:07pm (Last edited: 10 October 2013 10:08pm), Community Member, 2 Posts

Hello,
I want to add a form where the user in the backend can link a page with multiple other pages. Something like "You might also be interested in..." but NOT dynamically generated.
I found this tutorial:
http://blog.fraser-hart.co.uk/silverstripe-3-gridfield-with-many_many-relationship/
but the problem is, that when I set a relation the site tree is modified.
What I want is something like:
Frontpage is linked with About page
Contact page is linked with About Page and Imprint
etc.

How do I have to modify this code to achieve this?

public static $has_many = array(
    'Boxes' => 'Page'
    );

   public function getCMSFields() {
    $fields = parent::getCMSFields();
    $gridFieldConfig = GridFieldConfig_RelationEditor::create()->addComponents(
         new GridFieldDeleteAction('unlinkrelation')
      );
   
    $gridField = new GridField("Boxes", "Boxes", $this->Boxes(), $gridFieldConfig);
    $fields->addFieldToTab("Root.Boxes", $gridField);
    return $fields;
   }

Avatar
Allexx

11 October 2013 at 4:35am Community Member, 2 Posts

With help of this post http://www.silverstripe.org/general-questions/show/21867 I'm able to select multiple posts, but the values are not saved and I cannot access them from my template.
How can I save my selection in the database and access them from my template?

Avatar
zenmonkey

14 October 2013 at 6:48pm Community Member, 528 Posts

I suspect you're trying to assign a $has_many with a corrsponding $has_one relation. Also realitistally what you probably want is for Page to have $many_$many $belong_many_many relation with itself since any page can actually belong to multiple other pages http://doc.silverstripe.org/framework/en/topics/datamodel#many-many