<?php

/**
 * @file
 * preprocess-views.php
 *
 * Define all view preprocess HOOKs.
 */

/**
 * Implements hook_preprocess_views_view().
 */
function naomi_2025_preprocess_views_view(&$variables) {
  /** @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];
  $view_style_plugin = $view->style_plugin;
  $view_plugin_id = $view_style_plugin->getPluginId();
  $view_id = $view->id();
  $view_current_display = $view->current_display;

  // Key is view machine name.
  // Value is array of displays in that view.
  $views_to_skip_animation = [
    'content_stories' => [
      'attachment_featured',
    ],
  ];

  if (isset($views_to_skip_animation[$view_id])) {
    $displays_to_skip = $views_to_skip_animation[$view_id];

    if (in_array($view_current_display, $displays_to_skip)) {
      $variables['attributes']['data-animator-optout'] = TRUE;
    }
  }

  // Remove the pager if there are no items.
  if (!$view?->pager?->total_items) {
    unset($variables['pager']);
  }

  $view_formats = [
    'grid' => 'grid',
    'html_list' => 'list',
    'table' => 'table',
    'default' => 'unformatted',
  ];

  $variables['format'] = $view_formats[$view_plugin_id];

  if ($view_plugin_id == 'grid') {
    if (isset($view_style_plugin->options['columns'])) {
      $variables['columns'] = $view_style_plugin->options['columns'];
    }
  }

  // For all story views that have a more link, move it into the view header.
  if ($view_id === 'content_stories') {
    if (isset($variables['more'])) {
      $variables['header'][] = $variables['more'];
      unset($variables['more']);
    }
  }
}
