Ah okay. I think you want to use a TreeDropdownField - it should do most of the work for you. See example at http://api.silverstripe.org/2.4/forms/fields-relational/TreeDropdownField.html under Class Details
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
- Previous 10 entries
- 1
- Page 22(current)
Thanks JonoM!
For what i unterstand from the API (which might not be too much), the TreeDropdownfield ist not what i need.
This $pages = $pages->toDropdownMap('ID', 'Title', '(Select one)', true); } function works well and seems to be similar to TreeDropdownfield.
I am looking for an idea, or better a code howto, because i already have an idea and just don't know how to implement it.
I would like to automate the process of fetching all classtype for pages, get the associated DataObjects and merge them together.
You could store a comma separated list of classnames somewhere, maybe in the SiteConfig, so you can easily update it when you want to add more classnames. Then use that in a controller and iterate over each one, fetching their dataobjects and merging them.
Something like:
$classnames = array('Name1','Name2'); // in reality, this could be some code that fetches the stored classnames from where ever you're keeping them.
$set = new DataObjectSet();
foreach($classnames as $name) {
$objs = DataObject::get("Page", "ClassName = '{$name}'");
$set->merge($objs);
}
return $set;
Hi Tom,
I think you're making more work for yourself than you need to - am I right that you're trying to get a DataObjectSet that contains every page in the website?
That shouldn't be a hard thing to do. Maybe you want to revise all your page type classes so they extend from Page, rather than SiteTree. So you have one class (Page extends SiteTree) then all others are (SomeCustomPage extends Page). Then if you used your function
$pages = DataObject::get('Page');
if ($pages) {
$pages = $pages->toDropdownMap('ID', 'Title', '(Select one)', true);
}
You should get all the custom page types automatically because they extend from Page. However - if you're putting pages in to a dropdown field (which it looks like because you've included the optional blank value 'Select one') then I still think a TreeDropdownfield would be better because it mirrors your site tree structure and nests pages instead of listing them in one long list.
Thank you both for your help!
Now i have a better idea what i should do. I'm going to give your code a try a asap.
Thanks again, Tom
- Previous 10 entries
- 1
- Page 22(current)