class LayoutBuilderFieldDependencyCollector in Dependency Calculation 8
Subscribes to dependency collection to extract entities referenced on Layout Builder components.
Hierarchy
- class \Drupal\depcalc\EventSubscriber\DependencyCollector\BaseDependencyCollector implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- class \Drupal\depcalc\EventSubscriber\LayoutBuilderComponentDepencyCollector\LayoutBuilderDependencyCollectorBase
- class \Drupal\depcalc\EventSubscriber\DependencyCollector\LayoutBuilderFieldDependencyCollector
- class \Drupal\depcalc\EventSubscriber\LayoutBuilderComponentDepencyCollector\LayoutBuilderDependencyCollectorBase
Expanded class hierarchy of LayoutBuilderFieldDependencyCollector
1 string reference to 'LayoutBuilderFieldDependencyCollector'
1 service uses LayoutBuilderFieldDependencyCollector
File
- src/
EventSubscriber/ DependencyCollector/ LayoutBuilderFieldDependencyCollector.php, line 15
Namespace
Drupal\depcalc\EventSubscriber\DependencyCollectorView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BaseDependencyCollector:: |
protected | function | Gets the dependency calculator. | |
BaseDependencyCollector:: |
protected | function | Properly adds dependencies and their modules to a wrapper object. | |
LayoutBuilderDependencyCollectorBase:: |
protected | property | The event dispatcher. | |
LayoutBuilderDependencyCollectorBase:: |
protected | property | The layout plugin manager. | |
LayoutBuilderDependencyCollectorBase:: |
protected | function | Adds dependencies from components. | |
LayoutBuilderDependencyCollectorBase:: |
protected | function | Adds entity dependencies from this layout builder field to this event dependencies. | |
LayoutBuilderDependencyCollectorBase:: |
protected | function | Set module dependencies to the event. | |
LayoutBuilderDependencyCollectorBase:: |
public | function | The LayoutBuilderDependencyCollectorBase constructor. | |
LayoutBuilderFieldDependencyCollector:: |
public | function | Determines if the field is of one of the specified types. | |
LayoutBuilderFieldDependencyCollector:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
LayoutBuilderFieldDependencyCollector:: |
public | function | Calculates the entities referenced on Layout Builder components. |