Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support modules that use field_layout data (eg. field_group). #283

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions 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\Security\TrustedCallbackInterface;
use Drupal\ui_patterns\UiPatternsManager;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -17,7 +18,7 @@
*
* @package Drupal\ui_patterns_layouts\Plugin\Layout
*/
class PatternLayout extends LayoutDefault implements PluginFormInterface, ContainerFactoryPluginInterface {
class PatternLayout extends LayoutDefault implements PluginFormInterface, ContainerFactoryPluginInterface, TrustedCallbackInterface {

/**
* Module Handler.
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,29 @@ 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;
}

/**
* @return string[]|void
*/
public static function trustedCallbacks() {
return ['preProcessFields'];
}

}