class EntityReferenceFieldDependencyCollector in Dependency Calculation 8
Subscribes to dependency collection to extract referenced entities.
Hierarchy
- class \Drupal\depcalc\EventSubscriber\DependencyCollector\BaseDependencyCollector implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- class \Drupal\depcalc\EventSubscriber\DependencyCollector\EntityReferenceFieldDependencyCollector
Expanded class hierarchy of EntityReferenceFieldDependencyCollector
1 string reference to 'EntityReferenceFieldDependencyCollector'
1 service uses EntityReferenceFieldDependencyCollector
File
- src/
EventSubscriber/ DependencyCollector/ EntityReferenceFieldDependencyCollector.php, line 15
Namespace
Drupal\depcalc\EventSubscriber\DependencyCollectorView source
class EntityReferenceFieldDependencyCollector extends BaseDependencyCollector {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[DependencyCalculatorEvents::CALCULATE_DEPENDENCIES][] = [
'onCalculateDependencies',
];
return $events;
}
/**
* Calculates the referenced entities.
*
* @param \Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event
* The dependency calculation event.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
$entity = $event
->getEntity();
if ($entity instanceof ContentEntityInterface) {
$fields = FieldExtractor::getFieldsFromEntity($entity, [
$this,
'fieldCondition',
]);
foreach ($fields as $field) {
foreach ($field as $item) {
if (!$item->entity) {
$sub_entity = \Drupal::entityTypeManager()
->getStorage($field
->getFieldDefinition()
->getSetting('target_type'))
->load($item
->getValue()['target_id']);
if (is_null($sub_entity)) {
continue;
}
$item->entity = $sub_entity;
}
$item_entity_wrapper = new DependentEntityWrapper($item->entity);
$local_dependencies = [];
$this
->mergeDependencies($item_entity_wrapper, $event
->getStack(), $this
->getCalculator()
->calculateDependencies($item_entity_wrapper, $event
->getStack(), $local_dependencies));
$event
->addDependency($item_entity_wrapper);
}
}
}
}
/**
* 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(), [
'file',
'image',
'entity_reference',
'entity_reference_revisions',
]);
}
}
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. | |
EntityReferenceFieldDependencyCollector:: |
public | function | Determines if the field is of one of the specified types. | |
EntityReferenceFieldDependencyCollector:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
EntityReferenceFieldDependencyCollector:: |
public | function | Calculates the referenced entities. |