2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2017-11-14 09:17:39 +01:00
|
|
|
* Dropdown field, created from a select tag.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Setting a $has_one relation</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Using here an example of an art gallery, with Exhibition pages,
|
2010-10-15 05:55:22 +02:00
|
|
|
* each of which has a Gallery they belong to. The Gallery class is also user-defined.
|
|
|
|
* <code>
|
2016-11-29 00:31:16 +01:00
|
|
|
* static $has_one = array(
|
|
|
|
* 'Gallery' => 'Gallery',
|
|
|
|
* );
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* public function getCMSFields() {
|
|
|
|
* $fields = parent::getCMSFields();
|
|
|
|
* $field = DropdownField::create('GalleryID', 'Gallery', Gallery::get()->map('ID', 'Title'))
|
|
|
|
* ->setEmptyString('(Select one)');
|
|
|
|
* $fields->addFieldToTab('Root.Content', $field, 'Content');
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* As you see, you need to put "GalleryID", rather than "Gallery" here.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Populate with Array</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2021-12-13 09:05:33 +01:00
|
|
|
* Example model definition:
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* class MyObject extends DataObject {
|
|
|
|
* static $db = array(
|
|
|
|
* 'Country' => "Varchar(100)"
|
|
|
|
* );
|
2012-05-23 12:45:04 +02:00
|
|
|
* }
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-23 12:45:04 +02:00
|
|
|
* Example instantiation:
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
2014-11-22 22:01:08 +01:00
|
|
|
* DropdownField::create(
|
2010-10-15 05:55:22 +02:00
|
|
|
* 'Country',
|
|
|
|
* 'Country',
|
|
|
|
* array(
|
|
|
|
* 'NZ' => 'New Zealand',
|
2014-05-28 12:36:51 +02:00
|
|
|
* 'US' => 'United States',
|
2010-10-15 05:55:22 +02:00
|
|
|
* 'GEM'=> 'Germany'
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Populate with Enum-Values</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* You can automatically create a map of possible values from an {@link Enum} database column.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* Example model definition:
|
|
|
|
* <code>
|
|
|
|
* class MyObject extends DataObject {
|
|
|
|
* static $db = array(
|
|
|
|
* 'Country' => "Enum('New Zealand,United States,Germany','New Zealand')"
|
|
|
|
* );
|
2012-05-23 12:45:04 +02:00
|
|
|
* }
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* Field construction:
|
|
|
|
* <code>
|
2014-11-22 22:01:08 +01:00
|
|
|
* DropdownField::create(
|
2010-10-15 05:55:22 +02:00
|
|
|
* 'Country',
|
|
|
|
* 'Country',
|
|
|
|
* singleton('MyObject')->dbObject('Country')->enumValues()
|
|
|
|
* );
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-10-17 11:06:02 +02:00
|
|
|
* <b>Disabling individual items</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-10-17 11:06:02 +02:00
|
|
|
* Individual items can be disabled by feeding their array keys to setDisabledItems.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-10-17 11:06:02 +02:00
|
|
|
* <code>
|
|
|
|
* $DrDownField->setDisabledItems( array( 'US', 'GEM' ) );
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 00:50:48 +02:00
|
|
|
* @see CheckboxSetField for multiple selections through checkboxes instead.
|
|
|
|
* @see ListboxField for a single <select> box (with single or multiple selections).
|
|
|
|
* @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class DropdownField extends SingleSelectField
|
|
|
|
{
|
2011-12-21 17:35:42 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Build a field option for template rendering
|
|
|
|
*
|
|
|
|
* @param mixed $value Value of the option
|
|
|
|
* @param string $title Title of the option
|
|
|
|
* @return ArrayData Field option
|
|
|
|
*/
|
|
|
|
protected function getFieldOption($value, $title)
|
|
|
|
{
|
|
|
|
// Check selection
|
|
|
|
$selected = $this->isSelectedValue($value, $this->Value());
|
2015-02-13 05:35:39 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Check disabled
|
|
|
|
$disabled = false;
|
|
|
|
if ($this->isDisabledValue($value) && $title != $this->getEmptyString()) {
|
|
|
|
$disabled = 'disabled';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
return new ArrayData([
|
2019-01-25 01:34:36 +01:00
|
|
|
'Title' => (string)$title,
|
2016-11-29 00:31:16 +01:00
|
|
|
'Value' => $value,
|
|
|
|
'Selected' => $selected,
|
|
|
|
'Disabled' => $disabled,
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-05-19 05:47:51 +02:00
|
|
|
/**
|
|
|
|
* A required DropdownField must have a user selected attribute,
|
|
|
|
* so require an empty default for a required field
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getHasEmptyDefault()
|
|
|
|
{
|
|
|
|
return parent::getHasEmptyDefault() || $this->Required();
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param array $properties
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
public function Field($properties = [])
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$options = [];
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Add all options
|
|
|
|
foreach ($this->getSourceEmpty() as $value => $title) {
|
|
|
|
$options[] = $this->getFieldOption($value, $title);
|
|
|
|
}
|
2008-10-15 23:44:38 +02:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$properties = array_merge($properties, [
|
2016-11-29 00:31:16 +01:00
|
|
|
'Options' => new ArrayList($options)
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return parent::Field($properties);
|
|
|
|
}
|
2012-03-19 05:06:51 +01:00
|
|
|
}
|