3 namespace Drupal\layout_builder;
5 use Drupal\Component\Plugin\DerivativeInspectionInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface;
8 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
11 * Methods to help with entities using the layout builder.
15 trait LayoutEntityHelperTrait {
18 * Determines if an entity can have a layout.
20 * @param \Drupal\Core\Entity\EntityInterface $entity
21 * The entity to check.
24 * TRUE if the entity can have a layout otherwise FALSE.
26 protected function isLayoutCompatibleEntity(EntityInterface $entity) {
27 return $entity instanceof LayoutEntityDisplayInterface || $this->isEntityUsingFieldOverride($entity);
31 * Gets revision IDs for layout sections.
33 * @param \Drupal\layout_builder\Section[] $sections
34 * The layout sections.
39 protected function getInlineBlockRevisionIdsInSections(array $sections) {
41 foreach ($this->getInlineBlockComponents($sections) as $component) {
42 $configuration = $component->getPlugin()->getConfiguration();
43 if (!empty($configuration['block_revision_id'])) {
44 $revision_ids[] = $configuration['block_revision_id'];
51 * Gets the sections for an entity if any.
53 * @todo Replace this method with calls to the SectionStorageManagerInterface
54 * method for getting sections from an entity in
55 * https://www.drupal.org/node/2986403.
57 * @param \Drupal\Core\Entity\EntityInterface $entity
60 * @return \Drupal\layout_builder\Section[]|null
61 * The entity layout sections if available.
63 protected function getEntitySections(EntityInterface $entity) {
64 if ($entity instanceof LayoutEntityDisplayInterface) {
65 return $entity->getSections();
67 elseif ($this->isEntityUsingFieldOverride($entity)) {
68 return $entity->get('layout_builder__layout')->getSections();
74 * Gets components that have Inline Block plugins.
76 * @param \Drupal\layout_builder\Section[] $sections
77 * The layout sections.
79 * @return \Drupal\layout_builder\SectionComponent[]
80 * The components that contain Inline Block plugins.
82 protected function getInlineBlockComponents(array $sections) {
83 $inline_block_components = [];
84 foreach ($sections as $section) {
85 foreach ($section->getComponents() as $component) {
86 $plugin = $component->getPlugin();
87 if ($plugin instanceof DerivativeInspectionInterface && $plugin->getBaseId() === 'inline_block') {
88 $inline_block_components[] = $component;
92 return $inline_block_components;
96 * Determines if an entity is using a field for the layout override.
98 * @param \Drupal\Core\Entity\EntityInterface $entity
102 * TRUE if the entity is using a field for a layout override.
104 protected function isEntityUsingFieldOverride(EntityInterface $entity) {
105 return $entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout');