Chris@17: getSectionStorageForEntity($entity) !== NULL; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Gets revision IDs for layout sections. Chris@17: * Chris@17: * @param \Drupal\layout_builder\Section[] $sections Chris@17: * The layout sections. Chris@17: * Chris@17: * @return int[] Chris@17: * The revision IDs. Chris@17: */ Chris@17: protected function getInlineBlockRevisionIdsInSections(array $sections) { Chris@17: $revision_ids = []; Chris@17: foreach ($this->getInlineBlockComponents($sections) as $component) { Chris@17: $configuration = $component->getPlugin()->getConfiguration(); Chris@17: if (!empty($configuration['block_revision_id'])) { Chris@17: $revision_ids[] = $configuration['block_revision_id']; Chris@17: } Chris@17: } Chris@17: return $revision_ids; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Gets the sections for an entity if any. Chris@17: * Chris@17: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@17: * The entity. Chris@17: * Chris@18: * @return \Drupal\layout_builder\Section[] Chris@17: * The entity layout sections if available. Chris@17: */ Chris@17: protected function getEntitySections(EntityInterface $entity) { Chris@18: $section_storage = $this->getSectionStorageForEntity($entity); Chris@18: return $section_storage ? $section_storage->getSections() : []; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Gets components that have Inline Block plugins. Chris@17: * Chris@17: * @param \Drupal\layout_builder\Section[] $sections Chris@17: * The layout sections. Chris@17: * Chris@17: * @return \Drupal\layout_builder\SectionComponent[] Chris@17: * The components that contain Inline Block plugins. Chris@17: */ Chris@17: protected function getInlineBlockComponents(array $sections) { Chris@17: $inline_block_components = []; Chris@17: foreach ($sections as $section) { Chris@17: foreach ($section->getComponents() as $component) { Chris@17: $plugin = $component->getPlugin(); Chris@17: if ($plugin instanceof DerivativeInspectionInterface && $plugin->getBaseId() === 'inline_block') { Chris@17: $inline_block_components[] = $component; Chris@17: } Chris@17: } Chris@17: } Chris@17: return $inline_block_components; Chris@17: } Chris@17: Chris@17: /** Chris@18: * Gets the section storage for an entity. Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@18: * The entity. Chris@18: * Chris@18: * @return \Drupal\layout_builder\SectionStorageInterface|null Chris@18: * The section storage if found otherwise NULL. Chris@18: */ Chris@18: protected function getSectionStorageForEntity(EntityInterface $entity) { Chris@18: // @todo Take into account other view modes in Chris@18: // https://www.drupal.org/node/3008924. Chris@18: $view_mode = 'full'; Chris@18: if ($entity instanceof LayoutEntityDisplayInterface) { Chris@18: $contexts['display'] = EntityContext::fromEntity($entity); Chris@18: } Chris@18: else { Chris@18: $contexts['entity'] = EntityContext::fromEntity($entity); Chris@18: if ($entity instanceof FieldableEntityInterface) { Chris@18: $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode); Chris@18: if ($display instanceof LayoutEntityDisplayInterface) { Chris@18: $contexts['display'] = EntityContext::fromEntity($display); Chris@18: } Chris@18: $contexts['view_mode'] = new Context(new ContextDefinition('string'), $view_mode); Chris@18: } Chris@18: } Chris@18: return $this->sectionStorageManager()->findByContext($contexts, new CacheableMetadata()); Chris@18: } Chris@18: Chris@18: /** Chris@17: * Determines if an entity is using a field for the layout override. Chris@17: * Chris@17: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@17: * The entity. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE if the entity is using a field for a layout override. Chris@18: * Chris@18: * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Chris@18: * To determine if an entity has a layout override, use Chris@18: * \Drupal\layout_builder\LayoutEntityHelperTrait::getSectionStorageForEntity() Chris@18: * and check whether the result is an instance of Chris@18: * \Drupal\layout_builder\DefaultsSectionStorageInterface. Chris@18: * Chris@18: * @see https://www.drupal.org/node/3030609 Chris@17: */ Chris@17: protected function isEntityUsingFieldOverride(EntityInterface $entity) { Chris@18: @trigger_error('\Drupal\layout_builder\LayoutEntityHelperTrait::isEntityUsingFieldOverride() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Internal storage of overrides may change so the existence of the field does not necessarily guarantee an overridable entity. See https://www.drupal.org/node/3030609.', E_USER_DEPRECATED); Chris@17: return $entity instanceof FieldableEntityInterface && $entity->hasField(OverridesSectionStorage::FIELD_NAME); Chris@17: } Chris@17: Chris@18: /** Chris@18: * Determines if the original entity used the default section storage. Chris@18: * Chris@18: * This method can be used during the entity save process to determine whether Chris@18: * $entity->original is set and used the default section storage plugin as Chris@18: * determined by ::getSectionStorageForEntity(). Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@18: * The entity. Chris@18: * Chris@18: * @return bool Chris@18: * TRUE if the original entity used the default storage. Chris@18: */ Chris@18: protected function originalEntityUsesDefaultStorage(EntityInterface $entity) { Chris@18: $section_storage = $this->getSectionStorageForEntity($entity); Chris@18: if ($section_storage instanceof OverridesSectionStorageInterface && !$entity->isNew() && isset($entity->original)) { Chris@18: $original_section_storage = $this->getSectionStorageForEntity($entity->original); Chris@18: return $original_section_storage instanceof DefaultsSectionStorageInterface; Chris@18: } Chris@18: return FALSE; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the section storage manager. Chris@18: * Chris@18: * @return \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface Chris@18: * The section storage manager. Chris@18: */ Chris@18: private function sectionStorageManager() { Chris@18: return $this->sectionStorageManager ?: \Drupal::service('plugin.manager.layout_builder.section_storage'); Chris@18: } Chris@18: Chris@17: }