From 77cc37d87624f89378a89a7cbdb5fd0434b6ee2f Mon Sep 17 00:00:00 2001
From: Andreas Hennings <andreas@dqxtech.net>
Date: Tue, 19 Jul 2016 10:48:42 +0200
Subject: Performance of #parents in FormBuilder.

---
 core/lib/Drupal/Core/Form/FormBuilder.php | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php
index 544ee94..4813582 100644
--- a/core/lib/Drupal/Core/Form/FormBuilder.php
+++ b/core/lib/Drupal/Core/Form/FormBuilder.php
@@ -993,6 +993,8 @@ public function doBuildForm($form_id, &$element, FormStateInterface &$form_state
         $inherited_access = $access;
       }
     }
+    $parents = $element['#parents'];
+    $i_parents_insert = count($parents);
     foreach (Element::children($element) as $key) {
       // Prior to checking properties of child elements, their default
       // properties need to be loaded.
@@ -1023,7 +1025,13 @@ public function doBuildForm($form_id, &$element, FormStateInterface &$form_state
       if (!isset($element[$key]['#parents'])) {
         // Check to see if a tree of child elements is present. If so,
         // continue down the tree if required.
-        $element[$key]['#parents'] = $element[$key]['#tree'] && $element['#tree'] ? array_merge($element['#parents'], array($key)) : array($key);
+        if ($element[$key]['#tree'] && $element['#tree']) {
+          $parents[$i_parents_insert] = $key;
+          $element[$key]['#parents'] = $parents;
+        }
+        else {
+          $element[$key]['#parents'] = array($key);
+        }
       }
       // Ensure #array_parents follows the actual form structure.
       $array_parents = $element['#array_parents'];
-- 
1.9.1


From c0dd4b99b56d62d9cf7d297434d1b43386bfb945 Mon Sep 17 00:00:00 2001
From: Andreas Hennings <andreas@dqxtech.net>
Date: Wed, 20 Jul 2016 03:34:21 +0200
Subject: Fix FormTestFormStateValuesCleanForm::cleanValue(): Return the
 element.

---
 .../modules/form_test/src/Form/FormTestFormStateValuesCleanForm.php      | 1 +
 1 file changed, 1 insertion(+)

diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanForm.php
index bef5675..ff8a921 100644
--- a/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanForm.php
+++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanForm.php
@@ -49,6 +49,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
    */
   public static function cleanValue(&$element, FormStateInterface $form_state, &$complete_form) {
     $form_state->addCleanValueKey('wine');
+    return $element;
   }
 
   /**
-- 
1.9.1

