You are here

function template_preprocess_views_blogspot_archive_view_archive in Views Blogspot Archive 8

Prepares variables for view templates.

Default template: views_blogspot_archive-view-archive.html.twig.

Parameters

array $variables: An associative array containing:

  • view: A ViewExecutable object.
  • rows: The raw row data.
  • options: An array of options. Each option contains:
    • separator: A string to be placed between inline fields to keep them visually distinct.

File

templates/views_blogspot_archive.theme.inc, line 28
Preprocessors and helper functions to make theming easier.

Code

function template_preprocess_views_blogspot_archive_view_archive(array &$variables) {
  $view = $variables['view'];
  $style = $view->style_plugin->options;
  $rows = $variables['rows'];
  $data = array();
  foreach ($rows as $row) {

    /** @var \Drupal\Core\Entity\Entity $entity */
    $entity = $row->_entity;
    if ($entity instanceof FieldableEntityInterface && $entity
      ->hasField($style['vba_field_name'])) {
      $data_value = $entity
        ->get($style['vba_field_name'])
        ->getValue();

      // Consider Only the first item in the field.
      $date = $data_value[0]['value'];
      try {
        if (!is_numeric($date)) {
          $dateField = $entity
            ->get($style['vba_field_name']);
          $datetimeType = $dateField
            ->getFieldDefinition()
            ->getSetting('datetime_type');
          $storageFormat = $datetimeType === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
          $date_obj = DrupalDateTime::createFromFormat($storageFormat, $date);
        }
        else {

          // Timestamp.
          $date_obj = DrupalDateTime::createFromTimestamp($date);
        }
        $data[$date_obj
          ->format('Y')][$date_obj
          ->format('m') . '::' . $date_obj
          ->format('F')][$entity
          ->id()] = $entity;
      } catch (Exception $exception) {

        // @todo Handle this.
      }
    }
  }

  // Now add count to year and months.
  $data = views_blogspot_archive_add_count($data);

  // Get the context of year-to-expand and month-to-expand.
  $year_to_expand = $month_to_expand = NULL;

  // Check for entity view or detail page.
  if ($entities = \Drupal::routeMatch()
    ->getParameters()) {
    foreach ($entities as $entity) {
      if (is_object($entity) && $entity instanceof FieldableEntityInterface && $entity
        ->hasField($style['vba_field_name'])) {
        $data_value = $entity
          ->get($style['vba_field_name'])
          ->getValue();

        // Consider Only the first item in the field.
        $date = $data_value[0]['value'];
        try {
          if (!is_numeric($date)) {
            $dateField = $entity
              ->get($style['vba_field_name']);
            $datetimeType = $dateField
              ->getFieldDefinition()
              ->getSetting('datetime_type');
            $storageFormat = $datetimeType === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
            $date_obj = DrupalDateTime::createFromFormat($storageFormat, $date);
          }
          else {

            // Timestamp.
            $date_obj = DrupalDateTime::createFromTimestamp($date);
          }
          $year_to_expand = $date_obj
            ->format('Y');
          $month_to_expand = $date_obj
            ->format('m');
        } catch (Exception $exception) {

          // @todo Handle this.
        }
      }
    }
  }

  // Check drupal internal pages(archive page) with query string.
  if ($year_to_expand == NULL || $month_to_expand == NULL) {
    $arg = \Drupal::request()->query;
    $context_route = \Drupal::request()->attributes
      ->get('_route');
    if ($context_route == $style['vba_style']['vba_view_name']) {
      $year_to_expand = $arg
        ->get('year');
      if ($arg
        ->get('month')) {
        $month_to_expand = date('m', mktime(0, 0, 0, $arg
          ->get('month'), 10));
      }
    }
  }
  if ($style['vba_style']['vba_use_result_page']) {
    $archiveItems = views_blogspot_archive_with_link($data, $year_to_expand, $month_to_expand, $style['vba_style']['vba_view_name']);
    $library = [];
  }
  else {
    $archiveItems = views_blogspot_archive_without_link($data);
    $library = [
      'library' => [
        'views_blogspot_archive/views_blogspot_archive',
      ],
    ];
  }
  $variables['views_blogspot_archive'] = [
    '#theme' => 'item_list',
    '#items' => $archiveItems,
    '#list_type' => 'ul',
    '#context' => 'list_style',
    '#cache' => [
      'contexts' => [
        'url.query_args:year',
        'url.query_args:month',
        'url.path',
      ],
    ],
    '#attributes' => [
      'class' => [
        'vba-archive',
      ],
    ],
    '#attached' => $library,
  ];
}