W3cubDocs

/Drupal 8

public function EntityDisplayBase::onDependencyRemoval

public EntityDisplayBase::onDependencyRemoval(array $dependencies)

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()

File

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

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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityDisplayBase.php/function/EntityDisplayBase::onDependencyRemoval/8.1.x