You are here

public static function DynamicEntityReferenceItem::calculateDependencies in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem::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 EntityReferenceItem::calculateDependencies

See also

\Drupal\Core\Config\Entity\ConfigDependencyManager

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

File

src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php, line 530

Class

DynamicEntityReferenceItem
Defines the 'dynamic_entity_reference' entity field type.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldType

Code

public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
  $dependencies = FieldItemBase::calculateDependencies($field_definition);
  $entity_repository = \Drupal::service('entity.repository');
  if ($default_value = $field_definition
    ->getDefaultValueLiteral()) {
    foreach ($default_value as $value) {
      if (is_array($value) && isset($value['target_uuid']) && isset($value['target_type'])) {
        $entity = $entity_repository
          ->loadEntityByUuid($value['target_type'], $value['target_uuid']);

        // If the entity does not exist do not create the dependency.
        // @see \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceFieldItemList::processDefaultValue()
        if ($entity) {
          $dependencies[$entity
            ->getEntityType()
            ->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.
  $entity_type_manager = \Drupal::entityTypeManager();
  $settings = $field_definition
    ->getSettings();
  foreach (static::getTargetTypes($settings) as $target_type) {
    $handler = $settings[$target_type]['handler_settings'];
    if (!empty($handler['target_bundles'])) {
      $target_entity_type = $entity_type_manager
        ->getDefinition($target_type);
      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;
}