You are here

public function View::onDependencyRemoval in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Entity/View.php \Drupal\views\Entity\View::onDependencyRemoval()
  2. 9 core/modules/views/src/Entity/View.php \Drupal\views\Entity\View::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()

File

core/modules/views/src/Entity/View.php, line 480

Class

View
Defines a View configuration entity class.

Namespace

Drupal\views\Entity

Code

public function onDependencyRemoval(array $dependencies) {
  $changed = FALSE;

  // Don't intervene if the views module is removed.
  if (isset($dependencies['module']) && in_array('views', $dependencies['module'])) {
    return FALSE;
  }

  // If the base table for the View is provided by a module being removed, we
  // delete the View because this is not something that can be fixed manually.
  $views_data = Views::viewsData();
  $base_table = $this
    ->get('base_table');
  $base_table_data = $views_data
    ->get($base_table);
  if (!empty($base_table_data['table']['provider']) && in_array($base_table_data['table']['provider'], $dependencies['module'])) {
    return FALSE;
  }
  $current_display = $this
    ->getExecutable()->current_display;
  $handler_types = Views::getHandlerTypes();

  // Find all the handlers and check whether they want to do something on
  // dependency removal.
  foreach ($this->display as $display_id => $display_plugin_base) {
    $this
      ->getExecutable()
      ->setDisplay($display_id);
    $display = $this
      ->getExecutable()
      ->getDisplay();
    foreach (array_keys($handler_types) as $handler_type) {
      $handlers = $display
        ->getHandlers($handler_type);
      foreach ($handlers as $handler_id => $handler) {
        if ($handler instanceof DependentWithRemovalPluginInterface) {
          if ($handler
            ->onDependencyRemoval($dependencies)) {

            // Remove the handler and indicate we made changes.
            unset($this->display[$display_id]['display_options'][$handler_types[$handler_type]['plural']][$handler_id]);
            $changed = TRUE;
          }
        }
      }
    }
  }

  // Disable the View if we made changes.
  // @todo https://www.drupal.org/node/2832558 Give better feedback for
  // disabled config.
  if ($changed) {

    // Force a recalculation of the dependencies if we made changes.
    $this
      ->getExecutable()->current_display = NULL;
    $this
      ->calculateDependencies();
    $this
      ->disable();
  }
  $this
    ->getExecutable()
    ->setDisplay($current_display);
  return $changed;
}