You are here

class LayoutBuilderFieldDependencyCollector in Dependency Calculation 8

Subscribes to dependency collection to extract entities referenced on Layout Builder components.

Hierarchy

Expanded class hierarchy of LayoutBuilderFieldDependencyCollector

1 string reference to 'LayoutBuilderFieldDependencyCollector'
depcalc.services.yml in ./depcalc.services.yml
depcalc.services.yml
1 service uses LayoutBuilderFieldDependencyCollector
layout_builder.dependency_calculator in ./depcalc.services.yml
Drupal\depcalc\EventSubscriber\DependencyCollector\LayoutBuilderFieldDependencyCollector

File

src/EventSubscriber/DependencyCollector/LayoutBuilderFieldDependencyCollector.php, line 15

Namespace

Drupal\depcalc\EventSubscriber\DependencyCollector
View source
class LayoutBuilderFieldDependencyCollector extends LayoutBuilderDependencyCollectorBase {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[DependencyCalculatorEvents::CALCULATE_DEPENDENCIES][] = [
      'onCalculateDependencies',
    ];
    return $events;
  }

  /**
   * Calculates the entities referenced on Layout Builder components.
   *
   * @param \Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event
   *   The dependency calculation event.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
    if (!$this->layoutPluginManager) {
      return;
    }
    $entity = $event
      ->getEntity();
    if (!$entity instanceof ContentEntityInterface) {
      return;
    }
    $fields = FieldExtractor::getFieldsFromEntity($entity, [
      $this,
      'fieldCondition',
    ]);
    foreach ($fields as $field) {
      foreach ($field as $item) {
        $section = $item
          ->getValue()['section'];
        $this
          ->addSectionDependencies($event, $section);
        $this
          ->addComponentDependencies($event, $section
          ->getComponents());
      }
    }
  }

  /**
   * Determines if the field is of one of the specified types.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity.
   * @param string $field_name
   *   The field name.
   * @param \Drupal\Core\Field\FieldItemListInterface $field
   *   The field.
   *
   * @return bool
   *   Whether the field type is one of the specified ones.
   */
  public function fieldCondition(ContentEntityInterface $entity, $field_name, FieldItemListInterface $field) {
    return in_array($field
      ->getFieldDefinition()
      ->getType(), [
      'layout_section',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseDependencyCollector::getCalculator protected function Gets the dependency calculator.
BaseDependencyCollector::mergeDependencies protected function Properly adds dependencies and their modules to a wrapper object.
LayoutBuilderDependencyCollectorBase::$dispatcher protected property The event dispatcher.
LayoutBuilderDependencyCollectorBase::$layoutPluginManager protected property The layout plugin manager.
LayoutBuilderDependencyCollectorBase::addComponentDependencies protected function Adds dependencies from components.
LayoutBuilderDependencyCollectorBase::addSectionComponentEntityDependencies protected function Adds entity dependencies from this layout builder field to this event dependencies.
LayoutBuilderDependencyCollectorBase::addSectionDependencies protected function Set module dependencies to the event.
LayoutBuilderDependencyCollectorBase::__construct public function The LayoutBuilderDependencyCollectorBase constructor.
LayoutBuilderFieldDependencyCollector::fieldCondition public function Determines if the field is of one of the specified types.
LayoutBuilderFieldDependencyCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
LayoutBuilderFieldDependencyCollector::onCalculateDependencies public function Calculates the entities referenced on Layout Builder components.