You are here

public function TimelineJS::render in Views TimelineJS integration 8.3

Render the display in this style.

Overrides StylePluginBase::render

File

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

Class

TimelineJS
Style plugin to render items as TimelineJS3 slides.

Namespace

Drupal\views_timelinejs\Plugin\views\style

Code

public function render() {

  // Return if the start date field mapping is not configured.
  if (empty($this->options['timeline_fields']['start_date'])) {
    $this
      ->messenger()
      ->addWarning(t('The Start date field mapping must be configured in the TimelineJS format settings before any slides or eras can be rendered.'));
    return;
  }
  $timeline = new Timeline();

  // Render the fields.  If it isn't done now then the row_index will be unset
  // the first time that getField() is called, resulting in an undefined
  // property exception.
  $this
    ->renderFields($this->view->result);

  // Render slide arrays from the views data.
  foreach ($this->view->result as $row_index => $row) {
    $this->view->row_index = $row_index;

    // Determine the type of timeline entity to build.
    $type = 'event';
    if ($this->options['timeline_fields']['type']) {
      $type = $this
        ->getField($row_index, $this->options['timeline_fields']['type']);
    }
    switch ($type) {
      case 'title':
      case 'timeline_title_slide':
        $slide = $this
          ->buildTitleSlide();

        // Ensure the slide was built.
        if (!empty($slide)) {
          $timeline
            ->setTitleSlide($slide);
        }
        break;
      case 'era':
      case 'timeline_era':
        $era = $this
          ->buildEra();

        // Ensure the era was built.
        if (!empty($era)) {
          $timeline
            ->addEra($era);
        }
        break;
      default:
        $slide = $this
          ->buildSlide();

        // Ensure the slide was built.
        if (!empty($slide)) {
          $timeline
            ->addEvent($slide);
        }
    }
  }
  unset($this->view->row_index);

  // Skip theming if the view is being edited or previewed.
  if ($this->view->preview) {
    return '<pre>' . print_r($timeline
      ->buildArray(), 1) . '</pre>';
  }

  // Prepare the options array.
  $this
    ->prepareTimelineOptions();
  return [
    '#theme' => $this
      ->themeFunctions(),
    '#view' => $this->view,
    '#options' => [
      'timeline_options' => $this->options['timeline_config'],
      'timeline_font' => $this->options['additional_config']['font'],
    ],
    '#rows' => $timeline
      ->buildArray(),
  ];
}