class LegacyWidget

Plugin implementation for legacy widgets.

This special implementation acts as a temporary BC layer for widgets that have not been converted to Plugins, and bridges new methods to the old-style hook_field_widget_*() callbacks.

This class is not discovered by the annotations reader, but referenced by the Drupal\field\Plugin\Discovery\LegacyDiscoveryDecorator.

@todo Remove once all core widgets have been converted.

Hierarchy

Expanded class hierarchy of LegacyWidget

File

drupal/core/modules/field/lib/Drupal/field/Plugin/field/widget/LegacyWidget.php, line 27
Definition of Drupal\field\Plugin\field\widget\LegacyWidget.

Namespace

Drupal\field\Plugin\field\widget
View source
class LegacyWidget extends WidgetBase {

  /**
   * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm().
   */
  public function settingsForm(array $form, array &$form_state) {
    $definition = $this
      ->getDefinition();
    $function = $definition['module'] . '_field_widget_settings_form';
    if (function_exists($function)) {
      return $function($this->field, $this->instance);
    }
    return array();
  }

  /**
   * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    $definition = $this
      ->getDefinition();
    $function = $definition['module'] . '_field_widget_form';
    if (function_exists($function)) {

      // hook_field_widget_form() implementations read widget properties directly
      // from $instance. Put the actual properties we use here, which might have
      // been altered by hook_field_widget_property().
      $instance = clone $this->instance;
      $instance['widget']['type'] = $this
        ->getPluginId();
      $instance['widget']['settings'] = $this
        ->getSettings();
      return $function($form, $form_state, $this->field, $instance, $langcode, $items, $delta, $element);
    }
    return array();
  }

  /**
   * Overrides Drupal\field\Plugin\Type\Widget\WidgetBase::flagErrors().
   *
   * In D7, hook_field_widget_error() was supposed to call form_error() itself,
   * whereas the new errorElement() method simply returns the element to flag.
   * So we override the flagError() method to be more similar to the previous
   * code in field_default_form_errors().
   */
  public function flagErrors(EntityInterface $entity, $langcode, array $items, array $form, array &$form_state) {
    $field_name = $this->field['field_name'];
    $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state);
    if (!empty($field_state['errors'])) {

      // Locate the correct element in the form.
      $element = drupal_array_get_nested_value($form_state['complete_form'], $field_state['array_parents']);

      // Only set errors if the element is accessible.
      if (!isset($element['#access']) || $element['#access']) {
        $definition = $this
          ->getDefinition();
        $is_multiple = $definition['multiple_values'];
        $function = $definition['module'] . '_field_widget_error';
        $function_exists = function_exists($function);
        foreach ($field_state['errors'] as $delta => $delta_errors) {

          // For multiple single-value widgets, pass errors by delta.
          // For a multiple-value widget, pass all errors to the main widget.
          $error_element = $is_multiple ? $element : $element[$delta];
          foreach ($delta_errors as $error) {
            if ($function_exists) {
              $function($error_element, $error, $form, $form_state);
            }
            else {

              // Make sure that errors are reported (even incorrectly flagged) if
              // the widget module fails to implement hook_field_widget_error().
              form_error($error_element, $error['message']);
            }
          }
        }

        // Reinitialize the errors list for the next submit.
        $field_state['errors'] = array();
        field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LegacyWidget::flagErrors public function Overrides Drupal\field\Plugin\Type\Widget\WidgetBase::flagErrors(). Overrides WidgetBase::flagErrors
LegacyWidget::formElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). Overrides WidgetInterface::formElement
LegacyWidget::settingsForm public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). Overrides WidgetBase::settingsForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$discovery protected property The discovery object.
PluginBase::$plugin_id protected property The plugin_id.
PluginBase::getDefinition public function Implements Drupal\Component\Plugin\PluginInterface::getDefinition(). Overrides PluginInspectionInterface::getDefinition
PluginBase::getPluginId public function Implements Drupal\Component\Plugin\PluginInterface::getPluginId(). Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
WidgetBase::$field protected property The field definition.
WidgetBase::$instance protected property The field instance definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::$weight protected property The widget weight.
WidgetBase::errorElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement(). Overrides WidgetInterface::errorElement 4
WidgetBase::form public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form(). Overrides WidgetBaseInterface::form
WidgetBase::formMultipleElements protected function Special handling to create form elements for multiple values. 1
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::massageFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() Overrides WidgetInterface::massageFormValues 1
WidgetBase::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.
WidgetBase::submit public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::submit(). Overrides WidgetBaseInterface::submit
WidgetBase::__construct public function Constructs a WidgetBase object. Overrides PluginBase::__construct