From 7c219bbfc1edfbe95c9ebe4ddb9ae76a119143f0 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Wed, 16 Oct 2013 12:21:17 +0100
Subject: [PATCH] Issue #2060705 by yched, Hydra, swentel, amateescu: Remove
 the 'hidden widget' plugin, it is actually never used.

---
 core/modules/field/field.module               |  8 -----
 .../Plugin/field/widget/HiddenWidget.php      | 32 -------------------
 .../field/lib/Drupal/field/Tests/FormTest.php | 19 ++++-------
 .../Drupal/field_ui/FormDisplayOverview.php   |  7 ++++
 4 files changed, 14 insertions(+), 52 deletions(-)
 delete mode 100644 core/modules/field/lib/Drupal/field/Plugin/field/widget/HiddenWidget.php

diff --git a/core/modules/field/field.module b/core/modules/field/field.module
index 22d7592ff1ac..762159537231 100644
--- a/core/modules/field/field.module
+++ b/core/modules/field/field.module
@@ -243,14 +243,6 @@ function _field_generate_entity_field_definition(FieldInterface $field, FieldIns
   return $definition;
 }
 
-/**
- * Implements hook_field_widget_info_alter().
- */
-function field_field_widget_info_alter(&$info) {
-  // Add the Hidden widget to all field types.
-  $info['hidden']['field_types'] = array_keys(\Drupal::service('plugin.manager.entity.field.field_type')->getDefinitions());
-}
-
 /**
  * Implements hook_entity_bundle_create().
  */
diff --git a/core/modules/field/lib/Drupal/field/Plugin/field/widget/HiddenWidget.php b/core/modules/field/lib/Drupal/field/Plugin/field/widget/HiddenWidget.php
deleted file mode 100644
index 08b8d16b72bb..000000000000
--- a/core/modules/field/lib/Drupal/field/Plugin/field/widget/HiddenWidget.php
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\field\Plugin\field\widget\HiddenWidget.
- */
-
-namespace Drupal\field\Plugin\field\widget;
-
-use Drupal\Core\Entity\Field\FieldItemListInterface;
-use Drupal\field\Plugin\Type\Widget\WidgetBase;
-
-/**
- * Plugin implementation of the 'Hidden' widget.
- *
- * @FieldWidget(
- *   id = "hidden",
- *   label = @Translation("- Hidden -"),
- *   multiple_values = TRUE,
- *   weight = 50
- * )
- */
-class HiddenWidget extends WidgetBase {
-
-  /**
-   * {@inheritdoc}
-   */
-  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
-    // The purpose of this widget is to be hidden, so nothing to do here.
-    return array();
-  }
-}
diff --git a/core/modules/field/lib/Drupal/field/Tests/FormTest.php b/core/modules/field/lib/Drupal/field/Tests/FormTest.php
index f9b37d55cb6e..6fe20eb6b14a 100644
--- a/core/modules/field/lib/Drupal/field/Tests/FormTest.php
+++ b/core/modules/field/lib/Drupal/field/Tests/FormTest.php
@@ -578,9 +578,9 @@ function testFieldFormAccess() {
   }
 
   /**
-   * Tests the Hidden widget.
+   * Tests hiding a field in a form.
    */
-  function testFieldFormHiddenWidget() {
+  function testHiddenField() {
     $entity_type = 'entity_test_rev';
     $field = $this->field_single;
     $field['entity_type'] = $entity_type;
@@ -592,18 +592,15 @@ function testFieldFormHiddenWidget() {
     entity_create('field_entity', $field)->save();
     $this->instance = entity_create('field_instance', $this->instance);
     $this->instance->save();
-    entity_get_form_display($this->instance->entity_type, $this->instance->bundle, 'default')
-      ->setComponent($this->instance->getFieldName(), array(
-        'type' => 'hidden',
-      ))
-      ->save();
+    // We explicitly do not assign a widget in a form display, so the field
+    // stays hidden in forms.
 
     // Display the entity creation form.
     $this->drupalGet($entity_type . '/add');
 
     // Create an entity and test that the default value is assigned correctly to
     // the field that uses the hidden widget.
-    $this->assertNoField("{$field_name}[0][value]", 'The hidden widget is not displayed');
+    $this->assertNoField("{$field_name}[0][value]", 'The field does not appear in the form');
     $this->drupalPostForm(NULL, array('user_id' => 1, 'name' => $this->randomName()), t('Save'));
     preg_match('|' . $entity_type . '/manage/(\d+)|', $this->url, $match);
     $id = $match[1];
@@ -634,11 +631,9 @@ function testFieldFormHiddenWidget() {
     $entity = entity_load($entity_type, $id);
     $this->assertEqual($entity->{$field_name}->value, $value, 'Field value was updated');
 
-    // Update the form display and switch to the Hidden widget again.
+    // Set the field back to hidden.
     entity_get_form_display($entity_type, $this->instance->bundle, 'default')
-      ->setComponent($this->instance->getFieldName(), array(
-        'type' => 'hidden',
-      ))
+      ->removeComponent($this->instance->getFieldName())
       ->save();
 
     // Create a new revision.
diff --git a/core/modules/field_ui/lib/Drupal/field_ui/FormDisplayOverview.php b/core/modules/field_ui/lib/Drupal/field_ui/FormDisplayOverview.php
index a60b3290687f..e1df25a79974 100644
--- a/core/modules/field_ui/lib/Drupal/field_ui/FormDisplayOverview.php
+++ b/core/modules/field_ui/lib/Drupal/field_ui/FormDisplayOverview.php
@@ -82,6 +82,13 @@ protected function getPlugin($instance, $configuration) {
     return $plugin;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function getPluginOptions($field_type) {
+    return parent::getPluginOptions($field_type) + array('hidden' => '- ' . t('Hidden') . ' -');
+  }
+
   /**
    * {@inheritdoc}
    */
-- 
GitLab