views_timeline-1.0.x-dev/views_timeline.module
views_timeline.module
<?php
/**
* @file
* Provide views data for views_timeline.module.
*/
use Drupal\views_timeline\Plugin\views\style\ViewsTimeline;
/**
* Implements hook_preprocess_views_view().
*/
function views_timeline_preprocess_views_view(array &$variables) {
$view = $variables['view'];
// Check if the view uses the specific StylePluginBase.
if ($view->style_plugin instanceof ViewsTimeline) {
$style_plugin = $variables['view']->style_plugin->options;
// Add values from the style plugin to the Twig variables.
$variables['views_timeline_title'] = $style_plugin['views_timeline_title'] ?? '';
$variables['views_timeline_icon'] = $style_plugin['views_timeline_icon'] ?? '';
$variables['views_timeline_date'] = $style_plugin['views_timeline_date'] ?? '';
$variables['views_timeline_description1'] = $style_plugin['views_timeline_description1'] ?? '';
$variables['views_timeline_description2'] = $style_plugin['views_timeline_description2'] ?? '';
$variables['views_timeline_description3'] = $style_plugin['views_timeline_description3'] ?? '';
$variables['views_timeline_description4'] = $style_plugin['views_timeline_description4'] ?? '';
$views_timeline_layout = $style_plugin['views_timeline_layout'] ?? '';
$views_timeline_palette = $style_plugin['views_timeline_palette'] ?? '';
$variables['#attached']['library'][] = 'views_timeline/default';
if ($views_timeline_layout) {
$variables['#attached']['library'][] = 'views_timeline/layout-' . $views_timeline_layout;
}
if ($views_timeline_palette == 'custom') {
// Get the colors from the $style_plugin,
// or set to default if not available.
$timeline_primary_color = $style_plugin['timeline_primary_color'] ?? '#2D3A46';
$timeline_primary_date_color = $style_plugin['timeline_primary_date_color'] ?? '#fff';
$timeline_primary_heading_color = $style_plugin['timeline_primary_heading_color'] ?? '#fff';
$timeline_secondary_color = $style_plugin['timeline_secondary_color'] ?? '#2D3A46';
$timeline_secondary_date_color = $style_plugin['timeline_secondary_date_color'] ?? '#fff';
$timeline_secondary_heading_color = $style_plugin['timeline_secondary_heading_color'] ?? '#fff';
$timeline_accent_color = $style_plugin['timeline_accent_color'] ?? '#ECECEC';
$timeline_content_color = $style_plugin['timeline_content_color'] ?? '#ECECEC';
$timeline_content_dark_color = $style_plugin['timeline_content_dark_color'] ?? '#ECECEC';
// Build the CSS dynamically with the variables.
$css = "
:root {
--primary-color: $timeline_primary_color;
--primary-date-color: $timeline_primary_date_color;
--primary-heading-color: $timeline_primary_heading_color;
--secondary-color: $timeline_secondary_color;
--secondary-date-color: $timeline_secondary_date_color;
--secondary-heading-color: $timeline_secondary_heading_color;
--accent-color: $timeline_accent_color;
--timeline-content-color: $timeline_content_color;
--timeline-content-dark-color: $timeline_content_dark_color;
}
";
// Attach the CSS to the page.
$variables['#attached']['html_head'][] = [
[
'#tag' => 'style',
'#value' => $css,
'#attributes' => ['type' => 'text/css'],
],
'custom-timeline-root-colors',
];
}
else {
$variables['#attached']['library'][] = 'views_timeline/color-palettes';
}
}
}
/**
* Prepares variables for the Views Timeline.
*/
function template_preprocess_views_view_views_timeline(&$variables) {
$view = $variables['view'];
$style = $view->style_plugin;
$style_plugin = $style->options;
// Define timeline data using the default operator, reducing redundancy.
$timeline_data = [
'views_timeline_title' => $style_plugin['views_timeline_title'] ?? '',
'views_timeline_icon' => $style_plugin['views_timeline_icon'] ?? '',
'views_timeline_date' => $style_plugin['views_timeline_date'] ?? '',
'views_timeline_description1' => $style_plugin['views_timeline_description1'] ?? '',
'views_timeline_description2' => $style_plugin['views_timeline_description2'] ?? '',
'views_timeline_description3' => $style_plugin['views_timeline_description3'] ?? '',
'views_timeline_description4' => $style_plugin['views_timeline_description4'] ?? '',
'views_timeline_layout' => $style_plugin['views_timeline_layout'] ?? '',
'views_timeline_palette' => $style_plugin['views_timeline_palette'] ?? '',
];
$variables['timeline_data'] = $timeline_data;
// Default to NULL for template to include.
$variables['template_to_include'] = NULL;
// Only proceed if there is a valid layout specified.
$views_timeline_layout = $timeline_data['views_timeline_layout'];
if ($views_timeline_layout) {
// Get the current active theme's path.
$theme = \Drupal::theme()->getActiveTheme();
$theme_path = $theme->getPath() . '/templates/views-timeline-layouts/layout-' . $views_timeline_layout . '.html.twig';
// Define the module template path.
$module_path = \Drupal::moduleHandler()->getModule('views_timeline')->getPath() . '/templates/views-timeline-layouts/layout-' . $views_timeline_layout . '.html.twig';
// Check for theme and module template files.
if (file_exists($theme_path)) {
$variables['template_to_include'] = '@' . $theme->getName() . '/views-timeline-layouts/layout-' . $views_timeline_layout . '.html.twig';
}
elseif (file_exists($module_path)) {
$variables['template_to_include'] = '@views_timeline/views-timeline-layouts/layout-' . $views_timeline_layout . '.html.twig';
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for views_view templates.
*/
function views_timeline_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
// Get the view name and display ID.
$view = $variables['view'];
// Check if the view uses the specific StylePluginBase.
if ($view->style_plugin instanceof ViewsTimeline) {
$name = $view->id();
$display = $view->current_display;
$suggestions[] = 'views_view__views_timeline';
$suggestions[] = 'views_view__views_timeline';
$suggestions[] = 'views_view__views_timeline__' . $name;
$suggestions[] = 'views_view__views_timeline__' . $name . '__' . $display;
}
}
/**
* Implements hook_theme_registry_alter() for Views templates.
*/
function views_timeline_theme_registry_alter(array &$theme_registry) {
$module_path = \Drupal::service('extension.list.module')->getPath('views_timeline');
// Add a custom template for a specific view.
$theme_registry['views_view__views_timeline'] = [
'type' => 'module',
// Custom template file.
'template' => 'views-view--views-timeline',
// The path to the templates directory.
'path' => $module_path . '/templates',
'preprocess functions' => [
'views_timeline_preprocess_views_view',
'template_preprocess_views_view',
'views_ui_preprocess_views_view',
'template_preprocess',
// Optional: preprocess function.
],
];
}
/**
* Defines available layouts for Views timeline templates.
*/
function views_timeline_layouts() {
// Define layouts using an associative array with ID => Label pairs.
return [
'one' => 'Layout One',
'two' => 'Layout Two',
'three' => 'Layout Three',
'four' => 'Layout Four',
'five' => 'Layout Five',
'six' => 'Layout Six',
'seven' => 'Layout Seven',
'eight' => 'Layout Eight',
'nine' => 'Layout Nine',
'ten' => 'Layout Ten',
'eleven' => 'Layout Eleven',
'twelve' => 'Layout Twelve',
'thirteen' => 'Layout Thirteen',
];
}
