From 805052467875cbb3b77daada868ea8c94c6f7d0e Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Fri, 5 Oct 2012 21:42:55 -0700
Subject: [PATCH] Issue #1751244 by pcambra, yched: Convert taxonomy module
 widgets to Plugin system.

---
 .../widget/TaxonomyAutocompleteWidget.php     | 93 +++++++++++++++++++
 core/modules/taxonomy/taxonomy.module         | 82 +---------------
 2 files changed, 97 insertions(+), 78 deletions(-)
 create mode 100644 core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php

diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php
new file mode 100644
index 000000000000..3f4d5109edd8
--- /dev/null
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php
@@ -0,0 +1,93 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\taxonomy\Plugin\field\widget\TaxonomyAutocompleteWidget.
+ */
+
+namespace Drupal\taxonomy\Plugin\field\widget;
+
+use Drupal\Core\Annotation\Plugin;
+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",
+ *     "autocomplete_path" = "taxonomy/autocomplete"
+ *   },
+ *   multiple_values = TRUE
+ * )
+ */
+class TaxonomyAutocompleteWidget extends WidgetBase {
+
+  /**
+   * 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'),
+      '#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;
+  }
+
+}
diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module
index c28cc7b9a1e1..faa04dde89e3 100644
--- a/core/modules/taxonomy/taxonomy.module
+++ b/core/modules/taxonomy/taxonomy.module
@@ -1174,25 +1174,6 @@ function taxonomy_field_info() {
   );
 }
 
-/**
- * Implements hook_field_widget_info().
- */
-function taxonomy_field_widget_info() {
-  return array(
-    'taxonomy_autocomplete' => array(
-      'label' => t('Autocomplete term widget (tagging)'),
-      'field types' => array('taxonomy_term_reference'),
-      'settings' => array(
-        'size' => 60,
-        'autocomplete_path' => 'taxonomy/autocomplete',
-      ),
-      'behaviors' => array(
-        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
-      ),
-    ),
-  );
-}
-
 /**
  * Implements hook_field_widget_info_alter().
  */
@@ -1449,74 +1430,19 @@ function taxonomy_term_title(Term $term) {
   return $term->label();
 }
 
-/**
- * Implements hook_field_widget_form().
- */
-function taxonomy_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
-  $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' => $instance['widget']['settings']['autocomplete_path'] . '/' . $field['field_name'],
-    '#size' => $instance['widget']['settings']['size'],
-    '#maxlength' => 1024,
-    '#element_validate' => array('taxonomy_autocomplete_validate'),
-  );
-
-  return $element;
-}
-
 /**
  * Form element validate handler for taxonomy term autocomplete element.
  */
 function taxonomy_autocomplete_validate($element, &$form_state) {
-  // Autocomplete widgets do not send their tids in the form, so we must detect
-  // them here and process them independently.
-  $value = array();
+  // Split the values into an array.
+  // @see Drupal\taxonomy\Plugin\field\widget\TaxonomyAutocompleteWidget:massageFormValues()
+  $typed_terms = array();
   if ($tags = $element['#value']) {
-    // Collect candidate vocabularies.
-    $field = field_widget_field($element, $form_state);
-    $vocabularies = array();
-    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.
     $typed_terms = drupal_explode_tags($tags);
-    foreach ($typed_terms as $typed_term) {
-      // 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($typed_term), 'vid' => array_keys($vocabularies)))) {
-        $term = array_pop($possibilities);
-      }
-      else {
-        $vocabulary = reset($vocabularies);
-        $term = array(
-          'tid' => 'autocreate',
-          'vid' => $vocabulary->vid,
-          'name' => $typed_term,
-          'vocabulary_machine_name' => $vocabulary->machine_name,
-        );
-      }
-      $value[] = (array)$term;
-    }
   }
-
-  form_set_value($element, $value, $form_state);
+  form_set_value($element, $typed_terms, $form_state);
 }
 
-/**
- * Implements hook_field_widget_error().
- */
-function taxonomy_field_widget_error($element, $error, $form, &$form_state) {
-  form_error($element, $error['message']);
-}
 /**
  * Implements hook_field_settings_form().
  */
-- 
GitLab