From f8ee9b30237b3f035e7246d8304731d03eb8b8b1 Mon Sep 17 00:00:00 2001 From: Edouard Cunibil Date: Tue, 3 Dec 2019 18:06:06 +0100 Subject: [PATCH] Support modules that use field_layout data (eg. field_group). --- .../src/Plugin/Layout/PatternLayout.php | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/modules/ui_patterns_layouts/src/Plugin/Layout/PatternLayout.php b/modules/ui_patterns_layouts/src/Plugin/Layout/PatternLayout.php index 12ce3b14..2a477d04 100644 --- a/modules/ui_patterns_layouts/src/Plugin/Layout/PatternLayout.php +++ b/modules/ui_patterns_layouts/src/Plugin/Layout/PatternLayout.php @@ -8,6 +8,7 @@ use Drupal\Core\Layout\LayoutDefinition; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginFormInterface; +use Drupal\Core\Render\Element; use Drupal\ui_patterns\UiPatternsManager; use Drupal\Core\Render\ElementInfoManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -94,12 +95,22 @@ public function build(array $regions) { $fields[$region_name] = $regions[$region_name]; } - return [ + $build = [ '#type' => 'pattern', '#id' => $this->getPluginDefinition()->get('additional')['pattern'], '#fields' => $fields, '#variant' => $configuration['pattern']['variant'], ] + $this->elementInfo->getInfo('pattern'); + + // Add the fields at the root of the render array so modules that usually + // deal with layouts (eg. Field Group) can still do their jobs. + $build += $fields; + + // Prepend a new pre_render method that will copy back altered field arrays + // to the #fields variable. + array_unshift($build['#pre_render'], [get_class($this), 'preProcessFields']); + + return $build; } /** @@ -186,4 +197,22 @@ protected function processOnlyContentFields(array &$regions) { } } + /** + * Copy field content back where it belongs. + * + * @param array $element + * Render array. + * + * @return array + * Render array. + */ + public static function preProcessFields(array $element) { + foreach ($element['#fields'] as $field_name => $content) { + if (array_key_exists($field_name, $element)) { + $element['#fields'][$field_name] = $element[$field_name]; + } + } + return $element; + } + }