diff --git a/core/modules/layout_builder/layout_builder.info.yml b/core/modules/layout_builder/layout_builder.info.yml new file mode 100644 index 0000000000..50a51d0d2e --- /dev/null +++ b/core/modules/layout_builder/layout_builder.info.yml @@ -0,0 +1,8 @@ +name: 'Layout Builder' +type: module +description: 'Provides layout building utility.' +package: Core (Experimental) +version: VERSION +core: 8.x +dependencies: + - layout_discovery diff --git a/core/modules/layout_builder/src/Plugin/Field/FieldFormatter/LayoutSectionFormatter.php b/core/modules/layout_builder/src/Plugin/Field/FieldFormatter/LayoutSectionFormatter.php new file mode 100644 index 0000000000..6ef923da13 --- /dev/null +++ b/core/modules/layout_builder/src/Plugin/Field/FieldFormatter/LayoutSectionFormatter.php @@ -0,0 +1,126 @@ +account = $account; + $this->layoutPluginManager = $layoutPluginManager; + $this->blockManager = $blockManager; + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static($container->get('current_user'), $container->get('plugin.manager.core.layout'), $container->get('plugin.manager.block'), $plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings']); + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + $elements = []; + + foreach ($items as $delta => $item) { + $elements[$delta] = $this->viewValue($item); + } + + return $elements; + } + + /** + * Build the render array for the field item. + * + * @param FieldItemInterface $item + * The field item. + * + * @return array + */ + protected function viewValue(FieldItemInterface $item) { + /** @var \Drupal\Core\Layout\LayoutInterface $layout */ + $layout = $this->layoutPluginManager->createInstance($item->get('layout')->getValue()); + $section = $item->get('section')->getValue(); + $regions = []; + foreach ($section as $region => $blocks) { + foreach ($blocks as $uuid => $configuration) { + /** @var \Drupal\Core\Block\BlockPluginInterface $block */ + $block = $this->blockManager->createInstance($configuration['plugin_id'], $configuration); + $access = $block->access($this->account, TRUE); + if ($access->isAllowed()) { + $regions[$region][$uuid] = $block->build(); + } + } + } + return $layout->build($regions); + } + +} diff --git a/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSection.php b/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSection.php new file mode 100644 index 0000000000..9da1937388 --- /dev/null +++ b/core/modules/layout_builder/src/Plugin/Field/FieldType/LayoutSection.php @@ -0,0 +1,86 @@ +setLabel(new TranslatableMarkup('Layout')) + ->setSetting('case_sensitive', FALSE) + ->setRequired(TRUE); + $properties[static::mainPropertyName()] = MapDataDefinition::create('map') + ->setLabel(new TranslatableMarkup('Layout Section')) + ->setRequired(FALSE); + + return $properties; + } + + /** + * @inheritDoc + */ + public static function mainPropertyName() { + return 'section'; + } + + /** + * {@inheritdoc} + */ + public static function schema(FieldStorageDefinitionInterface $field_definition) { + $schema = [ + 'columns' => [ + 'layout' => [ + 'type' => 'varchar', + 'length' => '255', + 'binary' => FALSE, + ], + static::mainPropertyName() => [ + 'type' => 'blob', + 'size' => 'normal', + 'serialize' => TRUE, + ], + ] + ]; + + return $schema; + } + + /** + * {@inheritdoc} + */ + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + $values['layout'] = 'layout_onecol'; + $values[static::mainPropertyName()] = []; + return $values; + } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + $sections = $this->get(static::mainPropertyName())->getValue(); + return empty($sections); + } + +}