Jump to:

23479 Posts in 18951 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » If statement in getCMSFields

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: 576 Views
  • Stef87
    Avatar
    Community Member
    66 Posts

    If statement in getCMSFields Link to this post

    I apologise if this is really obvious and a stupid question but I'm a little stumped. I'm trying to add a field that is only visible on one page so I am using an if statement in the getCMSFields function of my page.

    My question is is this even possible? Will it be called, etc? The reason I ask is that my if statement doesn't work unless I change getCMSFields to getCMSFields_forPopup which causes a whole new set of problems.

    Please help!

    Thanks in advance.

    P.S. I'm using SilverStripe 2.4.7.

    if ($this->ClassName == 'Movies') {
             
             $movies= DataObject::get('Movie');

        if (! empty($stores)) {

                $map = $movies->toDropdownMap('ID', 'Name');
             
                $checkBox = new CheckboxSetField(
                   $name = "Moviess",
                   $title = "Select Movies",
                   $source = $map
                );

                $fieldset->push($checkBox);
             }
          }

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: If statement in getCMSFields Link to this post

    You should be able to use an if perfectly fine. Though it would be better design to move the functionality to your Movies.php class.

    <?php

    class Movies extends Page {
    ..

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $movies= DataObject::get('Movie');

    if (! empty($movies)) {
    $map = $movies->toDropdownMap('ID', 'Name');
    $fieldset->push(new CheckboxSetField( "Movies", "Select Movies", $map));
    }

    return $fields;
    }

  • Stef87
    Avatar
    Community Member
    66 Posts

    Re: If statement in getCMSFields Link to this post

    Thanks for your reply willr. I am using a Dataobject with a dataobjectmanager in movies. Can I still use this method? I'm relatively new to SilverStripe so my knowledge is limited but I can't see how it'll work that way.

    I'm using 2.4.7, I just realised I never specified that earlier.

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: If statement in getCMSFields Link to this post

    You should be able to use the same setup whether you have dataobjects or pages. Though getCMSFields may be getCMSFields_forPopup if you're using the popup to edit content.

  • Stef87
    Avatar
    Community Member
    66 Posts

    Re: If statement in getCMSFields Link to this post

    Ok that makes sense. Thank you. It's still not working exactly as I want it to but I have a better understanding of what is going on. I think my confusion lies with DataObjectManager though as it creates the popup for editing the data and is created within getCMSFields of Movies. If I understand correctly then I need to use getCMSFields_ForPopup in Movies instead?

    576 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.