You are here

public function Search::onDependencyRemoval in Search API Autocomplete 8

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

src/Entity/Search.php, line 511

Class

Search
Describes the autocomplete settings for a certain search.

Namespace

Drupal\search_api_autocomplete\Entity

Code

public function onDependencyRemoval(array $dependencies) {
  $changed = parent::onDependencyRemoval($dependencies);
  $plugins = $this
    ->getAllPlugins();
  $dependency_data = $this
    ->getDependencyData();

  // Make sure our dependency data has the exact same keys as $dependencies,
  // to simplify the subsequent code.
  $dependencies = array_filter($dependencies);
  $dependency_data = array_intersect_key($dependency_data, $dependencies);
  $dependency_data += array_fill_keys(array_keys($dependencies), []);
  $call_on_removal = [];
  foreach ($dependencies as $dependency_type => $dependency_objects) {

    // Annoyingly, modules and theme dependencies come not keyed by dependency
    // name here, while entities do. Flip the array for modules and themes to
    // make the code simpler.
    if (in_array($dependency_type, [
      'module',
      'theme',
    ])) {
      $dependency_objects = array_flip($dependency_objects);
    }
    $dependency_data[$dependency_type] = array_intersect_key($dependency_data[$dependency_type], $dependency_objects);
    foreach ($dependency_data[$dependency_type] as $name => $dependency_sources) {

      // We first remove all the "hard" dependencies.
      if (!empty($dependency_sources['always'])) {
        foreach ($dependency_sources['always'] as $plugin_type => $type_plugins) {

          // We can hardly remove the search entity itself.
          if ($plugin_type == 'entity') {
            continue;
          }

          // Otherwise, we need to remove the plugin in question.
          $changed = TRUE;
          $plugins[$plugin_type] = array_diff_key($plugins[$plugin_type], $type_plugins);
        }
      }

      // Then, collect all the optional ones.
      if (!empty($dependency_sources['optional'])) {

        // However this plays out, it will lead to a change.
        $changed = TRUE;
        foreach ($dependency_sources['optional'] as $plugin_type => $type_plugins) {

          // Only include those plugins that have not already been removed.
          $type_plugins = array_intersect_key($type_plugins, $plugins[$plugin_type]);
          foreach ($type_plugins as $plugin_id => $plugin) {
            $call_on_removal[$plugin_type][$plugin_id][$dependency_type][$name] = $dependency_objects[$name];
          }
        }
      }
    }
  }

  // Now for all plugins with optional dependencies (stored in
  // $call_on_removal, mapped to their removed dependencies) call their
  // onDependencyRemoval() methods.
  foreach ($call_on_removal as $plugin_type => $type_plugins) {
    foreach ($type_plugins as $plugin_id => $plugin_dependencies) {
      $plugin = $plugins[$plugin_type][$plugin_id];
      $removal_successful = $plugin
        ->onDependencyRemoval($plugin_dependencies);
      if (!$removal_successful) {
        unset($plugins[$plugin_type][$plugin_id]);
      }
    }
  }

  // If we had to remove the search plugin, the search entity cannot be
  // "rescued". Return FALSE in this case, which will cause the entity to be
  // deleted.
  if (empty($plugins['search_plugin'])) {
    return FALSE;
  }

  // If there are no suggesters left, we just disable the search.
  if (empty($plugins['suggesters'])) {
    $this
      ->disable();
  }

  // In case we removed any suggesters, set our suggesters to the remaining
  // ones. (If we didn't remove any, this is a no-op.) Since the plugins
  // already changed their configuration, if necessary, those changes should
  // be propagated automatically when saving via preSave().
  $this
    ->setSuggesters($plugins['suggesters']);
  return $changed;
}