vvjl-1.0.3/vvjl.module
vvjl.module
<?php
/**
* @file
* Provides the module implementation for vvjl.
*
* Contains template preprocessing and theme definitions for Views.
*
* Filename: vvjl.module
* Website: https://www.flashwebcenter.com
* Description: template.
* Developer: Alaa Haddad https://www.alaahaddad.com.
*/
declare(strict_types=1);
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Markup;
use Drupal\views\ViewExecutable;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\vvjl\Plugin\views\style\ViewsVanillaJavascriptLightbox;
/**
* Implements hook_help().
*/
function vvjl_help(string $route_name, RouteMatchInterface $route_match): ?string {
if ($route_name === 'help.page.vvjl') {
return _vvjl_helper_render_readme();
}
return NULL;
}
/**
* Helper function to render README.md.
*
* @return string
* The rendered content of README.md.
*/
function _vvjl_helper_render_readme(): string {
$readme_path = __DIR__ . '/README.md';
$text = file_get_contents($readme_path);
if ($text === FALSE) {
return (string) t('README.md file not found.');
}
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . htmlspecialchars($text) . '</pre>';
}
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::config('markdown.settings')->getRawData();
$filter = $filter_manager->createInstance('markdown', ['settings' => $settings]);
return $filter->process($text, 'en')->getProcessedText();
}
/**
* Implements hook_theme().
*/
function vvjl_theme(array $existing, string $type, string $theme, string $path): array {
return [
'views_view_vvjl_fields' => [
'variables' => [
'view' => NULL,
'options' => [],
'row' => NULL,
'field_alias' => NULL,
'attributes' => [],
'title_attributes' => [],
'content_attributes' => [],
'title_prefix' => [],
'title_suffix' => [],
'fields' => [],
],
'template' => 'views-view-vvjl-fields',
'path' => $path . '/templates',
],
'views_view_vvjl' => [
'variables' => [
'view' => NULL,
'rows' => [],
'options' => [],
],
'template' => 'views-view-vvjl',
'path' => $path . '/templates',
],
];
}
/**
* Implements hook_preprocess_HOOK() for views_view_vvjl.
*/
function template_preprocess_views_view_vvjl(array &$variables): void {
static $views_theme_loaded = FALSE;
if (!$views_theme_loaded) {
\Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');
$views_theme_loaded = TRUE;
}
/** @var \Drupal\vvjl\Plugin\views\style\ViewsVanillaJavascriptLightbox $handler */
$handler = $variables['view']->style_plugin;
$list_attributes = [];
if (!empty($handler->options['grid_image_width'])) {
$list_attributes['data-grid-image-width'] = $handler->options['grid_image_width'];
}
if (!empty($handler->options['grid_image_gap'])) {
$list_attributes['data-grid-image-gap'] = $handler->options['grid_image_gap'];
}
if (!empty($handler->options['disable_overlay'])) {
$list_attributes['data-disable-overlay'] = $handler->options['disable_overlay'] ? 'true' : 'false';
}
if (!empty($handler->options['animation'])) {
$list_attributes['data-animation'] = $handler->options['animation'];
}
if (!empty($handler->options['overlay_color'])) {
$rgb = _vvjl_hex_to_rgb($handler->options['overlay_color']);
$opacity = $handler->options['overlay_opacity'] ?? 1;
$background_rgb = "rgba({$rgb['r']}, {$rgb['g']}, {$rgb['b']}, $opacity)";
$variables['background_rgb'] = $background_rgb;
}
else {
$variables['background_rgb'] = NULL;
}
$variables['list_attributes'] = new Attribute($list_attributes);
$variables['options'] = $handler->options;
$variables['settings'] = [
'view_id' => $variables['view']->dom_id,
'grid_image_width' => $handler->options['grid_image_width'],
'grid_image_gap' => $handler->options['grid_image_gap'],
'overlay_color' => $handler->options['overlay_color'],
'overlay_opacity' => $handler->options['overlay_opacity'],
];
if (!empty($variables['rows'])) {
foreach ($variables['rows'] as $key => $row) {
if (isset($row['#theme']) && is_array($row['#theme'])) {
foreach ($row['#theme'] as $idx => $theme_hook_suggestion) {
$variables['rows'][$key]['#theme'][$idx] = str_replace('views_view_fields', 'views_view_vvjl_fields', $theme_hook_suggestion);
}
}
}
}
template_preprocess_views_view_unformatted($variables);
}
/**
* Prepares variables for views_view_vvjl_fields template.
*
* @param array $variables
* An associative array containing:
* - view: The view object.
* - options: Configuration options.
* - row: The result row object.
* - fields: An array of field render arrays.
*/
function template_preprocess_views_view_vvjl_fields(array &$variables): void {
static $views_theme_loaded = FALSE;
if (!$views_theme_loaded) {
\Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');
$views_theme_loaded = TRUE;
}
template_preprocess_views_view_fields($variables);
}
/**
* Implements hook_preprocess_views_view().
*/
function vvjl_preprocess_views_view(array &$variables): void {
if ($variables['view']->style_plugin instanceof ViewsVanillaJavascriptLightbox) {
$variables['attributes']['class'][] = 'vvj-lightbox';
}
}
/**
* Helper function to convert hex color to RGB.
*
* @param string $hex
* The hex color code.
*
* @return array
* An associative array with 'r', 'g', 'b' values.
*/
function _vvjl_hex_to_rgb(string $hex): array {
$hex = ltrim($hex, '#');
if (strlen($hex) === 3) {
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
}
return [
'r' => hexdec(substr($hex, 0, 2)),
'g' => hexdec(substr($hex, 2, 2)),
'b' => hexdec(substr($hex, 4, 2)),
];
}
/**
* Implements hook_token_info().
*/
function vvjl_token_info(): array {
return [
'tokens' => [
'view' => [
'vvjl' => [
'name' => t('VVJL field output'),
'description' => t("Use these tokens when you enable 'Use replacement tokens from the first row' in Views text areas such as the header, footer, or empty text. Use [vvjl:field_name] for rendered output, or [vvjl:field_name:plain] to strip HTML and return plain text. These tokens pull values from the first row of the View result."),
],
],
],
];
}
/**
* Implements hook_tokens().
*/
function vvjl_tokens(string $type, array $tokens, array $data = [], array $options = []): array {
$replacements = [];
if (!in_array($type, ['vvjl', 'global'])) {
return $replacements;
}
if (!isset($data['view']) || !($data['view'] instanceof ViewExecutable)) {
return $replacements;
}
$view = $data['view'];
if (!($view->style_plugin instanceof ViewsVanillaJavascriptLightbox)) {
return $replacements;
}
if (empty($view->result)) {
return $replacements;
}
$first_row = $view->result[0];
$field_handlers = $view->display_handler->getHandlers('field');
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
foreach ($tokens as $token => $name) {
if (!preg_match('/^[a-zA-Z0-9_]+(:plain)?$/', $token)) {
\Drupal::logger('vvjl')->warning('Invalid token format: @token', ['@token' => $token]);
continue;
}
$plain = FALSE;
$field_id = $token;
if (str_ends_with($token, ':plain')) {
$plain = TRUE;
$field_id = substr($token, 0, -6);
}
if (!isset($field_handlers[$field_id])) {
continue;
}
try {
$handler = $field_handlers[$field_id];
$value = $plain && method_exists($handler, 'advancedRenderText')
? $handler->advancedRenderText($first_row)
: $handler->advancedRender($first_row);
if (is_array($value)) {
$rendered = $renderer->renderPlain($value);
}
else {
$rendered = (string) $value;
}
$replacements["[vvjl:$token]"] = $plain
? Html::decodeEntities(strip_tags($rendered))
: Markup::create($rendered);
}
catch (\Throwable $e) {
\Drupal::logger('vvjl')->error('Token replacement failed for @token: @message', [
'@token' => $token,
'@message' => $e->getMessage(),
]);
$replacements["[vvjl:$token]"] = '';
}
}
return $replacements;
}
/**
* Implements hook_views_data_alter().
*/
function vvjl_views_data_alter(array &$data): void {
$data['views_style_plugin']['views_vvjl'] = [
'type' => 'views_style',
'label' => t('Views Vanilla JavaScript Lightbox'),
'mapping' => [
'unique_id' => [
'type' => 'integer',
'label' => t('Unique ID'),
'description' => t('Unique identifier for the view display.'),
],
'grid_image_width' => [
'type' => 'numeric',
'label' => t('Grid Image Width (px)'),
'description' => t('Set the width of the grid image in pixels.'),
],
'grid_image_gap' => [
'type' => 'numeric',
'label' => t('Grid Image Gap (px)'),
'description' => t('Set the gap between grid images in pixels.'),
],
'overlay_color' => [
'type' => 'string',
'label' => t('Overlay Color'),
'description' => t('Select the overlay color for the lightbox background.'),
],
'overlay_opacity' => [
'type' => 'float',
'label' => t('Overlay Opacity'),
'description' => t('Set the opacity level for the overlay.'),
],
'disable_overlay' => [
'type' => 'boolean',
'label' => t('Disable Overlay Color'),
'description' => t('Enable or disable the overlay color completely.'),
],
'animation' => [
'type' => 'string',
'label' => t('Animation Type'),
'description' => t('Choose the animation type for the lightbox.'),
],
],
];
}
