summaryrefslogtreecommitdiffstats
path: root/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php
blob: 52e082de4a5d1688f1bcd1ff35e6c3b1c0531439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

/**
 * @file
 * Contains \Drupal\entity_reference\Plugin\field\widget\AutocompleteTagsWidget.
 */

namespace Drupal\entity_reference\Plugin\field\widget;

use Drupal\field\Annotation\FieldWidget;
use Drupal\Core\Annotation\Translation;
use Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase;

/**
 * Plugin implementation of the 'entity_reference autocomplete-tags' widget.
 *
 * @FieldWidget(
 *   id = "entity_reference_autocomplete_tags",
 *   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
 * )
 */
class AutocompleteTagsWidget extends AutocompleteWidgetBase {

  /**
   * {@inheritdoc}
   */
  public function elementValidate($element, &$form_state, $form) {
    $value = array();
    // If a value was entered into the autocomplete.
    $handler = entity_reference_get_selection_handler($this->fieldDefinition);
    $bundles = entity_get_bundles($this->getFieldSetting('target_type'));
    $auto_create = $this->getSelectionHandlerSetting('auto_create');

    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->getSelectionHandlerSetting('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);
  }
}