You are here

class EntityReferenceFieldDependencyCollector in Dependency Calculation 8

Subscribes to dependency collection to extract referenced entities.

Hierarchy

Expanded class hierarchy of EntityReferenceFieldDependencyCollector

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

File

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

Namespace

Drupal\depcalc\EventSubscriber\DependencyCollector
View 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

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.
EntityReferenceFieldDependencyCollector::fieldCondition public function Determines if the field is of one of the specified types.
EntityReferenceFieldDependencyCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntityReferenceFieldDependencyCollector::onCalculateDependencies public function Calculates the referenced entities.