Description
Extension of SilverStripe form fields to allow adding attributes to child elements on dropdown and checkboxset fields., (*1)
Maintainer Contact
Marijn Kampf
, (*2)
Exadium Web Development and Online Marketing. Visit http://www.exadium.com for more information., (*3)
Installation
composer require "exadium/extra-attributes-field":"*"
The module should work out of the box on a standard installation of SilverStripe. If you have Bootstrap Forms module installed by, (*4)
\extra-attributes-field\templates\
\themes\bootstrap\templates, (*5)
Usage
public function getCMSFields() {
$fields = parent::getCMSFields();
$count = array('1' => 'One', '2' => 'Two', '3' => 'Three');
$batties = array('1' => 'Batty', '2' => 'Batty batty', '3' => 'Batty batty batty');
$fields->push(
DropdownAttributesField::create('DropdownAttributesCount', 'DropdownAttributesField Count', $count)
->setOptionsAttributes('data-bats', $batties)
);
$fields->push(
CheckboxSetField::create('CheckCount', 'CheckboxSetField Count', $count)
->setOptionsAttributes('data-bats', $batties)
);
return $fields;
}
Generates code like, (*6)
DropdownAttributesField Count
One
Two
Three
Or you can load from a database field map. Also shows adding multiple options., (*7)
$fields->push(
DropdownAttributesField::create('Members', 'Members', Member::get()->map('ID', 'Name'))
->setOptionsAttributes('data-email', Member::get()->map('ID', 'Email'))
->setOptionsAttributes('data-firstname', Member::get()->map('ID', 'FirstName'))
);
Notes
If you use silverstripe-bootstrap-forms copy BootstrapCheckboxSetField.ss to your templates folder. If you use your own custom Field templates add, (*8)
$OptionAttributesHTML
to your input/option/checkbox/..., (*9)
Requirements
SilverStripe 3.1, (*10)