timeline_styles-8.x-1.x-dev/timeline_styles.module
timeline_styles.module
<?php
/**
* @file
* Contains theme hook implementations and view customization for the module.
*/
use Drupal\views\ViewExecutable;
/**
* Implements hook_theme().
*/
function timeline_styles_theme($existing, $type, $theme, $path) {
return [
'timeline_style' => [
'variables' => ['view' => NULL, 'rows' => NULL],
'path' => \Drupal::service('extension.list.module')->getPath('timeline_styles') . '/templates',
],
'timeline_style_image' => [
'variables' => ['view' => NULL, 'rows' => NULL],
'path' => \Drupal::service('extension.list.module')->getPath('timeline_styles') . '/templates',
],
'views_view_fields__timeline_styles__page_1' => [
'template' => 'views-view-fields--timeline-styles--page-1',
'base hook' => 'views_view_fields',
],
];
}
/**
* Adds the global styling library to the view render array.
*/
function timeline_styles_views_pre_render(ViewExecutable $view) {
$view->element['#attached']['library'][] = 'timeline_styles/global-styling';
}
/**
* Prepares variables for views timeline_styles template.
*
* Template: timeline-styles.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The view object.
* - rows: An array of row items. Each row is an array of content.
*/
function template_preprocess_timeline_styles(&$variables) {
$view = $variables['view'];
$rows = $variables['rows'];
$variables['nav'] = [];
$field = $view->style_plugin->options['timeline_style_field'];
if (!$field || !isset($view->field[$field])) {
template_preprocess_views_view_unformatted($variables);
return;
}
$nav = [];
foreach ($rows as $id => $row) {
$nav[$id] = [
'#theme' => 'views_view_field',
'#view' => $view,
'#field' => $view->field[$field],
'#row' => $row['#row'],
];
}
template_preprocess_views_view_unformatted($variables);
$variables['nav'] = $nav;
}
/**
* Prepares variables for views timeline_styles_image template.
*
* Template: timeline-styles-image.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The view object.
* - rows: An array of row items. Each row is an array of content.
*/
function template_preprocess_timeline_styles_image(&$variables) {
$view = $variables['view'];
$rows = $variables['rows'];
$variables['nav'] = [];
// Safely check if the field option is set.
if (
!isset($view->style_plugin->options['timeline_style_image']) ||
!$view->style_plugin->options['timeline_style_image']
) {
template_preprocess_views_view_unformatted($variables);
return;
}
$field = $view->style_plugin->options['timeline_style_image'];
// Check if the specified field exists in the view.
if (!isset($view->field[$field])) {
template_preprocess_views_view_unformatted($variables);
return;
}
$nav = [];
foreach ($rows as $id => $row) {
$nav[$id] = [
'#theme' => 'views_view_field',
'#view' => $view,
'#field' => $view->field[$field],
'#row' => $row['#row'],
];
}
template_preprocess_views_view_unformatted($variables);
$variables['nav'] = $nav;
}
