You are here

protected function Search::getDependencyData in Search API Autocomplete 8

Retrieves data about this search entity's dependencies.

The return value is structured as follows:

[
  'config' => [
    'CONFIG_DEPENDENCY_KEY' => [
      'always' => [
        'search_plugin' => [
          'SEARCH_ID' => $search_plugin,
        ],
        'suggesters' => [
          'SUGGESTER_ID_1' => $suggester_1,
          'SUGGESTER_ID_2' => $suggester_2,
        ],
      ],
      'optional' => [
        'entity' => [
          'SEARCH_ID' => $search,
        ],
      ],
    ],
  ],
];

Enforced dependencies are not included in this method's return value.

Return value

object[][][][][] An associative array containing the search's dependencies. The array is first keyed by the config dependency type ("module", "config", etc.) and then by the names of the config dependencies of that type which the index has. The values are associative arrays with up to two keys, "always" and "optional", specifying whether the dependency is a hard one by the plugin (or entity) in question or potentially depending on the configuration. The values on this level are arrays with keys "entity", "search_plugin" and/or "suggesters" and values arrays of IDs mapped to their entities or plugins.

2 calls to Search::getDependencyData()
Search::calculateDependencies in src/Entity/Search.php
Calculates dependencies and stores them in the dependency property.
Search::onDependencyRemoval in src/Entity/Search.php
Informs the entity that entities it depends on will be deleted.

File

src/Entity/Search.php, line 638

Class

Search
Describes the autocomplete settings for a certain search.

Namespace

Drupal\search_api_autocomplete\Entity

Code

protected function getDependencyData() {
  $dependency_data = [];

  // Since calculateDependencies() will work directly on the $dependencies
  // property, we first save its original state and then restore it
  // afterwards.
  $original_dependencies = $this->dependencies;
  parent::calculateDependencies();
  unset($this->dependencies['enforced']);
  foreach ($this->dependencies as $dependency_type => $list) {
    foreach ($list as $name) {
      $dependency_data[$dependency_type][$name]['always']['entity'][$this->id] = $this;
    }
  }
  $this->dependencies = $original_dependencies;

  // Include the dependency to the search index.
  if ($this
    ->hasValidIndex()) {
    $name = $this
      ->getIndex()
      ->getConfigDependencyName();
    $dependency_data['config'][$name]['always']['entity'][$this->id] = $this;
  }

  // All other plugins can be treated uniformly.
  foreach ($this
    ->getAllPlugins() as $plugin_type => $type_plugins) {
    foreach ($type_plugins as $plugin_id => $plugin) {

      // Largely copied from
      // \Drupal\Core\Plugin\PluginDependencyTrait::calculatePluginDependencies().
      $definition = $plugin
        ->getPluginDefinition();

      // First, always depend on the module providing the plugin.
      $dependency_data['module'][$definition['provider']]['always'][$plugin_type][$plugin_id] = $plugin;

      // Plugins can declare additional dependencies in their definition.
      if (isset($definition['config_dependencies'])) {
        foreach ($definition['config_dependencies'] as $dependency_type => $list) {
          foreach ($list as $name) {
            $dependency_data[$dependency_type][$name]['always'][$plugin_type][$plugin_id] = $plugin;
          }
        }
      }

      // Finally, add the dynamically-calculated dependencies of the plugin.
      foreach ($plugin
        ->calculateDependencies() as $dependency_type => $list) {
        foreach ($list as $name) {
          $dependency_data[$dependency_type][$name]['optional'][$plugin_type][$plugin_id] = $plugin;
        }
      }
    }
  }
  return $dependency_data;
}