Skip to content

Commit

Permalink
Support modules that use field_layout data (eg. field_group).
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard Cunibil committed Dec 3, 2019
1 parent edb9ae0 commit f8ee9b3
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion modules/ui_patterns_layouts/src/Plugin/Layout/PatternLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

}

0 comments on commit f8ee9b3

Please sign in to comment.