public function loadItemsMultiple()

in modules/quanthub_indicator/src/Entity/QuanthubIndex.php [185:245]


  public function loadItemsMultiple(array $item_ids) {
    // Group the requested items by datasource. This will also later be used to
    // determine whether all items were loaded successfully.
    $items_by_datasource = [];
    foreach ($item_ids as $item_id) {
      [$datasource_id, $raw_id] = Utility::splitCombinedId($item_id);
      $items_by_datasource[$datasource_id][$raw_id] = $item_id;
    }

    // Load the items from the datasources and keep track of which were
    // successfully retrieved.
    $items = [];
    foreach ($items_by_datasource as $datasource_id => $raw_ids) {
      try {
        $datasource = $this->getDatasource($datasource_id);
        $datasource_items = $datasource->loadMultiple(array_keys($raw_ids));
        foreach ($datasource_items as $raw_id => $item) {
          $id = $raw_ids[$raw_id];
          $items[$id] = $item;
          // Remember that we successfully loaded this item.
          unset($items_by_datasource[$datasource_id][$raw_id]);
        }
      }
      catch (SearchApiException $e) {
        $this->logException($e);
        // If the complete datasource could not be loaded, don't report all its
        // individual requested items as missing.
        unset($items_by_datasource[$datasource_id]);
      }
    }

    // Check whether there are requested items that couldn't be loaded.
    $items_by_datasource = array_filter($items_by_datasource);
    if ($items_by_datasource) {
      // Extract the second-level values of the two-dimensional array (that is,
      // the combined item IDs) and log a warning reporting their absence.
      $missing_ids = array_reduce(array_map('array_values', $items_by_datasource), 'array_merge', []);

      $filtered_missing_ids = [];
      foreach ($missing_ids as $missing_id) {
        if (!str_contains($missing_id, 'indicator')) {
          $filtered_missing_ids[] = $missing_id;
        }
      }

      if (!empty($filtered_missing_ids)) {
        $args['%index'] = $this->label();
        $args['@items'] = '"' . implode('", "', $filtered_missing_ids) . '"';
        $this->getLogger()
          ->warning('Could not load the following items on index %index: @items.', $args);
        // Also remove those items from tracking so we don't keep trying to load
        // them.
      }
    }
    foreach ($items_by_datasource as $datasource_id => $raw_ids) {
      $this->trackItemsDeleted($datasource_id, array_keys($raw_ids));
    }

    // Return the loaded items.
    return $items;
  }