2008-09-29 03:18:23 +00:00
|
|
|
<?php
|
2017-08-09 11:55:09 +12:00
|
|
|
|
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
2021-02-26 16:13:23 +13:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2020-07-21 17:30:55 +12:00
|
|
|
use SilverStripe\UserForms\FormField\UserFormsOptionSetField;
|
2017-08-09 11:55:09 +12:00
|
|
|
use SilverStripe\UserForms\Model\EditableCustomRule;
|
|
|
|
|
2008-09-29 03:18:23 +00:00
|
|
|
/**
|
2009-04-17 02:26:40 +00:00
|
|
|
* EditableRadioField
|
|
|
|
*
|
2008-09-29 03:18:23 +00:00
|
|
|
* Represents a set of selectable radio buttons
|
2009-04-17 02:26:40 +00:00
|
|
|
*
|
|
|
|
* @package userforms
|
2008-09-29 03:18:23 +00:00
|
|
|
*/
|
2016-07-21 17:53:59 +12:00
|
|
|
class EditableRadioField extends EditableMultipleOptionField
|
|
|
|
{
|
|
|
|
private static $singular_name = 'Radio Group';
|
|
|
|
|
|
|
|
private static $plural_name = 'Radio Groups';
|
|
|
|
|
2017-08-11 11:33:06 +12:00
|
|
|
private static $table_name = 'EditableRadioField';
|
|
|
|
|
2016-07-21 17:53:59 +12:00
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2022-03-03 16:47:45 +13:00
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
|
|
|
$fields->removeByName('Default');
|
|
|
|
});
|
2016-07-21 17:53:59 +12:00
|
|
|
|
2022-03-03 16:47:45 +13:00
|
|
|
return parent::getCMSFields();
|
2016-07-21 17:53:59 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFormField()
|
|
|
|
{
|
2020-07-21 17:30:55 +12:00
|
|
|
$field = UserFormsOptionSetField::create($this->Name, $this->Title ?: false, $this->getOptionsMap())
|
2017-08-14 12:29:57 +12:00
|
|
|
->setFieldHolderTemplate(EditableMultipleOptionField::class . '_holder')
|
2020-07-21 17:30:55 +12:00
|
|
|
->setTemplate(UserFormsOptionSetField::class);
|
2016-07-21 17:53:59 +12:00
|
|
|
|
|
|
|
// Set default item
|
|
|
|
$defaultOption = $this->getDefaultOptions()->first();
|
|
|
|
if ($defaultOption) {
|
2017-06-13 09:49:02 +10:00
|
|
|
$field->setValue($defaultOption->Value);
|
2016-07-21 17:53:59 +12:00
|
|
|
}
|
|
|
|
$this->doUpdateFormField($field);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
|
|
|
|
{
|
|
|
|
// We only want to trigger on load once for the radio group - hence we focus on the first option only.
|
|
|
|
$first = $forOnLoad ? ':first' : '';
|
|
|
|
return "$(\"input[name='{$this->Name}']{$first}\")";
|
|
|
|
}
|
2017-04-28 10:22:15 +12:00
|
|
|
|
2017-08-11 12:37:03 +12:00
|
|
|
public function isRadioField()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-17 16:09:31 +12:00
|
|
|
}
|