Faloude Faloude - 1 month ago 12
PHP Question

SilverStripe return URLSegment from SiteTree relation

So I have this:

private static $has_one = array(
'ButtonLink' => 'SiteTree'
);


and this field

$fields->addFieldToTab('Root.Main', TreeDropdownField::create("ButtonLink", "Page link", "SiteTree"));


So that users can link this DataObject to a page from SiteTree. On the front-end I'm trying to return the
URLSegment
as:

$ButtonLink.Link


But it doesn't return anything.

Answer

Found it. You need to append ID to the field identifier because that's the db column name where the foreign record reference key is saved (SiteTree).

$fields->addFieldToTab('Root.Main', TreeDropdownField::create("ButtonLinkID", "Page link", "SiteTree"));