You are here

public function SearchApiFieldTrait::getEntity in Search API 8

Gets the entity matching the current row and relationship.

Parameters

\Drupal\views\ResultRow $values: An object containing all retrieved values.

Return value

\Drupal\Core\Entity\EntityInterface Returns the entity matching the values.

See also

\Drupal\views\Plugin\views\field\FieldHandlerInterface::getEntity()

1 call to SearchApiFieldTrait::getEntity()
SearchApiFieldTrait::getItemUrl in src/Plugin/views/field/SearchApiFieldTrait.php
Retrieves an alter options array for linking the given value to its item.
1 method overrides SearchApiFieldTrait::getEntity()
SearchApiBulkForm::getEntity in src/Plugin/views/field/SearchApiBulkForm.php
Gets the entity matching the current row and relationship.

File

src/Plugin/views/field/SearchApiFieldTrait.php, line 381

Class

SearchApiFieldTrait
Provides a trait to use for Search API Views field handlers.

Namespace

Drupal\search_api\Plugin\views\field

Code

public function getEntity(ResultRow $values) {
  $combined_property_path = $this
    ->getCombinedPropertyPath();
  list($datasource_id, $property_path) = Utility::splitCombinedId($combined_property_path);
  if ($values->search_api_datasource !== $datasource_id) {
    return NULL;
  }
  $value_index = $this->valueIndex;

  // Only try two levels. Otherwise, we might end up at an entirely different
  // entity, cause we go too far up.
  $levels = 2;
  while ($levels--) {
    if (!empty($values->_relationship_objects[$combined_property_path][$value_index])) {

      /** @var \Drupal\Core\TypedData\TypedDataInterface $object */
      $object = $values->_relationship_objects[$combined_property_path][$value_index];
      $value = $object
        ->getValue();
      if ($value instanceof EntityInterface) {
        return $value;
      }
    }
    if (!$property_path) {
      break;
    }

    // For multi-valued fields, the parent's index is not the same as the
    // field value's index.
    if (!empty($values->_relationship_parent_indices[$combined_property_path][$value_index])) {
      $value_index = $values->_relationship_parent_indices[$combined_property_path][$value_index];
    }
    list($property_path) = Utility::splitPropertyPath($property_path);
    $combined_property_path = $this
      ->createCombinedPropertyPath($datasource_id, $property_path);
  }
  return NULL;
}