21278 Posts in 5728 Topics by 2599 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
| Go to End | Next > | |
| Author | Topic: | 1377 Views |
-
[SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup

31 August 2010 at 12:28pm Last edited: 2 September 2010 12:06pm
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
-
Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup

31 August 2010 at 12:53pm Last edited: 31 August 2010 1:01pm
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;
}}
?> -
Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup

2 September 2010 at 2:45am
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
-
Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup

2 September 2010 at 9:16am
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
-
Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup

2 September 2010 at 9:39am
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.
-
Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup

2 September 2010 at 9:51am
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
-
Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup

2 September 2010 at 10:02am
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.
-
Re: [SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup

2 September 2010 at 10:16am
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;
}}
?>
| 1377 Views | ||
| Go to Top | Next > |



