Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup

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 2
Go to End
Author Topic: 1736 Views
  • RichMcNabb
    Avatar
    Community Member
    34 Posts

    [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup Link to this post

    Hi,

    I am trying to create a staff profile page and use a DropDownField for the $Location via a pop-up window in the CMS. This is what I have done so far but I am not sure where to add an array for the office locations (i.e. wellington, auckland, christchurch, london) so the dropdown menu are populated with values.

    Any help would be awesome

    <?php

    class Staff extends DataObject {

    static $db = array(
    'FirstName' => 'Text',
          'LastName' => 'Text',
          'Email' => 'Text',
          'Location' => 'Varchar(100)',
          'JobTitle' => 'Text',
          'JobDescription' => 'Text',
    );
       
    static $has_one = array(
    'Page' => 'StaffPage',
          'StaffProfileImage' => 'CustomImage',
          'Location' => 'Location',
          
    );
    static $fields = array(
    'FirstName' => 'FirstName',
          'LastName' => 'LastName',
          'Email' => 'Email',
          'JobTitle' => 'JobTitle',
          'JobDescription' => 'JobDescription',
    );

    public function getCMSFields_forPopup() {
    $fields = new FieldSet();

    $fields->push(new TextField('FirstName', 'First name'));
    $fields->push(new TextField('LastName', 'Last name'));
          $fields->push(new DropDownField('Location', 'Location'));
          $fields->push(new TextField('Email'));
          $fields->push(new TextField('JobTitle', 'Job Title'));
          $fields->push(new TextareaField('JobDescription', 'Job Description - write a couple of paragraphs to describe your role at CDP.'));
          $fields->push(new ImageField('StaffProfileImage', 'Staff profile image'));

    return $fields;
    }

    }
    ?>

    P.S. I did find this link (http://silverstripe.org/archive/show/141863#post141863) but wasn't able to reply as topic was archived.

    - Rich McNabb

  • RichMcNabb
    Avatar
    Community Member
    34 Posts

    Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup Link to this post

    Updated code to this but am now getting - Fatal error: Class 'Location' not found in /var/www/cdpconz/sapphire/core/model/DataObject.php on line 1207

    <?php

    class Staff extends DataObject {

    static $db = array(
    'FirstName' => 'Text',
          'LastName' => 'Text',
          'Email' => 'Text',
          'Location' => "Enum('Wellington,Auckland,Christchurch,London','Wellington')",
          'JobTitle' => 'Text',
          'JobDescription' => 'Text',
    );
       
    static $has_one = array(
    'Page' => 'StaffPage',
          'StaffProfileImage' => 'CustomImage',
          'Location' => 'Location',
          
    );
    static $fields = array(
    'FirstName' => 'FirstName',
          'LastName' => 'LastName',
          'Email' => 'Email',
          'JobTitle' => 'JobTitle',
          'JobDescription' => 'JobDescription',
    );

    public function getCMSFields_forPopup() {
    $fields = new FieldSet();

    $fields->push(new TextField('FirstName', 'First name'));
    $fields->push(new TextField('LastName', 'Last name'));
          $fields->push(new DropDownField('Location', 'Location', singleton('Staff')->dbObject('Location')->enumValues()));
          $fields->push(new TextField('Email'));
          $fields->push(new TextField('JobTitle', 'Job Title'));
          $fields->push(new TextareaField('JobDescription', 'Job Description - write a couple of paragraphs to describe your role at CDP.'));
          $fields->push(new ImageField('StaffProfileImage', 'Staff profile image'));

    return $fields;
    }

    }
    ?>

  • EdP
    Avatar
    Community Member
    13 Posts

    Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup Link to this post

    You need to tell the dropdown where to get its options. If you've only got four hard-coded options then you could use an ENUM, otherwise something from a table.

    Here's a link to some documentation: http://doc.silverstripe.org/dropdownfield

    Ed

  • RichMcNabb
    Avatar
    Community Member
    34 Posts

    Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup Link to this post

    Hi Ed,

    Thanks for your reply I did get the drop down field working using the ENUM using that link you posted. The problem I am having now is: Fatal error: Class 'Location' not found in /var/www/cdpconz/sapphire/core/model/DataObject.php on line 1207

    I am calling this from the StaffPage.ss template using the $Location. Any Ideas?

    - Rich

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup Link to this post

    Your using Location in your $db array and in your $has_one array. So SS will probably search for Location in the $has_one array.

  • RichMcNabb
    Avatar
    Community Member
    34 Posts

    Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup Link to this post

    Hi Martijn,

    Thanks for your message.
    I am quite new to SS so wasn't sure if I need to put Location in the $has_one = array or the $fields = array? Hopefully it is in the right place in the $has_one.

    To access the $Location from the $has_one array how would I do that?

    - Rich

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup Link to this post

    If you don't have a seperate Location DataObject, you don't.

    Also if you want to add a seperate Location DataObject with a hasone relation, you have to avoid giving the same name to the relation as a DataObject field.

  • RichMcNabb
    Avatar
    Community Member
    34 Posts

    Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup Link to this post

    Hi Martijn,

    Thanks your help!! I got it to work by moving it from $has_one = array to $fields = array. One last question if you have time. The staff members are displayed in order of when the were added into the CMS. Is there an easy way to have them displayed by last name? Really appreciate your help. Cheers!

    BTW I have paste the code below in case anyone else is having the same issue and wants some help.

    <?php

    class Staff extends DataObject {

    static $db = array(
    'FirstName' => 'Text',
    'LastName' => 'Text',
    'Email' => 'Text',
    'Location' => "Enum('Wellington,Auckland,Christchurch,London','Wellington')",
    'JobTitle' => 'Text',
    'JobDescription' => 'Text',
    );

    static $has_one = array(
    'Page' => 'StaffPage',
    'StaffProfileImage' => 'CustomImage',

    );
    static $fields = array(
    'FirstName' => 'FirstName',
    'LastName' => 'LastName',
        'Location' => 'Location',
    'Email' => 'Email',
    'JobTitle' => 'JobTitle',
    'JobDescription' => 'JobDescription',
    );

    public function getCMSFields_forPopup() {
    $fields = new FieldSet();

    $fields->push(new TextField('FirstName', 'First name'));
    $fields->push(new TextField('LastName', 'Last name'));
    $fields->push(new DropDownField('Location', 'Location', singleton('Staff')->dbObject('Location')->enumValues()));
    $fields->push(new TextField('Email'));
    $fields->push(new TextField('JobTitle', 'Job Title'));
    $fields->push(new TextareaField('JobDescription', 'Job Description - write a couple of paragraphs to describe your role at CDP.'));
    $fields->push(new ImageField('StaffProfileImage', 'Staff profile image'));

       return $fields;
       }

    }
    ?>

    1736 Views
Page: 1 2
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.