depcalc-8.x-1.x-dev/src/EventSubscriber/LayoutBuilderComponentDepencyCollector/BlockContentDependencyCollector.php

src/EventSubscriber/LayoutBuilderComponentDepencyCollector/BlockContentDependencyCollector.php
<?php

namespace Drupal\depcalc\EventSubscriber\LayoutBuilderComponentDepencyCollector;

use Drupal\block_content\Plugin\Block\BlockContentBlock;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\depcalc\DependencyCalculatorEvents;
use Drupal\depcalc\Event\SectionComponentDependenciesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Calculates dependencies of Block content.
 *
 * Subscribes to layout builder dependency collection to extract block
 * content dependencies.
 */
class BlockContentDependencyCollector implements EventSubscriberInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * BlockContentDependencyCollector constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    $events[DependencyCalculatorEvents::SECTION_COMPONENT_DEPENDENCIES_EVENT][] = ['onCalculateSectionComponentDependencies'];
    return $events;
  }

  /**
   * Calculates the entities referenced on Layout Builder components.
   *
   * @param \Drupal\depcalc\Event\SectionComponentDependenciesEvent $event
   *   The dependency calculation event.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function onCalculateSectionComponentDependencies(SectionComponentDependenciesEvent $event) {

    $component = $event->getComponent();
    $plugin = $component->getPlugin();

    if (!$plugin instanceof BlockContentBlock) {
      return;
    }

    $block_uuid = $plugin->getDerivativeId();
    $entities = $this->entityTypeManager->getStorage('block_content')->loadByProperties(['uuid' => $block_uuid]);
    $entity = array_shift($entities);
    $event->addEntityDependency($entity);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc