Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Help getting a page link (CheckboxsetField problem)

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

Page: 1
Go to End
Author Topic: 497 Views
  • woodb2
    Avatar
    Community Member
    48 Posts

    Help getting a page link (CheckboxsetField problem) Link to this post

    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

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Help getting a page link (CheckboxsetField problem) Link to this post

    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

    497 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.