You are here

public static function EntityReferenceItem::calculateDependencies in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::calculateDependencies()

Calculates dependencies for field items.

Dependencies are saved in the field configuration entity and are used to determine configuration synchronization order. For example, if the field type's default value is a content entity, this method should return an array of dependencies listing the content entities.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition.

Return value

array An array of dependencies grouped by type (config, content, module, theme). For example:

array(
  'config' => array(
    'user.role.anonymous',
    'user.role.authenticated',
  ),
  'content' => array(
    'node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d',
  ),
  'module' => array(
    'node',
    'user',
  ),
  'theme' => array(
    'seven',
  ),
);

Overrides FieldItemBase::calculateDependencies

See also

\Drupal\Core\Config\Entity\ConfigDependencyManager

\Drupal\Core\Config\Entity\ConfigEntityInterface::getConfigDependencyName()

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php, line 483

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
  $dependencies = parent::calculateDependencies($field_definition);
  $entity_type_manager = \Drupal::entityTypeManager();
  $target_entity_type = $entity_type_manager
    ->getDefinition($field_definition
    ->getFieldStorageDefinition()
    ->getSetting('target_type'));

  // Depend on default values entity types configurations.
  if ($default_value = $field_definition
    ->getDefaultValueLiteral()) {
    $entity_repository = \Drupal::service('entity.repository');
    foreach ($default_value as $value) {
      if (is_array($value) && isset($value['target_uuid'])) {
        $entity = $entity_repository
          ->loadEntityByUuid($target_entity_type
          ->id(), $value['target_uuid']);

        // If the entity does not exist do not create the dependency.
        // @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
        if ($entity) {
          $dependencies[$target_entity_type
            ->getConfigDependencyKey()][] = $entity
            ->getConfigDependencyName();
        }
      }
    }
  }

  // Depend on target bundle configurations. Dependencies for 'target_bundles'
  // also covers the 'auto_create_bundle' setting, if any, because its value
  // is included in the 'target_bundles' list.
  $handler = $field_definition
    ->getSetting('handler_settings');
  if (!empty($handler['target_bundles'])) {
    if ($bundle_entity_type_id = $target_entity_type
      ->getBundleEntityType()) {
      if ($storage = $entity_type_manager
        ->getStorage($bundle_entity_type_id)) {
        foreach ($storage
          ->loadMultiple($handler['target_bundles']) as $bundle) {
          $dependencies[$bundle
            ->getConfigDependencyKey()][] = $bundle
            ->getConfigDependencyName();
        }
      }
    }
  }
  return $dependencies;
}