You are here

protected function TimelineJS::buildDate in Views TimelineJS integration 8.3

Builds a timeline date from the current data row.

Parameters

string $field: The machine name of the date field.

Return value

\Drupal\views_timelinejs\TimelineJS\Date|null A date object or NULL if the start date could not be parsed.

2 calls to TimelineJS::buildDate()
TimelineJS::buildEra in src/Plugin/views/style/TimelineJS.php
Builds a timeline era from the current views data row.
TimelineJS::buildSlide in src/Plugin/views/style/TimelineJS.php
Builds a timeline slide from the current views data row.

File

src/Plugin/views/style/TimelineJS.php, line 526

Class

TimelineJS
Style plugin to render items as TimelineJS3 slides.

Namespace

Drupal\views_timelinejs\Plugin\views\style

Code

protected function buildDate($field) {
  try {
    $date_markup = $this
      ->getField($this->view->row_index, $field);
    if (empty($date_markup)) {
      return NULL;
    }

    // Store the date string so that it can be used in the error message, if
    // necessary.  Strip HTML tags from dates so users don't run into problems
    // like Date fields wrapping their output with metadata.
    $date_string = strip_tags($date_markup
      ->__toString());
    $date = new Date($date_string);
  } catch (Exception $e) {

    // Return NULL if the field didn't contain a parseable date string.
    // @todo: Implement a logger.
    $this
      ->messenger()
      ->addMessage($this
      ->t('The date "@date" does not conform to a <a href="@php-manual">PHP supported date and time format</a>.', [
      '@date' => $date_string,
      '@php-manual' => 'http://php.net/manual/en/datetime.formats.php',
    ]));
    $date = NULL;
  }
  return $date;
}