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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Help getting a page link (CheckboxsetField problem)


Go to End


2 Posts   839 Views

Avatar
woodb2

Community Member, 48 Posts

13 September 2011 at 7:29am

I have a Department page. I create several Department pages (Dept A, Dept B Dept C)

I have a Jobs DataObject with a field "ASIA" that has a CheckboxsetField that displays the Departments Page Titles you can select from.

$Rolesob = DataObject::get('Department');

$Rolesob = $Rolesob->toDropDownMap('Title', 'Title');

$fields->addFieldToTab('Root.ASIA', new CheckboxsetField('ASIA', 'ASIA (create Department Page first)', $Rolesob));

When I display the $ASIA on the template I get "Dept A, Dept B" as 1long string. Searching through the forum I found someone had a similar issue and the answer was that the result needed to be exploded with a function:

//Get Explode Title from $ASIA

function ExplodedTopics()

{

$set = new DataObjectSet();

if($ASIA = $this->ASIA) {

foreach(explode(',',$ASIA) as $key => $Title) {

$set->push(new ArrayData(array('Title' => $Title)));

}

}

return $set;

}

This function explodes the results perfectly. I place this in my template and I get the results I need.

<% control ExplodedTopics %>

<p>$Title</P>

<% end_control %>

My problem is this. The titles that are being displayed come from a Department page. How do I make them links back to their pages?

Thanks,
Brian

Avatar
Carbon Crayon

Community Member, 598 Posts

13 September 2011 at 10:06am

Edited: 13/09/2011 10:06am

Hi Woodb2,

Is your ASIA field a many_many? The way I would set it up would be like so, the following being in your Job dataobject (I have called your ASIA field 'Departments' just because that is convention, obviously in your code you can replace Deparments with ASIA if you wish):


static $many_many = array(
'Departments' => 'Department'
);

public function getCMSFields()
{
....
if($Departments = DataObject::get('Department'))
{
$fields->addFieldToTab('Root.Content.Departments', new CheckboxsetField('Deparments', 'Select some deparements', $Departments->map());
}
...
}

Then in your template you can simply loop through the Departments field and get anything that is on each Department from within the Loop:

<% control Departments %>
<a href="$Link">$Title</a>
<% end_control %>

Hope that helps :)

Aram

www.ssbits.com - SilverStripe Tutorials, Tips and other Bits