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.

Data Model Questions /

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

Access page details from linked pages in GridField?


Go to End


928 Views

Avatar
suntrop

Community Member, 141 Posts

15 April 2013 at 8:22am

I added a GridField to my pages to store related pages. Currently I just store the link to the related page in the GridField but I'd like to have all details of the related page (content, title, etc.) in my page templates. How do I get access to all that data? Do I have to load them in my page.php or is there a way just within my templates?

In my template I don't have access to the content and other details of the related page. Only the fields from the GridField.

<% control RelatedPages %>
			<div>
				<span>$Title</span>
				<span>$Meta.Description</span>
				<p>$Content</p>
			</div>
		<% end_control %>

function getRelatedPages() {
	return DataObject::get("RelatedPage");
}

public function getCMSFields() {
		$fields = parent::getCMSFields();

		$displayColumns = new GridFieldDataColumns();
	    $displayColumns->setDisplayFields(array(
			'Relted.MenuTitle' => 'Resort'
	    ));

		$gridFieldConfig = GridFieldConfig::create()->addComponents(
			new GridFieldToolbarHeader(),
			new GridFieldAddNewButton('toolbar-header-right'),
			new GridFieldEditButton(),
			new GridFieldDeleteAction(),
			$displayColumns
	    );

	    $gridField = new GridField("Related", "Pagelist", $this->relatedPages(), $gridFieldConfig);

	    $fields->addFieldToTab("Root.Main", $gridField);

		return $fields;
	}