You are here

class AutocompleteTagsWidget in Entity reference 8

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

@todo: Check if the following statement is still correct The autocomplete path doesn't have a default here, because it's not the the two widgets, and the Field API doesn't update default settings when the widget changes.

Plugin annotation


@Plugin(
  id = "entityreference_autocomplete_tags",
  module = "entityreference",
  label = @Translation("Autocomplete (Tags style)"),
  description = @Translation("An autocomplete text field."),
  field_types = {
    "entityreference"
  },
  settings = {
    "match_operator" = "CONTAINS",
    "size" = 60,
    "path" = ""
  },
  multiple_values = FIELD_BEHAVIOR_CUSTOM
)

Hierarchy

Expanded class hierarchy of AutocompleteTagsWidget

File

lib/Drupal/entityreference/Plugin/field/widget/AutocompleteTagsWidget.php, line 40
Definition of Drupal\entityreference\Plugin\field\widget\AutocompleteTagsWidget.

Namespace

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

  /**
   * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    return $this
      ->prepareElement($items, $delta, $element, $langcode, $form, $form_state, 'entityreference/autocomplete/tags');
  }

  /**
   * Implements Drupal\entityreference\Plugin\field\widget\DefaultAutocompleteWidget::elementValidate()
   */
  public function elementValidate($element, &$form_state) {
    $value = array();

    // If a value was entered into the autocomplete.
    if (!empty($element['#value'])) {
      $entities = drupal_explode_tags($element['#value']);
      $value = array();
      foreach ($entities as $entity) {

        // Take "label (entity id)', match the id from parenthesis.
        if (preg_match("/.+\\((\\d+)\\)/", $entity, $matches)) {
          $value[] = array(
            'target_id' => $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.
          $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
          $handler = entityreference_get_selection_handler($field, $instance);
          $value[] = array(
            'target_id' => $handler
              ->validateAutocompleteInput($entity, $element, $form_state, $form),
          );
        }
      }
    }
    form_set_value($element, $value, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AutocompleteTagsWidget::elementValidate public function Implements Drupal\entityreference\Plugin\field\widget\DefaultAutocompleteWidget::elementValidate() Overrides AutocompleteWidgetBase::elementValidate
AutocompleteTagsWidget::formElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). Overrides AutocompleteWidgetBase::formElement
AutocompleteWidgetBase::errorElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement().
AutocompleteWidgetBase::getLabels protected function Get the entity labels.
AutocompleteWidgetBase::prepareElement protected function Prepapre the element.
AutocompleteWidgetBase::settingsForm public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm().