You are here

public function SearchApiRow::render in Search API 8

Render a row object. This usually passes through to a theme template of some form, but not always.

Parameters

object $row: A single row of the query result, so an element of $view->result.

Return value

string The rendered output of a single row, used by the style plugin.

Overrides RowPluginBase::render

File

src/Plugin/views/row/SearchApiRow.php, line 184

Class

SearchApiRow
Provides a row plugin for displaying a result as a rendered item.

Namespace

Drupal\search_api\Plugin\views\row

Code

public function render($row) {
  $datasource_id = $row->search_api_datasource;
  if (!$row->_object 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 '';
  }
  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';
  try {
    $build = $this->index
      ->getDatasource($datasource_id)
      ->viewItem($row->_object, $view_mode);

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