I hit a wall with saving the selected DropdownField while editing a Page. I'm able to grab the different titles, but nothing is saved.
I want to select one Title Column that is inside the Brands class extending DataObject.
I successfully implemented the dropdown field as set up in Chapter 6 of Silverstripe book to use as a search field. This time I'm applying to same way of generating the field but for adding it to a Page's field.
Item.php
public static $many_many = array(
'Brands' => 'Brand'
);
function getCMSFields() {
...
$brands = Product::get_brands();
if($brands){
$brandMap = array_combine($brands,$brands);
} else {
$brandMap = null;
}
$brandField = new DropdownField(
'Brand',
'Brand',
$brandMap
);
$brandField->setHasEmptyDefault(true);
$fields->addFieldToTab('Root.Content.Main',$brandField);
Item.php cont'd
# 6.7.3 Drop-down Menu for Searching brands
public function getDefaultSearchContext() {
$context = parent::getDefaultSearchContext();
$context->removeFieldByName('Brand');
$brands = self::get_brands();
if($brands){
$brandMap = array_combine($brands,$brands);
} else {
$brandMap = null;
}
$brandField = new DropdownField(
'Brand',
'Brand',
$brandMap
);
$brandField->setHasEmptyDefault(true);
$context->addField($brandField);
return $context;
}
public static function get_brands() {
$comp = DataObject::get('Brand');
if(!$comp) return array();
$brands = array_unique($comp->column('Title'));
sort($brands);
return $brands;
}
Brand.php
class Brand extends DataObject {
static $db = array(
'Title' => 'Varchar(255)' ,
);
static $belongs_many_many = array(
'Products' => 'Product',
);
static $searchable_fields = array(
'Title' => 'ExactMatchFilter'
);
}