You are here

class BlockContentDependencyCollector in Dependency Calculation 8

Subscribes to layout builder dependency collection to extract block content dependencies.

Hierarchy

  • class \Drupal\depcalc\EventSubscriber\LayoutBuilderComponentDepencyCollector\BlockContentDependencyCollector implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of BlockContentDependencyCollector

1 string reference to 'BlockContentDependencyCollector'
depcalc.services.yml in ./depcalc.services.yml
depcalc.services.yml
1 service uses BlockContentDependencyCollector
block_content.layout_builder_dependency_calculator in ./depcalc.services.yml
Drupal\depcalc\EventSubscriber\LayoutBuilderComponentDepencyCollector\BlockContentDependencyCollector

File

src/EventSubscriber/LayoutBuilderComponentDepencyCollector/BlockContentDependencyCollector.php, line 14

Namespace

Drupal\depcalc\EventSubscriber\LayoutBuilderComponentDepencyCollector
View source
class BlockContentDependencyCollector implements EventSubscriberInterface {

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

  /**
   * TextItemFieldDependencyCollector 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() {
    $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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockContentDependencyCollector::$entityTypeManager protected property The entity type manager.
BlockContentDependencyCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
BlockContentDependencyCollector::onCalculateSectionComponentDependencies public function Calculates the entities referenced on Layout Builder components.
BlockContentDependencyCollector::__construct public function TextItemFieldDependencyCollector constructor.