/** * Processes results from the load query and returns a list of values. * * When an entity is loaded, the values might derive from multiple graph. * This function will process the results and attempt to load a published * version of the entity. * If there is no published version available, then it will fallback to the * rest of the graphs. * * If the graph parameter can be used to restrict the available graphs to load * from. * * The results array is an array of loaded entity values from different * graphs. * @code * $results = [ * 'http://entity_id.uri' => [ * 'http://field.mapping.uri' => [ * 'x-default' => [ * 0 => 'actual value' * ] * ] * ]; * @code * * @param array $results * A set of query results indexed per graph and entity id. * * @return array * The entity values indexed by the field mapping id. * * @throws \Exception * Thrown when the entity graph is empty. */ protected function processGraphResults($results) { $mapping = $this->mappingHandler->getEntityPredicates($this->entityTypeId); // If no graphs are passed, fetch all available graphs derived from the // results. $values_per_entity = []; foreach ($results as $result) { $entity_id = (string) $result->entity_id; $entity_graphs[$entity_id] = (string) $result->graph; $lang = LanguageInterface::LANGCODE_DEFAULT; if ($result->field_value instanceof \EasyRdf_Literal) { $lang_temp = $result->field_value->getLang(); if ($lang_temp) { $lang = $lang_temp; } } $values_per_entity[$entity_id][(string) $result->graph][(string) $result->predicate][$lang][] = (string) $result->field_value; } if (empty($values_per_entity)) { return NULL; } $return = []; foreach ($values_per_entity as $entity_id => $values_per_graph) { $request_graphs = $this->getGraphHandler()->getRequestGraphs($entity_id); $entity_graph_uris = $this->getGraphHandler()->getEntityTypeGraphUris($this->getEntityType()->getBundleEntityType()); foreach ($request_graphs as $priority_graph) { foreach ($values_per_graph as $graph_uri => $entity_values) { if (isset($return[$entity_id]) || array_search($graph_uri, array_column($entity_graph_uris, $priority_graph)) === FALSE) { continue; } // First determine the bundle of the returned entity. $bundle_predicates = $this->bundlePredicate; $pred_set = FALSE; foreach ($bundle_predicates as $bundle_predicate) { if (isset($entity_values[$bundle_predicate])) { $pred_set = TRUE; } } if (!$pred_set) { continue; } /** @var \Drupal\rdf_entity\Entity\RdfEntityType $bundle */ $bundle = $this->getActiveBundle($entity_values); if (!$bundle) { continue; } // Check if the graph checked is in the request graphs. // If there are multiple graphs set, probably the default is requested // with the rest as fallback or it is a neutral call. // If the default is requested, it is going to be first in line so in // any case, use the first one. $graph_id = $this->getGraphHandler()->getBundleGraphId($this->entityType->getBundleEntityType(), $bundle->id(), $graph_uri); // Map bundle and entity id. $return[$entity_id][$this->bundleKey][LanguageInterface::LANGCODE_DEFAULT] = $bundle->id(); $return[$entity_id][$this->idKey][LanguageInterface::LANGCODE_DEFAULT] = $entity_id; $return[$entity_id]['graph'][LanguageInterface::LANGCODE_DEFAULT] = $graph_id; $rdf_type = NULL; foreach ($entity_values as $predicate => $field) { // If not mapped, ignore. if (!isset($mapping[$bundle->id()][$predicate])) { continue; } $field_name = $mapping[$bundle->id()][$predicate]['field_name']; $column = $mapping[$bundle->id()][$predicate]['column']; foreach ($field as $lang => $items) { foreach ($items as $item) { if (!isset($return[$entity_id][$field_name]) || !is_string($return[$entity_id][$field_name][$lang])) { $return[$entity_id][$field_name][$lang][][$column] = $item; } if (!isset($return[$entity_id][$field_name][LanguageInterface::LANGCODE_DEFAULT])) { $return[$entity_id][$field_name][LanguageInterface::LANGCODE_DEFAULT][][$column] = $item; } } if (isset($mapping[$bundle->id()][$predicate]['storage_definition'])) { $storage_definition = $mapping[$bundle->id()][$predicate]['storage_definition']; $this->applyFieldDefaults($storage_definition, $return[$entity_id][$storage_definition->getName()][$lang]); } } } } } } return $return; }