You are here

public static function DynamicEntityReferenceItem::onDependencyRemoval 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::onDependencyRemoval()

Informs the plugin that a dependency of the field will be deleted.

Parameters

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

array $dependencies: An array of dependencies that will be deleted keyed by dependency type. Dependency types are, for example, entity, module and theme.

Return value

bool TRUE if the field definition has been changed as a result, FALSE if not.

Overrides EntityReferenceItem::onDependencyRemoval

See also

\Drupal\Core\Config\ConfigEntityInterface::onDependencyRemoval()

File

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

Class

DynamicEntityReferenceItem
Defines the 'dynamic_entity_reference' entity field type.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldType

Code

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

        // @see \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceFieldItemList::processDefaultValue()
        if ($entity && isset($dependencies[$entity
          ->getConfigDependencyKey()][$entity
          ->getConfigDependencyName()])) {
          unset($default_value[$key]);
          $changed = TRUE;
        }
      }
    }
    if ($changed) {
      $field_definition
        ->setDefaultValue($default_value);
    }
  }
  $entity_type_manager = \Drupal::entityTypeManager();

  // Update the 'target_bundles' handler setting if a bundle config dependency
  // has been removed.
  $settings = $field_definition
    ->getSettings();
  foreach (static::getTargetTypes($settings) as $target_type) {
    $bundles_changed = FALSE;
    $handler_settings = $settings[$target_type]['handler_settings'];
    if (!empty($handler_settings['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_settings['target_bundles']) as $bundle) {
            if (isset($dependencies[$bundle
              ->getConfigDependencyKey()][$bundle
              ->getConfigDependencyName()])) {
              unset($handler_settings['target_bundles'][$bundle
                ->id()]);

              // If this bundle is also used in the 'auto_create_bundle'
              // setting, disable the auto-creation feature completely.
              $auto_create_bundle = !empty($handler_settings['auto_create_bundle']) ? $handler_settings['auto_create_bundle'] : FALSE;
              if ($auto_create_bundle && $auto_create_bundle == $bundle
                ->id()) {
                $handler_settings['auto_create'] = FALSE;
                $handler_settings['auto_create_bundle'] = NULL;
              }
              $bundles_changed = TRUE;

              // In case we deleted the only target bundle allowed by the
              // field we have to log a critical message because the field
              // will not function correctly anymore.
              if ($handler_settings['target_bundles'] === []) {
                \Drupal::logger('dynamic_entity_reference')
                  ->critical('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name dynamic entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [
                  '%target_bundle' => $bundle
                    ->label(),
                  '%target_entity_type' => $bundle
                    ->getEntityType()
                    ->getBundleOf(),
                  '%field_name' => $field_definition
                    ->getName(),
                  '%entity_type' => $field_definition
                    ->getTargetEntityTypeId(),
                  '%bundle' => $field_definition
                    ->getTargetBundle(),
                ]);
              }
            }
          }
        }
      }
    }
    if ($bundles_changed) {
      $settings[$target_type]['handler_settings'] = $handler_settings;
      $field_definition
        ->setSettings($settings);
    }
    $changed |= $bundles_changed;
  }
  return $changed;
}