You are here

public function RulesComponentRepository::getMultiple in Rules 8.3

Gets the components for the given IDs.

Parameters

string[] $ids: The IDs of the components to get. The supported IDs depend on the given provider. For the default provider 'rules' the entity IDs of component configs may be passed.

string $resolver: The resolver of the component. See ::get() for a list of supported resolvers.

Return value

\Drupal\rules\Engine\RulesComponent[] An array of components, keyed by component ID.

Throws

\Drupal\rules\Exception\InvalidArgumentException Thrown if an unsupported provider is given.

Overrides RulesComponentRepositoryInterface::getMultiple

1 call to RulesComponentRepository::getMultiple()
RulesComponentRepository::get in src/Engine/RulesComponentRepository.php
Gets the component for the given ID.

File

src/Engine/RulesComponentRepository.php, line 84

Class

RulesComponentRepository
Provides an implementation of the component repository service.

Namespace

Drupal\rules\Engine

Code

public function getMultiple(array $ids, $provider = 'rules_component') {
  if (!isset($this->resolvers[$provider])) {
    throw new InvalidArgumentException("Invalid component provider {$provider} given.");
  }
  $cids = [];
  foreach ($ids as $id) {
    $cids[$id] = "{$provider}:{$id}:{$this->langcode}";
  }
  $results = array_intersect_key($this->components, array_flip($cids));
  $cids_missing = array_diff_assoc($cids, array_keys($results));
  if ($cids_missing) {

    // Note that the cache backend removes resolved IDs.
    $cache_results = $this->cacheBackend
      ->getMultiple($cids_missing);
    foreach ($cache_results as $cid => $cache_result) {
      $this->components[$cid] = $cache_result->data;
      $results[$cid] = $cache_result->data;
    }
    if ($cids_missing) {
      $resolved_results = $this->resolvers[$provider]
        ->getMultiple(array_keys($cids_missing));
      if ($resolved_results) {
        $cache_items = [];
        foreach ($resolved_results as $id => $component) {
          $cid = $cids[$id];
          $this->components[$cid] = $component;
          $results[$cid] = $component;
          $cache_items[$cid]['data'] = $component;

          // Set tags so that we can use invalidateTags later when needed.
          $cache_items[$cid]['tags'] = [
            $id,
          ];
        }

        // Cache entries to speed up future lookups.
        $this->cacheBackend
          ->setMultiple($cache_items);
      }
    }
  }
  return $results;
}