Unverified Commit 8946662d authored by larowlan's avatar larowlan

Issue #2928450 by tim.plunkett: Remove dead code in the Layout Builder...

Issue #2928450 by tim.plunkett: Remove dead code in the Layout Builder following Section refactoring
parent a351d726
......@@ -41,7 +41,7 @@ function layout_builder_entity_type_alter(array &$entity_types) {
* Removes the Layout Builder field both visually and from the #fields handling.
*
* This prevents any interaction with this field. It is rendered directly
* in layout_builder_entity_view_display_alter().
* in layout_builder_entity_view_alter().
*
* @internal
*/
......@@ -161,27 +161,16 @@ function layout_builder_add_layout_section_field($entity_type_id, $bundle, $fiel
return $field;
}
/**
* Implements hook_entity_view_display_alter().
*/
function layout_builder_entity_view_display_alter(EntityViewDisplayInterface $display, array $context) {
if ($display->getThirdPartySetting('layout_builder', 'allow_custom', FALSE)) {
// Force the layout to render with no label.
$display->setComponent('layout_builder__layout', [
'label' => 'hidden',
'region' => '__layout_builder',
]);
}
else {
$display->removeComponent('layout_builder__layout');
}
}
/**
* Implements hook_entity_view_alter().
*/
function layout_builder_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($display->getThirdPartySetting('layout_builder', 'allow_custom', FALSE) && !$entity->layout_builder__layout->isEmpty()) {
$sections = $entity->layout_builder__layout->getSections();
foreach ($sections as $delta => $section) {
$build['_layout_builder'][$delta] = $section->toRenderArray();
}
// If field layout is active, that is all that needs to be removed.
if (\Drupal::moduleHandler()->moduleExists('field_layout') && isset($build['_field_layout'])) {
unset($build['_field_layout']);
......
services:
layout_builder.builder:
class: Drupal\layout_builder\LayoutSectionBuilder
arguments: ['@current_user', '@plugin.manager.core.layout', '@plugin.manager.block', '@context.handler', '@context.repository']
layout_builder.tempstore_repository:
class: Drupal\layout_builder\LayoutTempstoreRepository
arguments: ['@user.shared_tempstore', '@entity_type.manager']
access_check.entity.layout:
class: Drupal\layout_builder\Access\LayoutSectionAccessCheck
arguments: ['@entity_type.manager']
tags:
- { name: access_check, applies_to: _has_layout_section }
layout_builder.routes:
......@@ -15,7 +11,6 @@ services:
arguments: ['@entity_type.manager', '@entity_field.manager']
layout_builder.route_enhancer:
class: Drupal\layout_builder\Routing\LayoutBuilderRouteEnhancer
arguments: ['@entity_type.manager']
tags:
- { name: route_enhancer }
layout_builder.param_converter:
......
......@@ -3,7 +3,6 @@
namespace Drupal\layout_builder\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
......@@ -16,23 +15,6 @@
*/
class LayoutSectionAccessCheck implements AccessInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new LayoutSectionAccessCheck.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* Checks routing access to layout for the entity.
*
......
......@@ -2,14 +2,11 @@
namespace Drupal\layout_builder\Controller;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\layout_builder\LayoutSectionBuilder;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\Section;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -24,27 +21,6 @@ class LayoutBuilderController implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The layout builder.
*
* @var \Drupal\layout_builder\LayoutSectionBuilder
*/
protected $builder;
/**
* The layout manager.
*
* @var \Drupal\Core\Layout\LayoutPluginManagerInterface
*/
protected $layoutManager;
/**
* The block manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockManager;
/**
* The layout tempstore repository.
*
......@@ -55,19 +31,10 @@ class LayoutBuilderController implements ContainerInjectionInterface {
/**
* LayoutBuilderController constructor.
*
* @param \Drupal\layout_builder\LayoutSectionBuilder $builder
* The layout section builder.
* @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_manager
* The layout manager.
* @param \Drupal\Core\Block\BlockManagerInterface $block_manager
* The block manager.
* @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
* The layout tempstore repository.
*/
public function __construct(LayoutSectionBuilder $builder, LayoutPluginManagerInterface $layout_manager, BlockManagerInterface $block_manager, LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
$this->builder = $builder;
$this->layoutManager = $layout_manager;
$this->blockManager = $block_manager;
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
}
......@@ -76,9 +43,6 @@ public function __construct(LayoutSectionBuilder $builder, LayoutPluginManagerIn
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('layout_builder.builder'),
$container->get('plugin.manager.core.layout'),
$container->get('plugin.manager.block'),
$container->get('layout_builder.tempstore_repository')
);
}
......
......@@ -8,7 +8,6 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Layout\LayoutInterface;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface;
......@@ -41,13 +40,6 @@ class ConfigureSectionForm extends FormBase {
*/
protected $layout;
/**
* The layout manager.
*
* @var \Drupal\Core\Layout\LayoutPluginManagerInterface
*/
protected $layoutManager;
/**
* The plugin form manager.
*
......@@ -81,16 +73,13 @@ class ConfigureSectionForm extends FormBase {
*
* @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
* The layout tempstore repository.
* @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_manager
* The layout manager.
* @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
* The class resolver.
* @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager
* The plugin form manager.
*/
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, LayoutPluginManagerInterface $layout_manager, ClassResolverInterface $class_resolver, PluginFormFactoryInterface $plugin_form_manager) {
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver, PluginFormFactoryInterface $plugin_form_manager) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
$this->layoutManager = $layout_manager;
$this->classResolver = $class_resolver;
$this->pluginFormFactory = $plugin_form_manager;
}
......@@ -101,7 +90,6 @@ public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore
public static function create(ContainerInterface $container) {
return new static(
$container->get('layout_builder.tempstore_repository'),
$container->get('plugin.manager.core.layout'),
$container->get('class_resolver'),
$container->get('plugin_form.factory')
);
......
<?php
namespace Drupal\layout_builder;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Layout\LayoutInterface;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Builds the UI for layout sections.
*
* @internal
*
* @todo Remove in https://www.drupal.org/project/drupal/issues/2928450.
*/
class LayoutSectionBuilder {
use StringTranslationTrait;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* The layout plugin manager.
*
* @var \Drupal\Core\Layout\LayoutPluginManagerInterface
*/
protected $layoutPluginManager;
/**
* The block plugin manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockManager;
/**
* The plugin context handler.
*
* @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
*/
protected $contextHandler;
/**
* The context manager service.
*
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
*/
protected $contextRepository;
/**
* Constructs a LayoutSectionFormatter object.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
* @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layoutPluginManager
* The layout plugin manager.
* @param \Drupal\Core\Block\BlockManagerInterface $blockManager
* THe block plugin manager.
* @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
* The ContextHandler for applying contexts to conditions properly.
* @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
* The lazy context repository service.
*/
public function __construct(AccountInterface $account, LayoutPluginManagerInterface $layoutPluginManager, BlockManagerInterface $blockManager, ContextHandlerInterface $context_handler, ContextRepositoryInterface $context_repository) {
$this->account = $account;
$this->layoutPluginManager = $layoutPluginManager;
$this->blockManager = $blockManager;
$this->contextHandler = $context_handler;
$this->contextRepository = $context_repository;
}
/**
* Builds the render array for the layout section.
*
* @param \Drupal\Core\Layout\LayoutInterface $layout
* The ID of the layout.
* @param \Drupal\layout_builder\SectionComponent[] $components
* An array of components.
*
* @return array
* The render array for a given section.
*/
public function buildSectionFromLayout(LayoutInterface $layout, array $components) {
$regions = [];
foreach ($components as $component) {
if ($output = $component->toRenderArray()) {
$regions[$component->getRegion()][$component->getUuid()] = $output;
}
}
return $layout->build($regions);
}
/**
* Builds the render array for the layout section.
*
* @param string $layout_id
* The ID of the layout.
* @param array $layout_settings
* The configuration for the layout.
* @param \Drupal\layout_builder\SectionComponent[] $components
* An array of components.
*
* @return array
* The render array for a given section.
*/
public function buildSection($layout_id, array $layout_settings, array $components) {
$layout = $this->layoutPluginManager->createInstance($layout_id, $layout_settings);
return $this->buildSectionFromLayout($layout, $components);
}
}
......@@ -2,12 +2,8 @@
namespace Drupal\layout_builder\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\layout_builder\LayoutSectionBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'layout_section' formatter.
......@@ -22,55 +18,7 @@
* }
* )
*/
class LayoutSectionFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* The layout section builder.
*
* @var \Drupal\layout_builder\LayoutSectionBuilder
*/
protected $builder;
/**
* Constructs a LayoutSectionFormatter object.
*
* @param \Drupal\layout_builder\LayoutSectionBuilder $builder
* The layout section builder.
* @param string $plugin_id
* The plugin ID for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
*/
public function __construct(LayoutSectionBuilder $builder, $plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings) {
$this->builder = $builder;
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('layout_builder.builder'),
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings']
);
}
class LayoutSectionFormatter extends FormatterBase {
/**
* {@inheritdoc}
......
......@@ -18,7 +18,6 @@
* id = "layout_section",
* label = @Translation("Layout Section"),
* description = @Translation("Layout Section"),
* default_formatter = "layout_section",
* list_class = "\Drupal\layout_builder\Field\LayoutSectionItemList",
* no_ui = TRUE,
* cardinality = \Drupal\Core\Field\FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
......
......@@ -4,18 +4,17 @@
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity_test\Entity\EntityTestBaseFieldDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\layout_builder\Section;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
/**
* Ensures that Layout Builder and Field Layout are compatible with each other.
*
* @group layout_builder
*/
class LayoutBuilderFieldLayoutCompatibilityTest extends KernelTestBase {
class LayoutBuilderFieldLayoutCompatibilityTest extends EntityKernelTestBase {
/**
* {@inheritdoc}
......@@ -23,12 +22,6 @@ class LayoutBuilderFieldLayoutCompatibilityTest extends KernelTestBase {
public static $modules = [
'layout_discovery',
'field_layout',
'user',
'field',
'entity_test',
'system',
'text',
'filter',
];
/**
......@@ -45,9 +38,7 @@ protected function setUp() {
parent::setUp();
$this->installEntitySchema('entity_test_base_field_display');
$this->installEntitySchema('user');
$this->installSchema('system', ['sequences', 'key_value']);
$this->installConfig(['field', 'filter', 'user', 'system']);
$this->installConfig(['filter']);
\Drupal::service('theme_handler')->install(['classy']);
$this->config('system.theme')->set('default', 'classy')->save();
......@@ -100,9 +91,10 @@ public function testCompatibility() {
// Install the Layout Builder, configure it for this entity display, and
// reload the entity.
$this->enableModules(['layout_builder']);
$this->installModule('layout_builder');
$this->display = $this->reloadEntity($this->display);
$this->display->setThirdPartySetting('layout_builder', 'allow_custom', TRUE)->save();
$entity = EntityTestBaseFieldDisplay::load($entity->id());
$entity = $this->reloadEntity($entity);
// Without using Layout Builder for an override, the result has not changed.
$new_markup = $this->renderEntity($entity);
......
......@@ -15,16 +15,16 @@
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\layout_builder\LayoutSectionBuilder;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\layout_builder\LayoutSectionBuilder
* @coversDefaultClass \Drupal\layout_builder\Section
* @group layout_builder
*/
class LayoutSectionBuilderTest extends UnitTestCase {
class SectionRenderTest extends UnitTestCase {
/**
* The current user.
......@@ -33,13 +33,6 @@ class LayoutSectionBuilderTest extends UnitTestCase {
*/
protected $account;
/**
* The layout plugin manager.
*
* @var \Drupal\Core\Layout\LayoutPluginManagerInterface
*/
protected $layoutPluginManager;
/**
* The block plugin manager.
*
......@@ -61,20 +54,6 @@ class LayoutSectionBuilderTest extends UnitTestCase {
*/
protected $contextRepository;
/**
* The object under test.
*
* @var \Drupal\layout_builder\LayoutSectionBuilder
*/
protected $layoutSectionBuilder;
/**
* The layout plugin.
*
* @var \Drupal\Core\Layout\LayoutInterface
*/
protected $layout;
/**
* {@inheritdoc}
*/
......@@ -82,29 +61,29 @@ protected function setUp() {
parent::setUp();
$this->account = $this->prophesize(AccountInterface::class);
$this->layoutPluginManager = $this->prophesize(LayoutPluginManagerInterface::class);
$layout_plugin_manager = $this->prophesize(LayoutPluginManagerInterface::class);
$this->blockManager = $this->prophesize(BlockManagerInterface::class);
$this->contextHandler = $this->prophesize(ContextHandlerInterface::class);
$this->contextRepository = $this->prophesize(ContextRepositoryInterface::class);
$this->layoutSectionBuilder = new LayoutSectionBuilder($this->account->reveal(), $this->layoutPluginManager->reveal(), $this->blockManager->reveal(), $this->contextHandler->reveal(), $this->contextRepository->reveal());
$this->layout = $this->prophesize(LayoutInterface::class);
$this->layout->getPluginDefinition()->willReturn(new LayoutDefinition([]));
$this->layout->build(Argument::type('array'))->willReturnArgument(0);
$this->layoutPluginManager->createInstance('layout_onecol', [])->willReturn($this->layout->reveal());
$layout = $this->prophesize(LayoutInterface::class);
$layout->getPluginDefinition()->willReturn(new LayoutDefinition([]));
$layout->build(Argument::type('array'))->willReturnArgument(0);
$layout_plugin_manager->createInstance('layout_onecol', [])->willReturn($layout->reveal());
$container = new ContainerBuilder();
$container->set('current_user', $this->account->reveal());
$container->set('plugin.manager.block', $this->blockManager->reveal());
$container->set('plugin.manager.core.layout', $layout_plugin_manager->reveal());
$container->set('context.handler', $this->contextHandler->reveal());
$container->set('context.repository', $this->contextRepository->reveal());
\Drupal::setContainer($container);
}
/**
* @covers ::buildSection
* @covers ::toRenderArray
*/
public function testBuildSection() {
public function testToRenderArray() {
$block_content = ['#markup' => 'The block content.'];
$render_array = [
'#theme' => 'block',
......@@ -143,15 +122,14 @@ public function testBuildSection() {
'some_uuid' => $render_array,
],
];
$result = $this->layoutSectionBuilder->buildSection('layout_onecol', [], $section);
$result = (new Section('layout_onecol', [], $section))->toRenderArray();
$this->assertEquals($expected, $result);
}
/**
* @covers ::buildSection
* @covers ::toRenderArray
*/
public function testBuildSectionAccessDenied() {
public function testToRenderArrayAccessDenied() {
$block = $this->prophesize(BlockPluginInterface::class);
$this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
......@@ -173,22 +151,22 @@ public function testBuildSectionAccessDenied() {
],
],
];
$result = $this->layoutSectionBuilder->buildSection('layout_onecol', [], $section);
$result = (new Section('layout_onecol', [], $section))->toRenderArray();
$this->assertEquals($expected, $result);
}
/**
* @covers ::buildSection
* @covers ::toRenderArray
*/
public function testBuildSectionEmpty() {