Skip to content

Commit f8ee9b3

Browse files
author
Edouard Cunibil
committed
Support modules that use field_layout data (eg. field_group).
1 parent edb9ae0 commit f8ee9b3

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

modules/ui_patterns_layouts/src/Plugin/Layout/PatternLayout.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drupal\Core\Layout\LayoutDefinition;
99
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
1010
use Drupal\Core\Plugin\PluginFormInterface;
11+
use Drupal\Core\Render\Element;
1112
use Drupal\ui_patterns\UiPatternsManager;
1213
use Drupal\Core\Render\ElementInfoManagerInterface;
1314
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -94,12 +95,22 @@ public function build(array $regions) {
9495
$fields[$region_name] = $regions[$region_name];
9596
}
9697

97-
return [
98+
$build = [
9899
'#type' => 'pattern',
99100
'#id' => $this->getPluginDefinition()->get('additional')['pattern'],
100101
'#fields' => $fields,
101102
'#variant' => $configuration['pattern']['variant'],
102103
] + $this->elementInfo->getInfo('pattern');
104+
105+
// Add the fields at the root of the render array so modules that usually
106+
// deal with layouts (eg. Field Group) can still do their jobs.
107+
$build += $fields;
108+
109+
// Prepend a new pre_render method that will copy back altered field arrays
110+
// to the #fields variable.
111+
array_unshift($build['#pre_render'], [get_class($this), 'preProcessFields']);
112+
113+
return $build;
103114
}
104115

105116
/**
@@ -186,4 +197,22 @@ protected function processOnlyContentFields(array &$regions) {
186197
}
187198
}
188199

200+
/**
201+
* Copy field content back where it belongs.
202+
*
203+
* @param array $element
204+
* Render array.
205+
*
206+
* @return array
207+
* Render array.
208+
*/
209+
public static function preProcessFields(array $element) {
210+
foreach ($element['#fields'] as $field_name => $content) {
211+
if (array_key_exists($field_name, $element)) {
212+
$element['#fields'][$field_name] = $element[$field_name];
213+
}
214+
}
215+
return $element;
216+
}
217+
189218
}

0 commit comments

Comments
 (0)