From e8ab0b268c144a66cb11dfcf5606f6e9a8411c92 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Mon, 30 Jun 2014 11:16:20 +0100
Subject: [PATCH] Issue #2293723 by yched: Generate lighter $form[$field]
 structures.

---
 core/lib/Drupal/Core/Field/WidgetBase.php        |  5 -----
 core/lib/Drupal/Core/Field/WidgetInterface.php   |  5 -----
 .../Plugin/Field/FieldWidget/CommentWidget.php   | 16 +++++++---------
 core/modules/field/src/Tests/FormTest.php        |  1 -
 .../src/Plugin/Field/FieldWidget/FileWidget.php  |  6 ++++--
 .../src/Plugin/Field/FieldWidget/LinkWidget.php  |  3 +--
 6 files changed, 12 insertions(+), 24 deletions(-)

diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php
index b366cd4e02a9..220d80d3f353 100644
--- a/core/lib/Drupal/Core/Field/WidgetBase.php
+++ b/core/lib/Drupal/Core/Field/WidgetBase.php
@@ -283,11 +283,6 @@ protected function formSingleElement(FieldItemListInterface $items, $delta, arra
     $entity = $items->getEntity();
 
     $element += array(
-      '#entity_type' => $entity->getEntityTypeId(),
-      '#bundle' => $entity->bundle(),
-      '#entity' => $entity,
-      '#field_name' => $this->fieldDefinition->getName(),
-      '#language' => $items->getLangcode(),
       '#field_parents' => $form['#parents'],
       // Only the first widget should be required.
       '#required' => $delta == 0 && $this->fieldDefinition->isRequired(),
diff --git a/core/lib/Drupal/Core/Field/WidgetInterface.php b/core/lib/Drupal/Core/Field/WidgetInterface.php
index 1edfe4513db6..443bc3fc2e66 100644
--- a/core/lib/Drupal/Core/Field/WidgetInterface.php
+++ b/core/lib/Drupal/Core/Field/WidgetInterface.php
@@ -79,11 +79,6 @@ public function settingsSummary();
    *   The order of this item in the array of subelements (0, 1, 2, etc).
    * @param array $element
    *   A form element array containing basic properties for the widget:
-   *   - #entity_type: The name of the entity the field is attached to.
-   *   - #bundle: The name of the field bundle the field is contained in.
-   *   - #entity: The entity the field is attached to.
-   *   - #field_name: The name of the field.
-   *   - #language: The language the field is being edited in.
    *   - #field_parents: The 'parents' space for the field in the form. Most
    *       widgets can simply overlook this property. This identifies the
    *       location where the field values are placed within
diff --git a/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php b/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php
index b4a856530b6a..4b8de0a99a2d 100644
--- a/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php
+++ b/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php
@@ -28,18 +28,13 @@ class CommentWidget extends WidgetBase {
    * {@inheritdoc}
    */
   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
-    $field = $this->fieldDefinition;
-    $entity = $items->getParent();
-
-    // Get default value from the field instance.
-    $field_default_values = $this->fieldDefinition->getDefaultValue($entity);
-    $status = $items->status;
+    $entity = $items->getEntity();
 
     $element['status'] = array(
       '#type' => 'radios',
       '#title' => t('Comments'),
       '#title_display' => 'invisible',
-      '#default_value' => $status,
+      '#default_value' => $items->status,
       '#options' => array(
         CommentItemInterface::OPEN => t('Open'),
         CommentItemInterface::CLOSED => t('Closed'),
@@ -58,7 +53,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
     // If the entity doesn't have any comments, the "hidden" option makes no
     // sense, so don't even bother presenting it to the user unless this is the
     // default value widget on the field settings form.
-    if ($element['#field_parents'] != array('default_value_input') && !$entity->get($field->getName())->comment_count) {
+    if ($element['#field_parents'] != array('default_value_input') && !$items->comment_count) {
       $element['status'][CommentItemInterface::HIDDEN]['#access'] = FALSE;
       // Also adjust the description of the "closed" option.
       $element['status'][CommentItemInterface::CLOSED]['#description'] = t('Users cannot post comments.');
@@ -67,6 +62,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
     // second column on wide-resolutions), place the field as a details element
     // in this tab-set.
     if (isset($form['advanced'])) {
+      // Get default value from the field instance.
+      $field_default_values = $this->fieldDefinition->getDefaultValue($entity);
+
       $element += array(
         '#type' => 'details',
         // Open the details when the selected value is different to the stored
@@ -74,7 +72,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
         '#open' => ($items->status != $field_default_values[0]['status']),
         '#group' => 'advanced',
         '#attributes' => array(
-          'class' => array('comment-' . drupal_html_class($element['#entity_type']) . '-settings-form'),
+          'class' => array('comment-' . drupal_html_class($entity->getEntityTypeId()) . '-settings-form'),
         ),
         '#attached' => array(
           'library' => array('comment/drupal.comment'),
diff --git a/core/modules/field/src/Tests/FormTest.php b/core/modules/field/src/Tests/FormTest.php
index 819e3891f6d5..255c19dedb76 100644
--- a/core/modules/field/src/Tests/FormTest.php
+++ b/core/modules/field/src/Tests/FormTest.php
@@ -540,7 +540,6 @@ function testFieldFormAccess() {
     $form_state = form_state_defaults();
     $display->buildForm($entity, $form, $form_state);
 
-    $this->assertEqual($form[$field_name_no_access]['widget'][0]['value']['#entity_type'], $entity_type, 'The correct entity type is set in the field structure.');
     $this->assertFalse($form[$field_name_no_access]['#access'], 'Field #access is FALSE for the field without edit access.');
 
     // Display creation form.
diff --git a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
index b9b900ff2e07..9b1dc7ef25d3 100644
--- a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
+++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
@@ -159,8 +159,8 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f
       $elements['#title'] = $title;
 
       $elements['#description'] = $description;
-      $elements['#field_name'] = $element['#field_name'];
-      $elements['#language'] = $element['#language'];
+      $elements['#field_name'] = $field_name;
+      $elements['#language'] = $items->getLangcode();
       $elements['#display_field'] = (bool) $this->getFieldSetting('display_field');
       // The field settings include defaults for the field type. However, this
       // widget is a base class for other widgets (e.g., ImageWidget) that may
@@ -218,6 +218,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
       // Allows this field to return an array instead of a single value.
       '#extended' => TRUE,
       // Add properties needed by value() and process() methods.
+      '#field_name' => $this->fieldDefinition->getName(),
+      '#entity_type' => $items->getEntity()->getEntityTypeId(),
       '#display_field' => (bool) $field_settings['display_field'],
       '#display_default' => $field_settings['display_default'],
       '#description_field' => $field_settings['description_field'],
diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
index 4541ba55d13c..27072ee2cd8e 100644
--- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
+++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
@@ -87,8 +87,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
     // Post-process the title field to make it conditionally required if URL is
     // non-empty. Omit the validation on the field edit form, since the field
     // settings cannot be saved otherwise.
-    $is_field_edit_form = ($element['#entity'] === NULL);
-    if (!$is_field_edit_form && $this->getFieldSetting('title') == DRUPAL_REQUIRED) {
+    if (empty($form_state['default_value_widget']) && $this->getFieldSetting('title') == DRUPAL_REQUIRED) {
       $element['#element_validate'][] = array($this, 'validateTitle');
     }
 
-- 
GitLab