You are here

public function EntityDisplayBase::onDependencyRemoval in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityDisplayBase.php \Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval()

Informs the entity that entities it depends on will be deleted.

This method allows configuration entities to remove dependencies instead of being deleted themselves. Configuration entities can use this method to avoid being unnecessarily deleted during an extension uninstallation. For example, entity displays remove references to widgets and formatters if the plugin that supplies them depends on a module that is being uninstalled.

If this method returns TRUE then the entity needs to be re-saved by the caller for the changes to take effect. Implementations should not save the entity.

Parameters

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 entity has been changed as a result, FALSE if not.

Overrides ConfigEntityBase::onDependencyRemoval

See also

\Drupal\Core\Config\Entity\ConfigDependencyManager

\Drupal\Core\Config\ConfigEntityBase::preDelete()

\Drupal\Core\Config\ConfigManager::uninstall()

\Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval()

1 call to EntityDisplayBase::onDependencyRemoval()
LayoutBuilderEntityViewDisplay::onDependencyRemoval in core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php
Informs the entity that entities it depends on will be deleted.
1 method overrides EntityDisplayBase::onDependencyRemoval()
LayoutBuilderEntityViewDisplay::onDependencyRemoval in core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php
Informs the entity that entities it depends on will be deleted.

File

core/lib/Drupal/Core/Entity/EntityDisplayBase.php, line 467

Class

EntityDisplayBase
Provides a common base class for entity view and form displays.

Namespace

Drupal\Core\Entity

Code

public function onDependencyRemoval(array $dependencies) {
  $changed = parent::onDependencyRemoval($dependencies);
  foreach ($dependencies['config'] as $entity) {
    if ($entity
      ->getEntityTypeId() == 'field_config') {

      // Remove components for fields that are being deleted.
      $this
        ->removeComponent($entity
        ->getName());
      unset($this->hidden[$entity
        ->getName()]);
      $changed = TRUE;
    }
  }
  foreach ($this
    ->getComponents() as $name => $component) {
    if ($renderer = $this
      ->getRenderer($name)) {
      if (in_array($renderer
        ->getPluginDefinition()['provider'], $dependencies['module'])) {

        // Revert to the defaults if the plugin that supplies the widget or
        // formatter depends on a module that is being uninstalled.
        $this
          ->setComponent($name);
        $changed = TRUE;
      }

      // Give this component the opportunity to react on dependency removal.
      $component_removed_dependencies = $this
        ->getPluginRemovedDependencies($renderer
        ->calculateDependencies(), $dependencies);
      if ($component_removed_dependencies) {
        if ($renderer
          ->onDependencyRemoval($component_removed_dependencies)) {

          // Update component settings to reflect changes.
          $component['settings'] = $renderer
            ->getSettings();
          $component['third_party_settings'] = [];
          foreach ($renderer
            ->getThirdPartyProviders() as $module) {
            $component['third_party_settings'][$module] = $renderer
              ->getThirdPartySettings($module);
          }
          $this
            ->setComponent($name, $component);
          $changed = TRUE;
        }

        // If there are unresolved deleted dependencies left, disable this
        // component to avoid the removal of the entire display entity.
        if ($this
          ->getPluginRemovedDependencies($renderer
          ->calculateDependencies(), $dependencies)) {
          $this
            ->removeComponent($name);
          $arguments = [
            '@display' => (string) $this
              ->getEntityType()
              ->getLabel(),
            '@id' => $this
              ->id(),
            '@name' => $name,
          ];
          $this
            ->getLogger()
            ->warning("@display '@id': Component '@name' was disabled because its settings depend on removed dependencies.", $arguments);
          $changed = TRUE;
        }
      }
    }
  }
  return $changed;
}