You are here

public function Calendar::preRender in Calendar 8

Allow the style to do stuff before each row is rendered.

Parameters

$result: The full array of results from the query.

Overrides RowPluginBase::preRender

File

src/Plugin/views/row/Calendar.php, line 334

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 preRender($result) {

  // Preload each entity used in this view from the cache. This provides all
  // the entity values relatively cheaply, and we don't need to do it
  // repeatedly for the same entity if there are multiple results for one
  // entity.
  $ids = [];

  /** @var \Drupal\views\ResultRow $row */
  foreach ($result as $row) {

    // Use the entity id as the key so we don't create more than one value per
    // entity.
    $entity = $row->_entity;

    // Node revisions need special loading.
    if (isset($this->view
      ->getBaseTables()['node_revision'])) {
      $this->entities[$entity
        ->id()] = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->loadRevision($entity
        ->id());
    }
    else {
      $ids[$entity
        ->id()] = $entity
        ->id();
    }
  }
  $base_tables = $this->view
    ->getBaseTables();
  $base_table = key($base_tables);
  $table_data = Views::viewsData()
    ->get($base_table);
  $this->entityType = $table_data['table']['entity type'];
  if (!empty($ids)) {
    $this->entities = \Drupal::entityTypeManager()
      ->getStorage($this->entityType)
      ->loadMultiple($ids);
  }

  // Identify the date argument and fields that apply to this view. Preload
  // the Date Views field info for each field, keyed by the field name, so we
  // know how to retrieve field values from the cached node.
  // @todo don't hardcode $date_fields, use viewsData() or viewsDataHelper()
  //   $data = date_views_fields($this->view->base_table);
  //   $data = $data['name'];
  $data = CalendarHelper::dateViewFields($this->entityType);
  $data = $data['name'];
  $date_fields = [];

  /** @var \Drupal\views\Plugin\views\argument\Formula $handler */
  foreach ($this->view
    ->getDisplay()
    ->getHandlers('argument') as $handler) {
    if ($handler instanceof Date) {

      // Strip "_calendar" from the field name.
      $fieldName = $handler->realField;
      $alias = $handler->table . '.' . $fieldName;
      $info = $data[$alias];
      $field_name = str_replace([
        '_value2',
        '_value',
      ], '', $info['real_field_name']);
      $date_fields[$field_name] = $info;
      $this->dateArgument = $handler;
      $this->dateFields = $date_fields;
    }
  }

  //
  //    Get the language for this view.
  //    $this->language = $this->display->handler
  //    ->get_option('field_language');
  //    $substitutions = views_views_query_substitutions($this->view);
  //    if (array_key_exists($this->language, $substitutions)) {
  //    $this->language = $substitutions[$this->language];
  //    }.
}