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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Link Page with multiple pages with GridField


Go to End


3 Posts   1079 Views

Avatar
Allexx

Community Member, 2 Posts

10 October 2013 at 10:07pm

Edited: 10/10/2013 10:08pm

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

Community Member, 2 Posts

11 October 2013 at 4:35am

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

Community Member, 545 Posts

14 October 2013 at 6:48pm

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