You are here

public function SearchApiRenderedItem::render in Search API 8

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/SearchApiRenderedItem.php, line 126

Class

SearchApiRenderedItem
Handles rendering an entity in a certain view mode in Search API Views.

Namespace

Drupal\search_api\Plugin\views\field

Code

public function render(ResultRow $row) {
  if (!($row->_object ?? NULL) instanceof ComplexDataInterface) {
    $context = [
      '%item_id' => $row->search_api_id,
      '%view' => $this->view->storage
        ->label(),
    ];
    $this
      ->getLogger()
      ->warning('Failed to load item %item_id in view %view.', $context);
    return '';
  }
  $datasource_id = $row->search_api_datasource;
  if (!$this->index
    ->isValidDatasource($datasource_id)) {
    $context = [
      '%datasource' => $datasource_id,
      '%view' => $this->view->storage
        ->label(),
    ];
    $this
      ->getLogger()
      ->warning('Item of unknown datasource %datasource returned in view %view.', $context);
    return '';
  }

  // Always use the default view mode if it was not set explicitly in the
  // options.
  $bundle = $this->index
    ->getDatasource($datasource_id)
    ->getItemBundle($row->_object);
  $view_mode = $this->options['view_modes'][$datasource_id][$bundle] ?? 'default';
  if ($view_mode === '') {
    return '';
  }
  try {
    $build = $this->index
      ->getDatasource($datasource_id)
      ->viewItem($row->_object, $view_mode);
    if ($build) {

      // Add the excerpt to the render array to allow adding it to view modes.
      $build['#search_api_excerpt'] = $row->_item
        ->getExcerpt();
    }
    return $build;
  } catch (SearchApiException $e) {
    $this
      ->logException($e);
    return '';
  }
}