Skip to content
Snippets Groups Projects
Commit ebb19630 authored by Florent Torregrosa's avatar Florent Torregrosa
Browse files

Merge branch '3444419-wrong-detection-of' into '8.x-1.x'

Draft: Issue #3444419 by Grimreaper: Wrong detection of layout builder overrides

See merge request !52
parents e1a32a91 0d04e610
No related branches found
No related tags found
No related merge requests found
Pipeline #160941 passed with warnings
......@@ -11,6 +11,7 @@ use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Template\AttributeHelper;
use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\ui_styles\SectionStorageTrait;
use Drupal\ui_styles\StylePluginManagerInterface;
use Drupal\ui_styles_entity_status\UiStylesEntityStatusInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -20,6 +21,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class EntityView implements ContainerInjectionInterface {
use SectionStorageTrait;
/**
* The styles plugin manager.
*
......@@ -100,7 +103,7 @@ class EntityView implements ContainerInjectionInterface {
$layout_builder = &$build['_layout_builder'];
$layout_field_name = OverridesSectionStorage::FIELD_NAME;
// Layout override: we are dealing with a content entity.
if ($entity->hasField($layout_field_name) && !$entity->get($layout_field_name)->isEmpty()) {
if ($entity->hasField($layout_field_name) && $this->isDisplayOverridden($entity, $display, $view_mode)) {
foreach ($entity->get($layout_field_name) as $delta => $section_item) {
/** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $section_item */
if (!$layout_builder[$delta]) {
......
......@@ -266,6 +266,7 @@ function ui_styles_layout_builder_entity_view_alter(array &$build, EntityInterfa
$styles_manager = \Drupal::service('plugin.manager.ui_styles');
$layout_builder = &$build['_layout_builder'];
$layout_field_name = OverridesSectionStorage::FIELD_NAME;
// @todo use SectionStorageTrait to fix overrides detection logic.
// Layout override: we are dealing with a content entity.
if ($entity->hasField($layout_field_name) && !$entity->get($layout_field_name)->isEmpty()) {
if ($build['#view_mode'] !== 'default' && $build['#view_mode'] !== 'full') {
......
<?php
declare(strict_types=1);
namespace Drupal\ui_styles;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\layout_builder\OverridesSectionStorageInterface;
/**
* Helper trait to get Layout Builder section storage overrides.
*
* Workaround until Core API improvements is done.
*/
trait SectionStorageTrait {
/**
* Check if the display is Layout Builder overridden.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The entity view display holding the display options configured for the
* entity components.
* @param string $viewMode
* The view mode the entity is rendered in.
*
* @return bool
* True if overridden.
*
* @see \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::buildSections()
*/
protected function isDisplayOverridden(ContentEntityInterface $entity, EntityViewDisplayInterface $display, string $viewMode): bool {
/** @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $section_storage_manager */
$section_storage_manager = \Drupal::service('plugin.manager.layout_builder.section_storage');
$contexts = $this->getContextsForEntity($entity, $display, $viewMode);
$label = new TranslatableMarkup('@entity being viewed', [
'@entity' => $entity->getEntityType()->getSingularLabel(),
]);
$contexts['layout_builder.entity'] = EntityContext::fromEntity($entity, $label);
$cacheability = new CacheableMetadata();
$storage = $section_storage_manager->findByContext($contexts, $cacheability);
if ($storage instanceof OverridesSectionStorageInterface) {
return $storage->isOverridden();
}
return FALSE;
}
/**
* Gets the available contexts for a given entity.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The entity view display holding the display options configured for the
* entity components.
* @param string $viewMode
* The view mode the entity is rendered in.
*
* @return \Drupal\Core\Plugin\Context\ContextInterface[]
* An array of context objects for a given entity.
*
* @see \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::getContextsForEntity()
*/
protected function getContextsForEntity(FieldableEntityInterface $entity, EntityViewDisplayInterface $display, string $viewMode): array {
/** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contextRepository */
$contextRepository = \Drupal::service('context.repository');
$available_context_ids = \array_keys($contextRepository->getAvailableContexts());
return [
'view_mode' => new Context(ContextDefinition::create('string'), $viewMode),
'entity' => EntityContext::fromEntity($entity),
'display' => EntityContext::fromEntity($display),
] + $contextRepository->getRuntimeContexts($available_context_ids);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment