You are here

public function Calendar::render in Calendar 8

Same name in this branch
  1. 8 src/Plugin/views/style/Calendar.php \Drupal\calendar\Plugin\views\style\Calendar::render()
  2. 8 src/Plugin/views/row/Calendar.php \Drupal\calendar\Plugin\views\row\Calendar::render()

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/Calendar.php, line 405

Class

Calendar
Plugin which creates a view on the resulting object and formats it as a Calendar entity.

Namespace

Drupal\calendar\Plugin\views\row

Code

public function render($row) {

  /** @var \Drupal\calendar\CalendarDateInfo $dateInfo */
  $dateInfo = $this->dateArgument->view->dateInfo;
  $id = $row->_entity
    ->id();
  $rows = [];
  if (!is_numeric($id)) {
    return [];
  }

  // Unrelated to end date: this addresses issue where an entity on a calendar
  // is duplicated if it has multiple entity references; ensure that the
  // calendar entity is only displayed once.
  static $used = '';
  if ($id != $used) {
    $used = $id;
  }
  else {
    return [];
  }

  // There could be more than one date field in a view so iterate through all
  // of them to find the right values for this view result.
  foreach ($this->dateFields as $field_name => $info) {

    // Clone this entity so we can change it's values without altering other
    // occurrences of this entity on the same page, for example in an
    // "Upcoming" block.

    /** @var \Drupal\Core\Entity\ContentEntityBase $entity */
    $entity = clone $this->entities[$id];
    if (empty($entity)) {
      return [];
    }
    $event = new CalendarEvent($entity);

    // Retrieve the field value(s) that matched our query
    // from the cached node. Find the date and set it to the right timezone.
    $entity->date_id = [];
    $item_start_date = NULL;
    $item_end_date = NULL;
    $granularity = 'month';
    $increment = 1;

    // @todo implement timezone support
    // use $dateInfo to get field(s) used as arguments
    $entity_field_name = str_replace('_value', '', $dateInfo
      ->getDateArgument()->realField);
    $field_definition = $entity
      ->getFieldDefinition($entity_field_name);
    if ($field_definition instanceof BaseFieldDefinition) {
      $storage_format = 'U';
    }
    else {
      $datetime_type = $field_definition
        ->getSetting('datetime_type');
      if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) {
        $storage_format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
      }
      else {
        $storage_format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
      }
    }
    $items = $entity
      ->get($field_name)
      ->getValue();

    // // @todo handle timezones
    $timezone = new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE);

    // $db_tz   = date_get_timezone_db($tz_handling, isset($item->$tz_field)
    // ? $item->$tz_field : timezone_name_get($dateInfo->getTimezone()));
    // $to_zone = date_get_timezone($tz_handling, isset($item->$tz_field)
    // ? $item->$tz_field : timezone_name_get($dateInfo->getTimezone()));
    // $item_start_date = new dateObject($item, $db_tz);
    $event_date_value = isset($row->{$info['query_name']}) ? $row->{$info['query_name']} : $row->_entity
      ->get($entity_field_name)
      ->getString();
    foreach ($items as $item) {

      // For each date on the entity create a new event in the calendar.
      $event = clone $event;
      if (isset($item)) {
        $item_start_date = \DateTime::createFromFormat($storage_format, $item['value'], $timezone);
      }
      if (isset($item) && !empty($item['end_value'])) {
        $item_end_date = \DateTime::createFromFormat($storage_format, $item['end_value'], $timezone);
      }
      else {
        $item_end_date = $item_start_date;
      }
      $entity->date_id = [
        'calendar.' . $id . '.' . $field_name . '.0',
      ];

      // If we don't have a date value, go no further.
      if (empty($item_start_date)) {
        continue;
      }

      // Set the item date to the proper display timezone;.
      // @todo handle timezones
      //   $item_start_date->setTimezone(new dateTimezone($to_zone));
      //   $item_end_date->setTimezone(new dateTimezone($to_zone));
      $event
        ->setStartDate($item_start_date);
      $event
        ->setEndDate($item_end_date);
      $event
        ->setTimezone(new \DateTimeZone(timezone_name_get($dateInfo
        ->getTimezone())));
      $event
        ->setGranularity($granularity);

      // @todo remove while properties get transfered to the new object
      //   $event_container = new stdClass();
      //   $event_container->db_tz = $db_tz;
      //   $event_container->to_zone = $to_zone;
      //   $event_container->increment = $increment;
      //   $event_container->field = $is_field ? $item : NULL;
      //   $event_container->row = $row;
      //   $event_container->entity = $entity;
      // All calendar row plugins should provide
      // a date_id that the theme can use.
      // @todo implement
      $event->date_id = $entity->date_id[0];

      // We are working with an array of partially rendered items
      // as we process the calendar, so we can group and organize them.
      // At the end of our processing we'll need to swap in the fully
      // formatted display of the row. We save it here and switch it in
      // template_preprocess_calendar_item().
      // @FIXME
      // theme() has been renamed to _theme() and should NEVER be called
      // directly. Calling _theme() directly can alter the expected
      // output and potentially introduce security issues
      // (see https://www.drupal.org/node/2195739).
      // You should use renderable arrays instead.
      //
      //
      // @see https://www.drupal.org/node/2195739
      // $event->rendered = theme($this->theme_functions(),
      // [
      // 'view' => $this->view,
      // 'options' => $this->options,
      // 'row' => $row,
      // 'field_alias' => isset($this->field_alias) ? $this->field_alias : '',
      // ]);

      /** @var \Drupal\calendar\CalendarEvent[] $events */
      $events = $this
        ->explodeValues($event);
      foreach ($events as $event) {
        switch ($this->options['colors']['legend']) {
          case 'type':
            if ($event
              ->getEntityTypeId() == 'node') {
              $this
                ->nodeTypeStripe($event);
            }
            break;
          case 'taxonomy':
            $this
              ->calendarTaxonomyStripe($event);
            break;
        }
        $rows[] = $event;
      }
    }
  }
  return $rows;
}