You are here

public function LiveResults::onDependencyRemoval in Search API Autocomplete 8

Informs the plugin that some of its dependencies are being removed.

The plugin should attempt to change its configuration in a way to remove its dependency on those items. However, to avoid problems, it should (as far as possible) not add any new dependencies in the process, since there is no guarantee that those are not currently being removed, too.

Parameters

object[][] $dependencies: An array of dependencies, keyed by dependency type ("module", "config", etc.) and dependency name.

Return value

bool Whether the dependency was successfully removed from the plugin – that is, after the configuration changes that were made, none of the removed items are dependencies of this plugin anymore.

Overrides ConfigurablePluginBase::onDependencyRemoval

File

src/Plugin/search_api_autocomplete/suggester/LiveResults.php, line 422

Class

LiveResults
Provides a suggester plugin that displays live results.

Namespace

Drupal\search_api_autocomplete\Plugin\search_api_autocomplete\suggester

Code

public function onDependencyRemoval(array $dependencies) {

  // All dependencies of this suggester are entity view modes, so we go
  // through all of the view modes set for specific bundles and remove all
  // those which have been removed (setting them back to "Use only label").
  // First, we collect all view mode dependencies keyed by entity ID, to make
  // this easier.
  $removed_view_modes = [];
  $non_view_mode_dependencies = FALSE;
  foreach ($dependencies as $objects) {
    foreach ($objects as $object) {
      if ($object instanceof EntityInterface && $object
        ->getEntityTypeId() === 'entity_view_mode') {
        $removed_view_modes[$object
          ->id()] = TRUE;
      }
      else {
        $non_view_mode_dependencies = TRUE;
      }
    }
  }

  // Then, we go through all bundle settings and look for those removed view
  // modes.
  $index = $this
    ->getSearch()
    ->getIndex();
  foreach ($this->configuration['view_modes'] as $datasource_id => $bundles) {
    $datasource = $index
      ->getDatasource($datasource_id);
    $entity_type_id = $datasource
      ->getEntityTypeId();

    // If the datasource doesn't represent an entity type, we unfortunately
    // can't know what dependencies its view modes might have.
    if (!$entity_type_id) {
      continue;
    }
    foreach ($bundles as $bundle => $view_mode) {
      if ($view_mode === '') {
        continue;
      }
      $view_mode_entity_id = $entity_type_id . '.' . $view_mode;
      if (!empty($removed_view_modes[$view_mode_entity_id])) {
        $this->configuration['view_modes'][$datasource_id][$bundle] = '';
      }
    }
  }

  // This will have successfully dealt with all affected dependencies unless
  // non-view mode dependencies (perhaps set by the parent class) were
  // involved.
  return !$non_view_mode_dependencies;
}