3 namespace Drupal\layout_builder;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Entity\RevisionableInterface;
10 use Drupal\layout_builder\Plugin\Block\InlineBlock;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Defines a class for reacting to entity events related to Inline Blocks.
18 class InlineBlockEntityOperations implements ContainerInjectionInterface {
20 use LayoutEntityHelperTrait;
23 * Inline block usage tracking service.
25 * @var \Drupal\layout_builder\InlineBlockUsage
30 * The block content storage.
32 * @var \Drupal\Core\Entity\EntityStorageInterface
34 protected $blockContentStorage;
37 * The entity type manager.
39 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
41 protected $entityTypeManager;
44 * Constructs a new EntityOperations object.
46 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
47 * The entity type manager service.
48 * @param \Drupal\layout_builder\InlineBlockUsage $usage
49 * Inline block usage tracking service.
50 * @param \Drupal\Core\Database\Connection $database
51 * The database connection.
53 public function __construct(EntityTypeManagerInterface $entityTypeManager, InlineBlockUsage $usage, Connection $database) {
54 $this->entityTypeManager = $entityTypeManager;
55 $this->blockContentStorage = $entityTypeManager->getStorage('block_content');
56 $this->usage = $usage;
62 public static function create(ContainerInterface $container) {
64 $container->get('entity_type.manager'),
65 $container->get('inline_block.usage'),
66 $container->get('database')
71 * Remove all unused inline blocks on save.
73 * Entities that were used in prevision revisions will be removed if not
74 * saving a new revision.
76 * @param \Drupal\Core\Entity\EntityInterface $entity
79 protected function removeUnusedForEntityOnSave(EntityInterface $entity) {
80 // If the entity is new or '$entity->original' is not set then there will
81 // not be any unused inline blocks to remove.
82 // If this is a revisionable entity then do not remove inline blocks. They
83 // could be referenced in previous revisions even if this is not a new
85 if ($entity->isNew() || !isset($entity->original) || $entity instanceof RevisionableInterface) {
88 $sections = $this->getEntitySections($entity);
89 // If this is a layout override and there are no sections then it is a new
91 if ($this->isEntityUsingFieldOverride($entity) && empty($sections)) {
95 // Delete and remove the usage for inline blocks that were removed.
96 if ($removed_block_ids = $this->getRemovedBlockIds($entity)) {
97 $this->deleteBlocksAndUsage($removed_block_ids);
102 * Gets the IDs of the inline blocks that were removed.
104 * @param \Drupal\Core\Entity\EntityInterface $entity
108 * The block content IDs that were removed.
110 protected function getRemovedBlockIds(EntityInterface $entity) {
111 $original_sections = $this->getEntitySections($entity->original);
112 $current_sections = $this->getEntitySections($entity);
113 // Avoid un-needed conversion from revision IDs to block content IDs by
114 // first determining if there are any revisions in the original that are not
115 // also in the current sections.
116 $current_block_content_revision_ids = $this->getInlineBlockRevisionIdsInSections($current_sections);
117 $original_block_content_revision_ids = $this->getInlineBlockRevisionIdsInSections($original_sections);
118 if ($unused_original_revision_ids = array_diff($original_block_content_revision_ids, $current_block_content_revision_ids)) {
119 // If there are any revisions in the original that aren't in the current
120 // there may some blocks that need to be removed.
121 $current_block_content_ids = $this->getBlockIdsForRevisionIds($current_block_content_revision_ids);
122 $unused_original_block_content_ids = $this->getBlockIdsForRevisionIds($unused_original_revision_ids);
123 return array_diff($unused_original_block_content_ids, $current_block_content_ids);
129 * Handles entity tracking on deleting a parent entity.
131 * @param \Drupal\Core\Entity\EntityInterface $entity
134 public function handleEntityDelete(EntityInterface $entity) {
135 if ($this->isLayoutCompatibleEntity($entity)) {
136 $this->usage->removeByLayoutEntity($entity);
141 * Handles saving a parent entity.
143 * @param \Drupal\Core\Entity\EntityInterface $entity
146 public function handlePreSave(EntityInterface $entity) {
147 if (!$this->isLayoutCompatibleEntity($entity)) {
150 $duplicate_blocks = FALSE;
152 if ($sections = $this->getEntitySections($entity)) {
153 if ($this->isEntityUsingFieldOverride($entity)) {
154 if (!$entity->isNew() && isset($entity->original)) {
155 if (empty($this->getEntitySections($entity->original))) {
156 // If there were no sections in the original entity then this is a
157 // new override from a default and the blocks need to be duplicated.
158 $duplicate_blocks = TRUE;
162 $new_revision = FALSE;
163 if ($entity instanceof RevisionableInterface) {
164 // If the parent entity will have a new revision create a new revision
166 // @todo Currently revisions are never created for the parent entity.
167 // This will be fixed in https://www.drupal.org/node/2937199.
168 // To work around this always make a revision when the parent entity
169 // is an instance of RevisionableInterface. After the issue is fixed
170 // only create a new revision if '$entity->isNewRevision()'.
171 $new_revision = TRUE;
174 foreach ($this->getInlineBlockComponents($sections) as $component) {
175 $this->saveInlineBlockComponent($entity, $component, $new_revision, $duplicate_blocks);
178 $this->removeUnusedForEntityOnSave($entity);
182 * Gets a block ID for an inline block plugin.
184 * @param \Drupal\layout_builder\Plugin\Block\InlineBlock $block_plugin
185 * The inline block plugin.
188 * The block content ID or null none available.
190 protected function getPluginBlockId(InlineBlock $block_plugin) {
191 $configuration = $block_plugin->getConfiguration();
192 if (!empty($configuration['block_revision_id'])) {
193 $revision_ids = $this->getBlockIdsForRevisionIds([$configuration['block_revision_id']]);
194 return array_pop($revision_ids);
200 * Delete the inline blocks and the usage records.
202 * @param int[] $block_content_ids
203 * The block content entity IDs.
205 protected function deleteBlocksAndUsage(array $block_content_ids) {
206 foreach ($block_content_ids as $block_content_id) {
207 if ($block = $this->blockContentStorage->load($block_content_id)) {
211 $this->usage->deleteUsage($block_content_ids);
215 * Removes unused inline blocks.
218 * The maximum number of inline blocks to remove.
220 public function removeUnused($limit = 100) {
221 $this->deleteBlocksAndUsage($this->usage->getUnused($limit));
225 * Gets blocks IDs for an array of revision IDs.
227 * @param int[] $revision_ids
233 protected function getBlockIdsForRevisionIds(array $revision_ids) {
235 $query = $this->blockContentStorage->getQuery();
236 $query->condition('revision_id', $revision_ids, 'IN');
237 $block_ids = $query->execute();
244 * Saves an inline block component.
246 * @param \Drupal\Core\Entity\EntityInterface $entity
247 * The entity with the layout.
248 * @param \Drupal\layout_builder\SectionComponent $component
249 * The section component with an inline block.
250 * @param bool $new_revision
251 * Whether a new revision of the block should be created.
252 * @param bool $duplicate_blocks
253 * Whether the blocks should be duplicated.
255 protected function saveInlineBlockComponent(EntityInterface $entity, SectionComponent $component, $new_revision, $duplicate_blocks) {
256 /** @var \Drupal\layout_builder\Plugin\Block\InlineBlock $plugin */
257 $plugin = $component->getPlugin();
258 $pre_save_configuration = $plugin->getConfiguration();
259 $plugin->saveBlockContent($new_revision, $duplicate_blocks);
260 $post_save_configuration = $plugin->getConfiguration();
261 if ($duplicate_blocks || (empty($pre_save_configuration['block_revision_id']) && !empty($post_save_configuration['block_revision_id']))) {
262 $this->usage->addUsage($this->getPluginBlockId($plugin), $entity);
264 $component->setConfiguration($post_save_configuration);