SelectWidget.php in Typed Data API enhancements 8
File
src/Plugin/TypedDataFormWidget/SelectWidget.php
View source
<?php
namespace Drupal\typed_data\Plugin\TypedDataFormWidget;
use Drupal\Core\Form\SubformStateInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\ListInterface;
use Drupal\Core\TypedData\OptionsProviderInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\typed_data\Context\ContextDefinition;
use Drupal\typed_data\Form\SubformState;
use Drupal\typed_data\Widget\FormWidgetBase;
use Symfony\Component\Validator\ConstraintViolationListInterface;
class SelectWidget extends FormWidgetBase {
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'label' => NULL,
'description' => NULL,
'empty_option' => NULL,
];
}
public function isApplicable(DataDefinitionInterface $definition) {
return is_subclass_of($definition
->getClass(), OptionsProviderInterface::class);
}
public function form(TypedDataInterface $data, SubformStateInterface $form_state) {
assert($data instanceof OptionsProviderInterface);
$form = SubformState::getNewSubForm();
$form['value'] = [
'#type' => 'select',
'#title' => $this->configuration['label'] ?: $data
->getDataDefinition()
->getLabel(),
'#description' => $this->configuration['description'] ?: $data
->getDataDefinition()
->getDescription(),
'#default_value' => $data
->getValue(),
'#multiple' => $data instanceof ListInterface,
'#empty_option' => $this->configuration['empty_option'],
'#empty_value' => '',
'#required' => $data
->getDataDefinition()
->isRequired(),
'#disabled' => $data
->getDataDefinition()
->isReadOnly(),
'#options' => $data
->getSettableOptions(),
];
return $form;
}
public function extractFormValues(TypedDataInterface $data, SubformStateInterface $form_state) {
$value = $form_state
->getValue('value');
if ($value === '') {
$value = NULL;
}
$data
->setValue($value);
}
public function flagViolations(TypedDataInterface $data, ConstraintViolationListInterface $violations, SubformStateInterface $formState) {
foreach ($violations as $violation) {
$formState
->setErrorByName('value', $violation
->getMessage());
}
}
public function getConfigurationDefinitions(DataDefinitionInterface $definition) {
return [
'label' => ContextDefinition::create('string')
->setLabel($this
->t('Label')),
'description' => ContextDefinition::create('string')
->setLabel($this
->t('Description')),
'empty_option' => ContextDefinition::create('string')
->setLabel($this
->t('Empty option label'))
->setDescription($this
->t('Allows overriding the label of the empty option')),
];
}
}