Commit 8b86801f authored by xjm's avatar xjm
Browse files

Issue #2920394 by tim.plunkett: Field Layout should not alter fields placed...

Issue #2920394 by tim.plunkett: Field Layout should not alter fields placed into regions it doesn't know about
parent d0e37969
...@@ -67,12 +67,15 @@ public function buildView(array &$build, EntityDisplayWithLayoutInterface $displ ...@@ -67,12 +67,15 @@ public function buildView(array &$build, EntityDisplayWithLayoutInterface $displ
$regions = array_fill_keys($layout_definition->getRegionNames(), []); $regions = array_fill_keys($layout_definition->getRegionNames(), []);
foreach ($fields as $name => $field) { foreach ($fields as $name => $field) {
// Move the field from the top-level of $build into a region-specific // If the region is controlled by the layout, move the field from the
// section. // top-level of $build into a region-specific section. Custom regions
// could be set by other code at run-time; these should be ignored.
// @todo Ideally the array structure would remain unchanged, see // @todo Ideally the array structure would remain unchanged, see
// https://www.drupal.org/node/2846393. // https://www.drupal.org/node/2846393.
$regions[$field['region']][$name] = $build[$name]; if (isset($regions[$field['region']])) {
unset($build[$name]); $regions[$field['region']][$name] = $build[$name];
unset($build[$name]);
}
} }
// Ensure this will not conflict with any existing array elements by // Ensure this will not conflict with any existing array elements by
// prefixing with an underscore. // prefixing with an underscore.
...@@ -103,7 +106,7 @@ public function buildForm(array &$build, EntityDisplayWithLayoutInterface $displ ...@@ -103,7 +106,7 @@ public function buildForm(array &$build, EntityDisplayWithLayoutInterface $displ
// avoids breaking hook_form_alter() implementations by not actually // avoids breaking hook_form_alter() implementations by not actually
// moving the field in the form structure. If a #group is already set, // moving the field in the form structure. If a #group is already set,
// do not overwrite it. // do not overwrite it.
if (!isset($build[$name]['#group'])) { if (isset($regions[$field['region']]) && !isset($build[$name]['#group'])) {
$build[$name]['#group'] = $field['region']; $build[$name]['#group'] = $field['region'];
} }
} }
......
...@@ -93,6 +93,9 @@ public function testBuildView() { ...@@ -93,6 +93,9 @@ public function testBuildView() {
'test1' => [ 'test1' => [
'#markup' => 'Test1', '#markup' => 'Test1',
], ],
'test2' => [
'#markup' => 'Test2',
],
'non_configurable_field' => [ 'non_configurable_field' => [
'#markup' => 'Non-configurable', '#markup' => 'Non-configurable',
], ],
...@@ -111,6 +114,9 @@ public function testBuildView() { ...@@ -111,6 +114,9 @@ public function testBuildView() {
'test1' => [ 'test1' => [
'region' => 'right', 'region' => 'right',
], ],
'test2' => [
'region' => 'unknown_region',
],
'non_configurable_field' => [ 'non_configurable_field' => [
'region' => 'left', 'region' => 'left',
], ],
...@@ -120,6 +126,9 @@ public function testBuildView() { ...@@ -120,6 +126,9 @@ public function testBuildView() {
]); ]);
$expected = [ $expected = [
'test2' => [
'#markup' => 'Test2',
],
'non_configurable_field' => [ 'non_configurable_field' => [
'#markup' => 'Non-configurable', '#markup' => 'Non-configurable',
], ],
...@@ -169,6 +178,9 @@ public function testBuildForm() { ...@@ -169,6 +178,9 @@ public function testBuildForm() {
'#markup' => 'Test2', '#markup' => 'Test2',
'#group' => 'existing_group', '#group' => 'existing_group',
], ],
'test3' => [
'#markup' => 'Test3',
],
'field_layout' => [ 'field_layout' => [
'#markup' => 'Field created through the UI happens to be named "Layout"', '#markup' => 'Field created through the UI happens to be named "Layout"',
], ],
...@@ -190,6 +202,9 @@ public function testBuildForm() { ...@@ -190,6 +202,9 @@ public function testBuildForm() {
'test2' => [ 'test2' => [
'region' => 'left', 'region' => 'left',
], ],
'test3' => [
'region' => 'unknown_region',
],
'field_layout' => [ 'field_layout' => [
'region' => 'right', 'region' => 'right',
], ],
...@@ -207,6 +222,9 @@ public function testBuildForm() { ...@@ -207,6 +222,9 @@ public function testBuildForm() {
'#markup' => 'Test2', '#markup' => 'Test2',
'#group' => 'existing_group', '#group' => 'existing_group',
], ],
'test3' => [
'#markup' => 'Test3',
],
'field_layout' => [ 'field_layout' => [
'#markup' => 'Field created through the UI happens to be named "Layout"', '#markup' => 'Field created through the UI happens to be named "Layout"',
'#group' => 'right', '#group' => 'right',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment