Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldWidget / EntityReferenceAutocompleteTagsWidget.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Plugin implementation of the 'entity_reference_autocomplete_tags' widget.
10  *
11  * @FieldWidget(
12  *   id = "entity_reference_autocomplete_tags",
13  *   label = @Translation("Autocomplete (Tags style)"),
14  *   description = @Translation("An autocomplete text field with tagging support."),
15  *   field_types = {
16  *     "entity_reference"
17  *   },
18  *   multiple_values = TRUE
19  * )
20  */
21 class EntityReferenceAutocompleteTagsWidget extends EntityReferenceAutocompleteWidget {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
27     $element = parent::formElement($items, $delta, $element, $form, $form_state);
28
29     $element['target_id']['#tags'] = TRUE;
30     $element['target_id']['#default_value'] = $items->referencedEntities();
31
32     return $element;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
39     return $values['target_id'];
40   }
41
42 }