class AutocompleteTagsWidget

Plugin implementation of the 'entity_reference autocomplete-tags' widget.

Plugin annotation


@Plugin(
  id = "entity_reference_autocomplete_tags",
  module = "entity_reference",
  label = @Translation("Autocomplete (Tags style)"),
  description = @Translation("An autocomplete text field."),
  field_types = {
    "entity_reference"
  },
  settings = {
    "match_operator" = "CONTAINS",
    "size" = 60,
    "autocomplete_path" = "entity_reference/autocomplete/tags",
    "placeholder" = ""
  },
  multiple_values = TRUE
)

Hierarchy

Expanded class hierarchy of AutocompleteTagsWidget

File

drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php, line 34
Contains \Drupal\entity_reference\Plugin\field\widget\AutocompleteTagsWidget.

Namespace

Drupal\entity_reference\Plugin\field\widget
View source
class AutocompleteTagsWidget extends AutocompleteWidgetBase {

  /**
   * Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::elementValidate()
   */
  public function elementValidate($element, &$form_state, $form) {
    $value = array();

    // If a value was entered into the autocomplete.
    $handler = entity_reference_get_selection_handler($this->field, $this->instance);
    $bundles = entity_get_bundles($this->field['settings']['target_type']);
    $auto_create = isset($this->instance['settings']['handler_settings']['auto_create']) ? $this->instance['settings']['handler_settings']['auto_create'] : FALSE;
    if (!empty($element['#value'])) {
      $value = array();
      foreach (drupal_explode_tags($element['#value']) as $input) {
        $match = FALSE;

        // Take "label (entity id)', match the id from parenthesis.
        if (preg_match("/.+\\((\\d+)\\)/", $input, $matches)) {
          $match = $matches[1];
        }
        else {

          // Try to get a match from the input string when the user didn't use
          // the autocomplete but filled in a value manually.
          $match = $handler
            ->validateAutocompleteInput($input, $element, $form_state, $form, !$auto_create);
        }
        if ($match) {
          $value[] = array(
            'target_id' => $match,
          );
        }
        elseif ($auto_create && (count($this->instance['settings']['handler_settings']['target_bundles']) == 1 || count($bundles) == 1)) {

          // Auto-create item. see entity_reference_field_presave().
          $value[] = array(
            'target_id' => 0,
            'entity' => $this
              ->createNewEntity($input, $element['#autocreate_uid']),
          );
        }
      }
    }

    // Change the element['#parents'], so in form_set_value() we
    // populate the correct key.
    array_pop($element['#parents']);
    form_set_value($element, $value, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AutocompleteTagsWidget::elementValidate public function Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::elementValidate() Overrides AutocompleteWidgetBase::elementValidate
AutocompleteWidgetBase::createNewEntity protected function Creates a new entity from a label entered in the autocomplete input.
AutocompleteWidgetBase::errorElement public function Overrides \Drupal\field\Plugin\Type\Widget\WidgetBase::errorElement(). Overrides WidgetBase::errorElement
AutocompleteWidgetBase::formElement public function Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). Overrides WidgetInterface::formElement 1
AutocompleteWidgetBase::getLabels protected function Gets the entity labels.
AutocompleteWidgetBase::settingsForm public function Overrides \Drupal\field\Plugin\Type\Widget\WidgetBase::settingsForm(). Overrides WidgetBase::settingsForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. 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::extractFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues(). Overrides WidgetBaseInterface::extractFormValues
WidgetBase::flagErrors public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). Overrides WidgetBaseInterface::flagErrors
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 2
WidgetBase::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.
WidgetBase::__construct public function Constructs a WidgetBase object. Overrides PluginBase::__construct 3