Skip to content
Snippets Groups Projects

Issue #3359137: Update inline block usage on import

2 files
+ 164
0
Compare changes
  • Side-by-side
  • Inline

Files

 
<?php
 
 
declare(strict_types = 1);
 
 
namespace Drupal\default_content\EventSubscriber;
 
 
use Drupal\Core\Entity\EntityStorageInterface;
 
use Drupal\Core\Entity\EntityTypeManagerInterface;
 
use Drupal\default_content\Event\DefaultContentEvents;
 
use Drupal\default_content\Event\ImportEvent;
 
use Drupal\layout_builder\InlineBlockUsageInterface;
 
use Drupal\layout_builder\LayoutEntityHelperTrait;
 
use Drupal\layout_builder\Plugin\Block\InlineBlock;
 
use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface;
 
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
 
/**
 
* Fix inline block usage after install.
 
*
 
* @see \Drupal\layout_builder\InlineBlockEntityOperations
 
*/
 
class InlineBlockUsageFixer implements EventSubscriberInterface {
 
use LayoutEntityHelperTrait;
 
 
/**
 
* The entity type manager service.
 
*
 
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
 
*/
 
protected EntityTypeManagerInterface $entityTypeManager;
 
 
/**
 
* Inline block usage tracking service.
 
*
 
* @var \Drupal\layout_builder\InlineBlockUsageInterface|null
 
*/
 
protected $usage;
 
 
/**
 
* The section storage manager.
 
*
 
* @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface|null
 
*/
 
protected $sectionStorageManager;
 
 
/**
 
* The block content storage.
 
*
 
* @var \Drupal\Core\Entity\EntityStorageInterface
 
*/
 
protected EntityStorageInterface $blockContentStorage;
 
 
/**
 
* Constructor.
 
*
 
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
 
* The entity type manager service.
 
* @param \Drupal\layout_builder\InlineBlockUsageInterface|null $usage
 
* Inline block usage tracking service.
 
* @param \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface|null $sectionStorageManager
 
* The section storage manager.
 
*/
 
public function __construct(
 
EntityTypeManagerInterface $entityTypeManager,
 
?InlineBlockUsageInterface $usage,
 
?SectionStorageManagerInterface $sectionStorageManager
 
) {
 
$this->entityTypeManager = $entityTypeManager;
 
$this->usage = $usage;
 
$this->sectionStorageManager = $sectionStorageManager;
 
}
 
 
/**
 
* {@inheritdoc}
 
*/
 
public static function getSubscribedEvents() {
 
return [
 
DefaultContentEvents::IMPORT => 'initUsage',
 
];
 
}
 
 
/**
 
* Init inline block layout builder usage for imported entities.
 
*
 
* @param \Drupal\default_content\Event\ImportEvent $importEvent
 
* The default content import event.
 
*/
 
public function initUsage(ImportEvent $importEvent): void {
 
// Ensure Layout Builder module is present.
 
if (!$this->sectionStorageManager instanceof SectionStorageManagerInterface
 
|| !$this->usage instanceof InlineBlockUsageInterface) {
 
return;
 
}
 
 
// Ensure Block content module is present.
 
try {
 
$this->blockContentStorage = $this->entityTypeManager->getStorage('block_content');
 
}
 
catch (\Exception $exception) {
 
return;
 
}
 
 
foreach ($importEvent->getImportedEntities() as $entity) {
 
$sections = $this->getEntitySections($entity);
 
if ($sections) {
 
foreach ($this->getInlineBlockComponents($sections) as $component) {
 
$plugin = $component->getPlugin();
 
if ($plugin instanceof InlineBlock) {
 
$block_id = $this->getPluginBlockId($plugin);
 
if ($block_id) {
 
$this->usage->addUsage($block_id, $entity);
 
}
 
}
 
}
 
}
 
}
 
}
 
 
/**
 
* Gets a block ID for an inline block plugin.
 
*
 
* @param \Drupal\layout_builder\Plugin\Block\InlineBlock $block_plugin
 
* The inline block plugin.
 
*
 
* @return int|null
 
* The block content ID or null none available.
 
*/
 
protected function getPluginBlockId(InlineBlock $block_plugin) {
 
$configuration = $block_plugin->getConfiguration();
 
if (!empty($configuration['block_revision_id'])) {
 
$revision_ids = $this->getBlockIdsForRevisionIds([$configuration['block_revision_id']]);
 
return \array_pop($revision_ids);
 
}
 
return NULL;
 
}
 
 
/**
 
* Gets blocks IDs for an array of revision IDs.
 
*
 
* @param int[] $revision_ids
 
* The revision IDs.
 
*
 
* @return int[]
 
* The block IDs.
 
*/
 
protected function getBlockIdsForRevisionIds(array $revision_ids): array {
 
if ($revision_ids) {
 
$query = $this->blockContentStorage->getQuery()->accessCheck(FALSE);
 
$query->condition('revision_id', $revision_ids, 'IN');
 
$block_ids = $query->execute();
 
return array_map('intval', $block_ids);
 
}
 
return [];
 
}
 
 
}
Loading