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.

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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

populating DependentDropdownField with a parent/child page relationship


Go to End


2 Posts   1553 Views

Avatar
benald

Community Member, 10 Posts

10 January 2017 at 4:21pm

Hi guys,

I need some help with the DependentDropdownField module.
I am using a Front end form on NewListingPage.php to create new 'Item' DataObjects, each Item is assigned a CategoryPage & SubCategoryPage for display filters and the page structure in the CMS is as follows.

- CategoryPage (This Page lists all child Sub Category Pages)
-- SubCategoryPage (This Page lists all related Item DataObjects)

When creating a new Item using NewListingPage.php, the user must first select the Category and SubCategroy. I want to use the DependentDropdownField to assign these pages to the DataObject.

This is my code:

NewListingPage.php

public function NewItemForm() {

        // Get the Lottery Feed
        $map = LotteryFeed::get()->map('ID','Title');

        // Get the Child Pages of the selected Category
        $subcategorySource = function () { 
            $subcategory = SubCategoryPage::get()->map('ID','Title'); 
            return $subcategory->filter('ParentID', $parent_)->sort('Date', 'DESC')->map('ID', 'Title')->toArray();
        };

        // Create the fields
        $fields = FieldList::create(
            TextField::create('Title', 'Product Name'),
            TextField::create('Description', 'Product Description'),
            TextField::create('Brand', 'Product Brand'),
            TextField::create('Year', 'Product Year'),
            OptionsetField::create('LotteryFeedID', 'LotteryFeed', $map, $map[0]),
            $categoryField = DropdownField::create('CategoryPage', 'Select a Category', CategoryPage::get()->sort('Title', 'ASC')->map('ID', 'Title'))->setEmptyString('- Please Select -'),
            DependentDropdownField::create('SubCategoryPage', 'Select a Sub Category', $subcategorySource)->setEmptyString('-- Please Select --')->setDepends($categoryField),
            TextField::create('Price', 'Price'),
            FileAttachmentField::create('ProofImage', 'Proof Image'),
            FileAttachmentField::create('Images', 'Product Images')
        );

        // Create actions
        $actions = new FieldList(
            FormAction::create('submit', 'Create New Item')
        );

        $required = new RequiredFields('Name', 'Description');
        $form = new Form($this, 'NewItemForm', $fields, $actions, $required);

        return $form;
    }

The result of this code is the Category Dropdown gets all Category Pages, but the SubCategory Dependant Dropdown lists only the EmptyString twice.

If I change the code to the following I can list all CategoryPages and all Sub Category Pages, but there is no dependancy on selection of the CategoryPage.

public function NewItemForm() {
        
        $map = LotteryFeed::get()->map('ID','Title');

        // Create fields
        $fields = FieldList::create(
            TextField::create('Title', 'Product Name'),
            TextField::create('Description', 'Product Description'),
            TextField::create('Brand', 'Product Brand'),
            TextField::create('Year', 'Product Year'),
            OptionsetField::create('LotteryFeedID', 'LotteryFeed', $map, $map[0]),
            $categoryField = DropdownField::create('CategoryPage', 'Select a Category', CategoryPage::get()->sort('Title', 'ASC')->map('ID', 'Title'))->setEmptyString('- Please Select -'),
            DependentDropdownField::create('SubCategoryPage', 'Select a Sub Category', SubCategoryPage::get()->sort('Title', 'ASC')->map('ID', 'Title'))->setEmptyString('-- Please Select --')->setDepends($categoryField), 
            TextField::create('Price', 'Price'),
            FileAttachmentField::create('ProofImage', 'Proof Image'),
            FileAttachmentField::create('Images', 'Product Images')
        );

        // Create actions
        $actions = new FieldList(
            FormAction::create('submit', 'Create New Item')
        );

        $required = new RequiredFields('Name', 'Description');
        $form = new Form($this, 'NewItemForm', $fields, $actions, $required);

        return $form;
    }

I get that the function must evaluate the child pages of the selected Category, but I'm no PHP expert and cannot fathom how to do it.
Can someone please assist me with resolving this?

Avatar
martimiz

Forum Moderator, 1391 Posts

3 February 2017 at 4:00am

I don't know the module, but it feels like the problem is here:

$subcategory = SubCategoryPage::get()->map('ID','Title'); 
return $subcategory->filter('ParentID', $parent_)->sort('Date', 'DESC')->map('ID', 'Title')->toArray();

In the first line $subcategory is already mapped, then in the second line you treat it as if it is a DataList, that can be filtered and sorted, while it is actually a map...

Try removing the first ->map('ID','Title')