You are here

class FieldExtractor in Dependency Calculation 8

Class FieldExtractor.

Hierarchy

Expanded class hierarchy of FieldExtractor

7 files declare their use of FieldExtractor
DrupalMediaEmbedCollector.php in src/EventSubscriber/DependencyCollector/DrupalMediaEmbedCollector.php
EntityEmbedCollector.php in src/EventSubscriber/DependencyCollector/EntityEmbedCollector.php
EntityLanguage.php in src/EventSubscriber/DependencyCollector/EntityLanguage.php
EntityReferenceFieldDependencyCollector.php in src/EventSubscriber/DependencyCollector/EntityReferenceFieldDependencyCollector.php
LayoutBuilderFieldDependencyCollector.php in src/EventSubscriber/DependencyCollector/LayoutBuilderFieldDependencyCollector.php

... See full list

File

src/FieldExtractor.php, line 11

Namespace

Drupal\depcalc
View source
class FieldExtractor {

  /**
   * Extract all fields in all translations that match our criteria.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity.
   * @param callable $condition
   *   The condition.
   *
   * @return \Drupal\Core\Field\FieldItemListInterface[]
   *   The list of fields.
   */
  public static function getFieldsFromEntity(ContentEntityInterface $entity, callable $condition) {
    $fields = [];
    $languages = $entity
      ->getTranslationLanguages();

    /**
     * @var string $field_name
     * @var \Drupal\Core\Field\FieldItemListInterface $field
     */
    foreach ($entity as $field_name => $field) {

      // Check if field definition type is a link.
      if ($condition($entity, $field_name, $field)) {

        // If the field is translatable get all translations of it.
        if ($field
          ->getFieldDefinition()
          ->isTranslatable()) {
          foreach ($languages as $language) {
            $translated = $entity
              ->getTranslation($language
              ->getId());
            $fields[] = $translated
              ->get($field_name);
          }
        }
        else {
          $fields[] = $field;
        }
      }
    }
    if ($fields) {
      $event = new FilterDependencyCalculationFieldsEvent($entity, ...$fields);

      /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */
      $dispatcher = \Drupal::service('event_dispatcher');
      $dispatcher
        ->dispatch(DependencyCalculatorEvents::FILTER_FIELDS, $event);
      $fields = $event
        ->getFields();
    }
    return $fields;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldExtractor::getFieldsFromEntity public static function Extract all fields in all translations that match our criteria.