class TaxonomyAutocompleteWidget

Plugin implementation of the 'taxonomy_autocomplete' widget.

Plugin annotation


@Plugin(
  id = "taxonomy_autocomplete",
  module = "taxonomy",
  label = @Translation("Autocomplete term widget (tagging)"),
  field_types = {
    "taxonomy_term_reference"
  },
  settings = {
    "size" = "60",
    "autocomplete_path" = "taxonomy/autocomplete",
    "placeholder" = ""
  },
  multiple_values = TRUE
)

Hierarchy

Expanded class hierarchy of TaxonomyAutocompleteWidget

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php, line 32
Definition of Drupal\taxonomy\Plugin\field\widget\TaxonomyAutocompleteWidget.

Namespace

Drupal\taxonomy\Plugin\field\widget
View source
class TaxonomyAutocompleteWidget extends WidgetBase {

  /**
   * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm().
   */
  public function settingsForm(array $form, array &$form_state) {
    $element['placeholder'] = array(
      '#type' => 'textfield',
      '#title' => t('Placeholder'),
      '#default_value' => $this
        ->getSetting('placeholder'),
      '#description' => t('The placeholder is a short hint (a word or short phrase) intended to aid the user with data entry. A hint could be a sample value or a brief description of the expected format.'),
    );
    return $element;
  }

  /**
   * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    $field = $this->field;
    $tags = array();
    foreach ($items as $item) {
      $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
    }
    $element += array(
      '#type' => 'textfield',
      '#default_value' => taxonomy_implode_tags($tags),
      '#autocomplete_path' => $this
        ->getSetting('autocomplete_path') . '/' . $field['field_name'],
      '#size' => $this
        ->getSetting('size'),
      '#placeholder' => $this
        ->getSetting('placeholder'),
      '#maxlength' => 1024,
      '#element_validate' => array(
        'taxonomy_autocomplete_validate',
      ),
    );
    return $element;
  }

  /**
   * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues()
   */
  public function massageFormValues(array $values, array $form, array &$form_state) {

    // Autocomplete widgets do not send their tids in the form, so we must detect
    // them here and process them independently.
    $terms = array();
    $field = $this->field;

    // Collect candidate vocabularies.
    foreach ($field['settings']['allowed_values'] as $tree) {
      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
        $vocabularies[$vocabulary->vid] = $vocabulary;
      }
    }

    // Translate term names into actual terms.
    foreach ($values as $value) {

      // See if the term exists in the chosen vocabulary and return the tid;
      // otherwise, create a new 'autocreate' term for insert/update.
      if ($possibilities = entity_load_multiple_by_properties('taxonomy_term', array(
        'name' => trim($value),
        'vid' => array_keys($vocabularies),
      ))) {
        $term = array_pop($possibilities);
      }
      else {
        $vocabulary = reset($vocabularies);
        $term = array(
          'tid' => 'autocreate',
          'vid' => $vocabulary->vid,
          'name' => $value,
          'vocabulary_machine_name' => $vocabulary->machine_name,
        );
      }
      $terms[] = (array) $term;
    }
    return $terms;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
TaxonomyAutocompleteWidget::formElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). Overrides WidgetInterface::formElement
TaxonomyAutocompleteWidget::massageFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() Overrides WidgetBase::massageFormValues
TaxonomyAutocompleteWidget::settingsForm public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). Overrides WidgetBase::settingsForm
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::flagErrors public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). Overrides WidgetBaseInterface::flagErrors 1
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::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