You are here

protected function InlineBlockEntityOperations::removeUnusedForEntityOnSave in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/src/InlineBlockEntityOperations.php \Drupal\layout_builder\InlineBlockEntityOperations::removeUnusedForEntityOnSave()

Remove all unused inline blocks on save.

Entities that were used in prevision revisions will be removed if not saving a new revision.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The parent entity.

1 call to InlineBlockEntityOperations::removeUnusedForEntityOnSave()
InlineBlockEntityOperations::handlePreSave in core/modules/layout_builder/src/InlineBlockEntityOperations.php
Handles saving a parent entity.

File

core/modules/layout_builder/src/InlineBlockEntityOperations.php, line 101

Class

InlineBlockEntityOperations
Defines a class for reacting to entity events related to Inline Blocks.

Namespace

Drupal\layout_builder

Code

protected function removeUnusedForEntityOnSave(EntityInterface $entity) {

  // If the entity is new or '$entity->original' is not set then there will
  // not be any unused inline blocks to remove.
  // If this is a revisionable entity then do not remove inline blocks. They
  // could be referenced in previous revisions even if this is not a new
  // revision.
  if ($entity
    ->isNew() || !isset($entity->original) || $entity instanceof RevisionableInterface) {
    return;
  }

  // If the original entity used the default storage then we cannot remove
  // unused inline blocks because they will still be referenced in the
  // defaults.
  if ($this
    ->originalEntityUsesDefaultStorage($entity)) {
    return;
  }

  // Delete and remove the usage for inline blocks that were removed.
  if ($removed_block_ids = $this
    ->getRemovedBlockIds($entity)) {
    $this
      ->deleteBlocksAndUsage($removed_block_ids);
  }
}