Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » How do I get a drop-down list in the CMS editor?

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

Page: 1
Go to End
Author Topic: 3814 Views
  • ianpiper
    Avatar
    Community Member
    32 Posts

    How do I get a drop-down list in the CMS editor? Link to this post

    I hope this isn't a FAQ - I have looked but failed to find it.

    One of my fields in the CMS editor is presently a text field, but it contains a limited range of options - basically "left", "middle" or "right" relating to which column on the page will be used to display the information.

    Is it possible to have the editor display a list of options as a pull-down menu? This will limit the risk of typos spoiling the output.

    Thanks,

    Ian.
    --

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: How do I get a drop-down list in the CMS editor? Link to this post

    Sure you can. Simple example:

    class RegionalPage extends Page {

       static $db = array(
          'PageRegion' => 'Int'
       );
       
       static $defaults = array(
          'PageRegion' => 0
       );

       
       function getCMSFields() {
          $fields = parent::getCMSFields();
       
          $options = array("Rest of the World", "Asia");
          $fields->addFieldToTab("Root.Content.Main", new DropdownField("PageRegion", "Acessable to Region", $options), 'Content');
          
          return $fields;
       }    
    }

    Cheers
    Aaron

  • ianpiper
    Avatar
    Community Member
    32 Posts

    Re: How do I get a drop-down list in the CMS editor? Link to this post

    Fantastic. Thanks Aaron.

  • ianpiper
    Avatar
    Community Member
    32 Posts

    Re: How do I get a drop-down list in the CMS editor? Link to this post

    Hmm. Having taken a look at this I am confused. It looks as though the example code is for an integer property. I want to pass back a string - basically one of several position choices of string: "left',"middle" or "right". I will then be picking this up in the template to identify a style that needs to be applied for the relevant choice.

    I took a look at the docs for DropdownField, and found some sample code that uses an Enum to hold an array of string values
    (here: http://doc.silverstripe.org/doku.php?id=recipes:extending_the_event_calendar&s=dropdownfield%20cms%20editor). But this doesn't seem to work either - throws this error when I try to build the database:

    [User Error] Enum::__construct() The default value does not match any item in the enumeration

    So I'm completely confused now. Can you throw some light on how to manage a drop-down list of string values?

    Thanks,

    Ian.
    --

  • ianpiper
    Avatar
    Community Member
    32 Posts

    Re: How do I get a drop-down list in the CMS editor? Link to this post

    <blush>Erm, user error I'm afraid...</blush>

    I had quotes around the individual items in the Enum array instead of the collection of items. So SS was seeing the second array item as the default.

    In case anyone is interested, the fixed version of my code is here:

    class ContentBlockPage extends Page {
       static $db = array(
          'ColumnRef' => "Enum('columnLeft, columnMiddle, columnRight')",
          'RowNumber' => 'Int',
          'BlockColour' => "Enum('blue, bluegrey, brickred, mustardyellow, mossgreen')",
          'TeaserText' => 'HTMLText'
          );
       static $has_one = array(
          'TeaserImage' => 'Image'
          );
       
       static $defaults = array(
          'ColumnRef' => 'columnLeft',
          'RowNumber' => 1,
          'BlockColour' => 'blue'
          );
          
       static $icon = "themes/tellura/images/treeicons/blockpage";
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $rowOptions = array(1, 2, 3, 4, 5, 6, 7, 8);      
          $fields->addFieldToTab("Root.Content.Main", new DropdownField('ColumnRef', 'Which column?', singleton('ContentBlockPage')->dbObject('ColumnRef')->enumValues()), 'Content');
          $fields->addFieldToTab("Root.Content.Main", new DropdownField('RowNumber', 'Row number', $rowOptions), 'Content');
          $fields->addFieldToTab("Root.Content.Main", new DropdownField('BlockColour', 'Header bar colour?', singleton('ContentBlockPage')->dbObject('BlockColour')->enumValues()), 'Content');
          $fields->addFieldToTab("Root.Content.Main", new HTMLEditorField('TeaserText'), 'Content');
          $fields->addFieldToTab("Root.Content.Images", new ImageField('TeaserImage'));
          return $fields;
       }
       
    }

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