TaxonomyAutocompleteWidget.php 3.45 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\taxonomy\Plugin\field\widget\TaxonomyAutocompleteWidget.
 */

namespace Drupal\taxonomy\Plugin\field\widget;

10
use Drupal\Component\Annotation\Plugin;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Widget\WidgetBase;

/**
 * Plugin implementation of the 'taxonomy_autocomplete' widget.
 *
 * @Plugin(
 *   id = "taxonomy_autocomplete",
 *   module = "taxonomy",
 *   label = @Translation("Autocomplete term widget (tagging)"),
 *   field_types = {
 *     "taxonomy_term_reference"
 *   },
 *   settings = {
 *     "size" = "60",
26 27
 *     "autocomplete_path" = "taxonomy/autocomplete",
 *     "placeholder" = ""
28 29 30 31 32 33
 *   },
 *   multiple_values = TRUE
 * )
 */
class TaxonomyAutocompleteWidget extends WidgetBase {

34 35 36 37 38 39 40 41
  /**
   * 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'),
42
      '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
43 44 45 46
    );
    return $element;
  }

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  /**
   * 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'),
62
      '#placeholder' => $this->getSetting('placeholder'),
63 64 65 66 67 68 69 70 71 72 73 74 75
      '#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.
76
    $items = array();
77 78 79 80
    $field = $this->field;

    // Collect candidate vocabularies.
    foreach ($field['settings']['allowed_values'] as $tree) {
81 82
      if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) {
        $vocabularies[$vocabulary->id()] = $vocabulary;
83 84 85 86 87 88
      }
    }

    // Translate term names into actual terms.
    foreach($values as $value) {
      // See if the term exists in the chosen vocabulary and return the tid;
89
      // otherwise, create a new term.
90 91
      if ($possibilities = entity_load_multiple_by_properties('taxonomy_term', array('name' => trim($value), 'vid' => array_keys($vocabularies)))) {
        $term = array_pop($possibilities);
92
        $item = array('tid' => $term->id());
93 94 95
      }
      else {
        $vocabulary = reset($vocabularies);
96
        $term = entity_create('taxonomy_term', array(
97
          'vid' => $vocabulary->id(),
98
          'name' => $value,
99
        ));
100
        $item = array('tid' => 0, 'entity' => $term);
101
      }
102
      $items[] = $item;
103 104
    }

105
    return $items;
106 107 108
  }

}