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?