You are here

public function ContentEntity::viewItem in Search API 8

Returns the render array for the provided item and view mode.

Parameters

\Drupal\Core\TypedData\ComplexDataInterface $item: The item to render.

string $view_mode: (optional) The view mode that should be used to render the item.

string|null $langcode: (optional) For which language the item should be rendered. Defaults to the language the item has been loaded in.

Return value

array A render array for displaying the item.

Overrides DatasourcePluginBase::viewItem

File

src/Plugin/search_api/datasource/ContentEntity.php, line 999

Class

ContentEntity
Represents a datasource which exposes the content entities.

Namespace

Drupal\search_api\Plugin\search_api\datasource

Code

public function viewItem(ComplexDataInterface $item, $view_mode, $langcode = NULL) {
  try {
    if ($entity = $this
      ->getEntity($item)) {
      $langcode = $langcode ?: $entity
        ->language()
        ->getId();
      return $this
        ->getEntityTypeManager()
        ->getViewBuilder($this
        ->getEntityTypeId())
        ->view($entity, $view_mode, $langcode);
    }
  } catch (\Exception $e) {

    // The most common reason for this would be a
    // \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException in
    // getViewBuilder(), because the entity type definition doesn't specify a
    // view_builder class.
  }
  return [];
}