You are here

public function SearchApiDate::render_item in Search API 8

Renders a single item of a row.

\Drupal\views\Plugin\views\field\Date::render() assumes values are always timestamps, so here we make sure this is indeed the case.

Parameters

int $count: The index of the item inside the row.

mixed $item: The item for the field to render.

Return value

string The rendered output.

Overrides MultiItemsFieldHandlerInterface::render_item

See also

\Drupal\views\Plugin\views\field\MultiItemsFieldHandlerInterface::render_item()

\Drupal\views\Plugin\views\field\Date::render()

File

src/Plugin/views/field/SearchApiDate.php, line 40

Class

SearchApiDate
Handles the display of date fields in Search API Views.

Namespace

Drupal\search_api\Plugin\views\field

Code

public function render_item($count, $item) {
  if (!empty($item['value']) && !is_numeric($item['value'])) {
    try {
      $timezone = new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE);
      $date_time = new \DateTime($item['value'], $timezone);
      $item['value'] = $date_time
        ->getTimestamp();
    } catch (\Exception $e) {
      $this
        ->logException($e, '%type while trying to parse date value (Views field "@field_id"): @message in %function (line %line of %file).', [
        '@field_id' => $this->options['id'],
      ]);
      return NULL;
    }
  }
  return $this
    ->trait_render_item($count, $item);
}