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.

General Questions

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

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

Help getting a page link (CheckboxsetField problem)


Reply

2 Posts   508 Views

Avatar
woodb2

13 September 2011 at 7:29am Community Member, 48 Posts

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

13 September 2011 at 10:06am (Last edited: 13 September 2011 10:06am), Community Member, 598 Posts

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