You are here

class SelectWidget in Typed Data API enhancements 8

Plugin implementation of the 'select' widget.

Plugin annotation


@TypedDataFormWidget(
  id = "select",
  label = @Translation("Select"),
  description = @Translation("A simple select box."),
)

Hierarchy

Expanded class hierarchy of SelectWidget

File

src/Plugin/TypedDataFormWidget/SelectWidget.php, line 24

Namespace

Drupal\typed_data\Plugin\TypedDataFormWidget
View source
class SelectWidget extends FormWidgetBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'label' => NULL,
      'description' => NULL,
      'empty_option' => NULL,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function isApplicable(DataDefinitionInterface $definition) {
    return is_subclass_of($definition
      ->getClass(), OptionsProviderInterface::class);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function extractFormValues(TypedDataInterface $data, SubformStateInterface $form_state) {

    // Ensure empty values correctly end up as NULL value.
    $value = $form_state
      ->getValue('value');
    if ($value === '') {
      $value = NULL;
    }
    $data
      ->setValue($value);
  }

  /**
   * {@inheritdoc}
   */
  public function flagViolations(TypedDataInterface $data, ConstraintViolationListInterface $violations, SubformStateInterface $formState) {
    foreach ($violations as $violation) {

      /** @var ConstraintViolationInterface $violation */
      $formState
        ->setErrorByName('value', $violation
        ->getMessage());
    }
  }

  /**
   * {@inheritdoc}
   */
  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')),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormWidgetBase::$typedDataManager protected property The typed data plugin manager.
FormWidgetBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FormWidgetBase::createDefaultDateTime public function Create a default DrupalDateTime object.
FormWidgetBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
FormWidgetBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
FormWidgetBase::__construct public function Constructs a FormWidgetBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SelectWidget::defaultConfiguration public function Gets default configuration for this plugin. Overrides FormWidgetBase::defaultConfiguration
SelectWidget::extractFormValues public function Extracts the data value from submitted form values. Overrides FormWidgetInterface::extractFormValues
SelectWidget::flagViolations public function Reports validation errors against actual form elements. Overrides FormWidgetInterface::flagViolations
SelectWidget::form public function Creates the widget's form elements for editing the given data. Overrides FormWidgetInterface::form
SelectWidget::getConfigurationDefinitions public function Defines the supported configuration settings. Overrides FormWidgetInterface::getConfigurationDefinitions
SelectWidget::isApplicable public function Returns if the widget can be used for the provided data. Overrides FormWidgetInterface::isApplicable
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.