You are here

public static function EntityReferenceRevisionsItem::onDependencyRemoval in Entity Reference Revisions 8

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/EntityReferenceRevisionsItem.php, line 424

Class

EntityReferenceRevisionsItem
Defines the 'entity_reference_revisions' entity field type.

Namespace

Drupal\entity_reference_revisions\Plugin\Field\FieldType

Code

public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {
  $changed = FALSE;
  $entity_type_manager = \Drupal::entityTypeManager();
  $target_entity_type = $entity_type_manager
    ->getDefinition($field_definition
    ->getFieldStorageDefinition()
    ->getSetting('target_type'));
  $handler_settings = $field_definition
    ->getSetting('handler_settings');

  // Update the 'target_bundles' handler setting if a bundle config dependency
  // has been removed.
  if (!empty($handler_settings['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_settings['target_bundles']) as $bundle) {
          if (isset($dependencies[$bundle
            ->getConfigDependencyKey()][$bundle
            ->getConfigDependencyName()])) {
            unset($handler_settings['target_bundles'][$bundle
              ->id()]);
            $changed = TRUE;

            // In case we deleted the only target bundle allowed by the field
            // we can log a message because the behaviour of the field will
            // have changed.
            if ($handler_settings['target_bundles'] === []) {
              \Drupal::logger('entity_reference_revisions')
                ->notice('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name entity reference revisions field (entity_type: %entity_type, bundle: %bundle) no longer specifies a specific target bundle. The field will now accept any bundle and may need 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 ($changed) {
    $field_definition
      ->setSetting('handler_settings', $handler_settings);
  }
  return $changed;
}