Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

User Population of DropDownField


Reply


4 Posts   1768 Views

Avatar
taligent

Community Member, 18 Posts

19 December 2009 at 3:49am

I am getting started with silverStripe and need a good kick in the butt. I am trying to figure out how to add a field in the admin area that page editors can use to enter in a description and a url. I then want to populate a DropDownField in the front end with the description so that site visitors can click the dropdownfield and select a link to go to a new section of the site or to an external site.

I have searched quite a bit through the forums and docks, but can't seem to get started on this. Anyone able to get me started?

Avatar
Pigeon

Community Member, 243 Posts

19 December 2009 at 5:47am

Hmmm, it's a bit difficult to know exactly what you are getting at, but using the 'toDropdownMap' function of DataObjects might help.

Something like:

$do = DataObject::get('Page');
$dropdownArray = $do->toDropdownMap('ID','Description');
/* Produces array like:
array(
[ID] => [Description],
....
);
*/
$field = new DropdownField('[Label]','[FieldName]',$dropdownArray);

But it depends a bit on how you then want to have the links working, etc.

There is another way, which is to just do a control loop through the Pages and then list out all of the descriptions/links, etc:

class Page extends SiteTree {
...
function getAllPages() {
return DataObject::get('Page');
}
}


(in Page.ss)

<label for="myDropdown">A dropdown</label>
<select name="myDropdown" id="myDropdown">
<% control Pages %>
<option value="$ID">$Desctiption</option>
<% end_control %>
</select>

Hope that helps! Sorry if there are any sloppy mistakes or it completely misses what you were getting at.

Avatar
taligent

Community Member, 18 Posts

19 December 2009 at 10:25am

Thanks for the reply. My description was a bit vague. For a visual of what I want to do, go to www.send.hr. In the right side of that page is a dropdownfield populated with links. I need to replicate that into SilverStripe. But I want the site editor to maintain the links in the backend rather than hard coding them into an array in the php.

So I need two things. First, how do I set up an entry form in the back end so that site maintainers can enter in multiple descriptions and urls. Second, how do I read that information into a dropdownfield on the web page so visitors can select the links.

Hopefully that is a little clearer.

Avatar
Pigeon

Community Member, 243 Posts

21 December 2009 at 2:04pm

Hmmm... Ok. Maybe this is what you want:

Set up a DataObject that is 'DropDownElement' and that would have: PageLink and Name.

The Name would be Text and the Object would has_one Page. You can then use the SimpleSiteTreeDropdownField to allow the user to select a page :)

Something like (sorry for syntax errors):

class DropDownElement extends DataObject {
$db = array (
'Description' => 'VarChar(255)'
);
$has_one = array ('Page' => 'Page');

function getCMSFields_forPopup() {
...
$fields = new SimpleSiteTreeDropdownField('PageID', 'Page Link','SiteTree');
$fields = new TextField('Description');
...
}
...
}

You would then control through all the data objects and create the dropdown in the front end.

I'm doing this all from memory and really needs more digging, but i hope that helps.