Commit 46377812 authored by xjm's avatar xjm

Revert "Issue #2921626 by tim.plunkett, EclipseGc, larowlan, dead_arm: Add...

Revert "Issue #2921626 by tim.plunkett, EclipseGc, larowlan, dead_arm: Add proper context-awareness to Layout Builder"

This reverts commit 749f41ff.
parent 2909c978
...@@ -9,9 +9,6 @@ ...@@ -9,9 +9,6 @@
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
...@@ -169,13 +166,9 @@ function layout_builder_add_layout_section_field($entity_type_id, $bundle, $fiel ...@@ -169,13 +166,9 @@ function layout_builder_add_layout_section_field($entity_type_id, $bundle, $fiel
*/ */
function layout_builder_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { 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()) { if ($display->getThirdPartySetting('layout_builder', 'allow_custom', FALSE) && !$entity->layout_builder__layout->isEmpty()) {
$contexts = \Drupal::service('context.repository')->getAvailableContexts();
// @todo Use EntityContextDefinition after resolving
// https://www.drupal.org/node/2932462.
$contexts['layout_builder.entity'] = new Context(new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('@entity being viewed', ['@entity' => $entity->getEntityType()->getLabel()])), $entity);
$sections = $entity->layout_builder__layout->getSections(); $sections = $entity->layout_builder__layout->getSections();
foreach ($sections as $delta => $section) { foreach ($sections as $delta => $section) {
$build['_layout_builder'][$delta] = $section->toRenderArray($contexts); $build['_layout_builder'][$delta] = $section->toRenderArray();
} }
// If field layout is active, that is all that needs to be removed. // If field layout is active, that is all that needs to be removed.
......
<?php
namespace Drupal\layout_builder\Context;
use Drupal\Core\Plugin\Context\ContextInterface;
use Drupal\layout_builder\SectionStorageInterface;
/**
* Provides a wrapper around getting contexts from a section storage object.
*/
trait LayoutBuilderContextTrait {
/**
* The context repository.
*
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
*/
protected $contextRepository;
/**
* Gets the context repository service.
*
* @return \Drupal\Core\Plugin\Context\ContextRepositoryInterface
* The context repository service.
*/
protected function contextRepository() {
if (!$this->contextRepository) {
$this->contextRepository = \Drupal::service('context.repository');
}
return $this->contextRepository;
}
/**
* Provides all available contexts, both global and section_storage-specific.
*
* @param \Drupal\layout_builder\SectionStorageInterface $section_storage
* The section storage.
*
* @return \Drupal\Core\Plugin\Context\ContextInterface[]
* The array of context objects.
*/
protected function getAvailableContexts(SectionStorageInterface $section_storage) {
// Get all globally available contexts that have a defined value.
$contexts = array_filter($this->contextRepository()->getAvailableContexts(), function (ContextInterface $context) {
return $context->hasContextValue();
});
// Add in the per-section_storage contexts.
$contexts += $section_storage->getContexts();
return $contexts;
}
}
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
use Drupal\Core\Block\BlockManagerInterface; use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\layout_builder\SectionStorageInterface; use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -17,7 +16,6 @@ ...@@ -17,7 +16,6 @@
class ChooseBlockController implements ContainerInjectionInterface { class ChooseBlockController implements ContainerInjectionInterface {
use AjaxHelperTrait; use AjaxHelperTrait;
use LayoutBuilderContextTrait;
/** /**
* The block manager. * The block manager.
...@@ -62,8 +60,7 @@ public function build(SectionStorageInterface $section_storage, $delta, $region) ...@@ -62,8 +60,7 @@ public function build(SectionStorageInterface $section_storage, $delta, $region)
$build['#type'] = 'container'; $build['#type'] = 'container';
$build['#attributes']['class'][] = 'block-categories'; $build['#attributes']['class'][] = 'block-categories';
$definitions = $this->blockManager->getDefinitionsForContexts($this->getAvailableContexts($section_storage)); foreach ($this->blockManager->getGroupedDefinitions() as $category => $blocks) {
foreach ($this->blockManager->getGroupedDefinitions($definitions) as $category => $blocks) {
$build[$category]['#type'] = 'details'; $build[$category]['#type'] = 'details';
$build[$category]['#open'] = TRUE; $build[$category]['#open'] = TRUE;
$build[$category]['#title'] = $category; $build[$category]['#title'] = $category;
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface; use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\Section; use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionStorageInterface; use Drupal\layout_builder\SectionStorageInterface;
...@@ -20,7 +19,6 @@ ...@@ -20,7 +19,6 @@
*/ */
class LayoutBuilderController implements ContainerInjectionInterface { class LayoutBuilderController implements ContainerInjectionInterface {
use LayoutBuilderContextTrait;
use StringTranslationTrait; use StringTranslationTrait;
/** /**
...@@ -172,7 +170,7 @@ protected function buildAdministrativeSection(SectionStorageInterface $section_s ...@@ -172,7 +170,7 @@ protected function buildAdministrativeSection(SectionStorageInterface $section_s
$section = $section_storage->getSection($delta); $section = $section_storage->getSection($delta);
$layout = $section->getLayout(); $layout = $section->getLayout();
$build = $section->toRenderArray($this->getAvailableContexts($section_storage)); $build = $section->toRenderArray();
$layout_definition = $layout->getPluginDefinition(); $layout_definition = $layout->getPluginDefinition();
foreach ($layout_definition->getRegions() as $region => $info) { foreach ($layout_definition->getRegions() as $region => $info) {
......
...@@ -3,9 +3,6 @@ ...@@ -3,9 +3,6 @@
namespace Drupal\layout_builder\Field; namespace Drupal\layout_builder\Field;
use Drupal\Core\Field\FieldItemList; use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\layout_builder\Section; use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionStorageInterface; use Drupal\layout_builder\SectionStorageInterface;
...@@ -77,17 +74,6 @@ public function removeSection($delta) { ...@@ -77,17 +74,6 @@ public function removeSection($delta) {
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
public function getContexts() {
$entity = $this->getEntity();
// @todo Use EntityContextDefinition after resolving
// https://www.drupal.org/node/2932462.
$contexts['layout_builder.entity'] = new Context(new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('@entity being viewed', ['@entity' => $entity->getEntityType()->getLabel()])), $entity);
return $contexts;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\PluginFormFactoryInterface; use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface; use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\layout_builder\Controller\LayoutRebuildTrait; use Drupal\layout_builder\Controller\LayoutRebuildTrait;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface; use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\Section; use Drupal\layout_builder\Section;
...@@ -30,7 +29,6 @@ abstract class ConfigureBlockFormBase extends FormBase { ...@@ -30,7 +29,6 @@ abstract class ConfigureBlockFormBase extends FormBase {
use AjaxFormHelperTrait; use AjaxFormHelperTrait;
use ContextAwarePluginAssignmentTrait; use ContextAwarePluginAssignmentTrait;
use LayoutBuilderContextTrait;
use LayoutRebuildTrait; use LayoutRebuildTrait;
/** /**
...@@ -181,7 +179,7 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt ...@@ -181,7 +179,7 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt
$this->region = $region; $this->region = $region;
$this->block = $this->prepareBlock($plugin_id, $configuration); $this->block = $this->prepareBlock($plugin_id, $configuration);
$form_state->setTemporaryValue('gathered_contexts', $this->getAvailableContexts($section_storage)); $form_state->setTemporaryValue('gathered_contexts', $this->contextRepository->getAvailableContexts());
// @todo Remove once https://www.drupal.org/node/2268787 is resolved. // @todo Remove once https://www.drupal.org/node/2268787 is resolved.
$form_state->set('block_theme', $this->config('system.theme')->get('default')); $form_state->set('block_theme', $this->config('system.theme')->get('default'));
......
...@@ -66,16 +66,13 @@ public function __construct($layout_id, array $layout_settings = [], array $comp ...@@ -66,16 +66,13 @@ public function __construct($layout_id, array $layout_settings = [], array $comp
/** /**
* Returns the renderable array for this section. * Returns the renderable array for this section.
* *
* @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
* An array of available contexts.
*
* @return array * @return array
* A renderable array representing the content of the section. * A renderable array representing the content of the section.
*/ */
public function toRenderArray(array $contexts = []) { public function toRenderArray() {
$regions = []; $regions = [];
foreach ($this->getComponents() as $component) { foreach ($this->getComponents() as $component) {
if ($output = $component->toRenderArray($contexts)) { if ($output = $component->toRenderArray()) {
$regions[$component->getRegion()][$component->getUuid()] = $output; $regions[$component->getRegion()][$component->getUuid()] = $output;
} }
} }
......
...@@ -88,16 +88,13 @@ public function __construct($uuid, $region, array $configuration = [], array $ad ...@@ -88,16 +88,13 @@ public function __construct($uuid, $region, array $configuration = [], array $ad
/** /**
* Returns the renderable array for this component. * Returns the renderable array for this component.
* *
* @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
* An array of available contexts.
*
* @return array * @return array
* A renderable array representing the content of the component. * A renderable array representing the content of the component.
*/ */
public function toRenderArray(array $contexts = []) { public function toRenderArray() {
$output = []; $output = [];
$plugin = $this->getPlugin($contexts); $plugin = $this->getPlugin();
// @todo Figure out the best way to unify fields and blocks and components // @todo Figure out the best way to unify fields and blocks and components
// in https://www.drupal.org/node/1875974. // in https://www.drupal.org/node/1875974.
if ($plugin instanceof BlockPluginInterface) { if ($plugin instanceof BlockPluginInterface) {
...@@ -262,15 +259,13 @@ public function getUuid() { ...@@ -262,15 +259,13 @@ public function getUuid() {
/** /**
* Gets the plugin for this component. * Gets the plugin for this component.
* *
* @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
* An array of contexts to set on the plugin.
*
* @return \Drupal\Component\Plugin\PluginInspectionInterface * @return \Drupal\Component\Plugin\PluginInspectionInterface
* The plugin. * The plugin.
*/ */
public function getPlugin(array $contexts = []) { public function getPlugin() {
$plugin = $this->pluginManager()->createInstance($this->getPluginId(), $this->getConfiguration()); $plugin = $this->pluginManager()->createInstance($this->getPluginId(), $this->getConfiguration());
if ($contexts && $plugin instanceof ContextAwarePluginInterface) { if ($plugin instanceof ContextAwarePluginInterface) {
$contexts = $this->contextRepository()->getRuntimeContexts(array_values($plugin->getContextMapping()));
$this->contextHandler()->applyContextMapping($plugin, $contexts); $this->contextHandler()->applyContextMapping($plugin, $contexts);
} }
return $plugin; return $plugin;
...@@ -286,6 +281,16 @@ protected function pluginManager() { ...@@ -286,6 +281,16 @@ protected function pluginManager() {
return \Drupal::service('plugin.manager.block'); return \Drupal::service('plugin.manager.block');
} }
/**
* Wraps the context repository.
*
* @return \Drupal\Core\Plugin\Context\ContextRepositoryInterface
* The context repository.
*/
protected function contextRepository() {
return \Drupal::service('context.repository');
}
/** /**
* Wraps the context handler. * Wraps the context handler.
* *
......
...@@ -68,14 +68,6 @@ public function insertSection($delta, Section $section); ...@@ -68,14 +68,6 @@ public function insertSection($delta, Section $section);
*/ */
public function removeSection($delta); public function removeSection($delta);
/**
* Provides any available contexts for the object using the sections.
*
* @return \Drupal\Core\Plugin\Context\ContextInterface[]
* The array of context objects.
*/
public function getContexts();
/** /**
* Returns an identifier for this storage. * Returns an identifier for this storage.
* *
......
...@@ -55,7 +55,7 @@ protected function setUp() { ...@@ -55,7 +55,7 @@ protected function setUp() {
*/ */
public function providerTestLayoutSectionFormatter() { public function providerTestLayoutSectionFormatter() {
$data = []; $data = [];
$data['block_with_global_context'] = [ $data['block_with_context'] = [
[ [
[ [
'section' => new Section('layout_onecol', [], [ 'section' => new Section('layout_onecol', [], [
...@@ -80,31 +80,6 @@ public function providerTestLayoutSectionFormatter() { ...@@ -80,31 +80,6 @@ public function providerTestLayoutSectionFormatter() {
'user:2', 'user:2',
'UNCACHEABLE', 'UNCACHEABLE',
]; ];
$data['block_with_entity_context'] = [
[
[
'section' => new Section('layout_onecol', [], [
'baz' => new SectionComponent('baz', 'content', [
'id' => 'field_block:node:body',
'context_mapping' => [
'entity' => 'layout_builder.entity',
],
]),
]),
],
],
[
'.layout--onecol',
'.field--name-body',
],
[
'Body',
'The node body',
],
'',
'',
'MISS',
];
$data['single_section_single_block'] = [ $data['single_section_single_block'] = [
[ [
[ [
......
...@@ -199,6 +199,9 @@ public function testContextAwareBlock() { ...@@ -199,6 +199,9 @@ public function testContextAwareBlock() {
$block->getDerivativeId()->willReturn(NULL); $block->getDerivativeId()->willReturn(NULL);
$block->getConfiguration()->willReturn([]); $block->getConfiguration()->willReturn([]);
$this->contextRepository->getRuntimeContexts([])->willReturn([]);
$this->contextHandler->applyContextMapping($block->reveal(), [])->shouldBeCalled();
$section = [ $section = [
new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']), new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
]; ];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment