vvja-1.0.1/vvja.module
vvja.module
<?php
/**
* @file
* Provides the module implementation for vvja.
*
* Contains template preprocessing and theme definitions for Views.
*
* Filename: vvja.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\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Drupal\vvja\Plugin\views\style\ViewsVanillaJavascriptAccordion;
/**
* Implements hook_help().
*/
function vvja_help(string $route_name, RouteMatchInterface $route_match): ?string {
if ($route_name === 'help.page.vvja') {
return _vvja_helper_render_readme();
}
return NULL;
}
/**
* Helper function to render README.md.
*/
function _vvja_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 vvja_theme(array $existing, string $type, string $theme, string $path): array {
return [
'views_view_vvja_fields' => [
'variables' => [
'view' => NULL,
'options' => [],
'row' => NULL,
'field_alias' => NULL,
'attributes' => [],
'title_attributes' => [],
'content_attributes' => [],
'title_prefix' => [],
'title_suffix' => [],
'fields' => [],
],
'template' => 'views-view-vvja-fields',
'path' => $path . '/templates',
],
'views_view_vvja' => [
'variables' => [
'view' => NULL,
'rows' => [],
'options' => [],
],
'template' => 'views-view-vvja',
'path' => $path . '/templates',
],
];
}
/**
* Implements hook_preprocess_HOOK() for views_view_vvja.
*/
function template_preprocess_views_view_vvja(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\vvja\Plugin\views\style\ViewsVanillaJavascriptAccordion $handler */
$handler = $variables['view']->style_plugin;
$list_attributes = [];
if (!empty($handler->options['animation'])) {
$list_attributes['data-animation'] = $handler->options['animation'];
}
if (!empty($handler->options['unique_id'])) {
$list_attributes['data-unique-id'] = $handler->options['unique_id'];
}
if (!empty($handler->options['transition_speed'])) {
$list_attributes['data-transition-speed'] = $handler->options['transition_speed'];
}
if (!empty($handler->options['accordion_item_width'])) {
$list_attributes['data-accordion-item-width'] = $handler->options['accordion_item_width'];
}
if (isset($handler->options['global_toggle'])) {
$list_attributes['data-global-toggle'] = $handler->options['global_toggle'] ? 'true' : 'false';
}
if (isset($handler->options['single_toggle'])) {
$list_attributes['data-single-toggle'] = $handler->options['single_toggle'] ? 'true' : 'false';
}
if (isset($handler->options['first_toggle'])) {
$list_attributes['data-first-toggle'] = $handler->options['first_toggle'] ? 'true' : 'false';
}
if (isset($handler->options['enable_css'])) {
$list_attributes['data-enable-css'] = $handler->options['enable_css'] ? 'true' : 'false';
}
if (isset($handler->options['exclusive_panel'])) {
$list_attributes['data-exclusive-panel'] = $handler->options['exclusive_panel'] ? 'true' : 'false';
}
$variables['deeplink_enabled'] = $handler->options['enable_deeplink'] ?? FALSE;
$variables['deeplink_identifier'] = $handler->options['deeplink_identifier'] ?? '';
$variables['list_attributes'] = new Attribute($list_attributes);
$variables['options'] = $handler->options;
$variables['settings'] = [
'view_id' => $variables['view']->dom_id,
'animation' => $handler->options['animation'],
'unique_id' => $handler->options['unique_id'],
'transition_speed' => $handler->options['transition_speed'],
'accordion_item_width' => $handler->options['accordion_item_width'],
'global_toggle' => $handler->options['global_toggle'],
'single_toggle' => $handler->options['single_toggle'],
'first_toggle' => $handler->options['first_toggle'],
'enable_css' => $handler->options['enable_css'],
'exclusive_panel' => $handler->options['exclusive_panel'],
];
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_vvja_fields', $theme_hook_suggestion);
}
}
}
}
template_preprocess_views_view_unformatted($variables);
}
/**
* Prepares variables for views_view_vvja_fields template.
*/
function template_preprocess_views_view_vvja_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 vvja_preprocess_views_view(array &$variables): void {
if ($variables['view']->style_plugin instanceof ViewsVanillaJavascriptAccordion) {
$variables['attributes']['class'][] = 'vvj-accordion';
}
}
/**
* Implements hook_token_info().
*/
function vvja_token_info(): array {
return [
'tokens' => [
'view' => [
'vvja' => [
'name' => t('VVJA 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 [vvja:field_name] for rendered output, or [vvja: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 vvja_tokens(string $type, array $tokens, array $data = [], array $options = []): array {
$replacements = [];
if (!in_array($type, ['vvja', 'global'])) {
return $replacements;
}
if (!isset($data['view']) || !($data['view'] instanceof ViewExecutable)) {
return $replacements;
}
$view = $data['view'];
if (!($view->style_plugin instanceof ViewsVanillaJavascriptAccordion)) {
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('vvja')->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["[vvja:$token]"] = $plain
? Html::decodeEntities(strip_tags($rendered))
: Markup::create($rendered);
}
catch (\Throwable $e) {
\Drupal::logger('vvja')->error('Token replacement failed for @token: @message', [
'@token' => $token,
'@message' => $e->getMessage(),
]);
$replacements["[vvja:$token]"] = '';
}
}
return $replacements;
}
/**
* Implements hook_views_data_alter().
*/
function vvja_views_data_alter(array &$data): void {
$data['views_style_plugin']['views_vvja'] = [
'type' => 'views_style',
'label' => t('Views Vanilla JavaScript Accordion'),
'mapping' => [
'unique_id' => [
'type' => 'string',
'label' => t('Unique ID for the view display'),
],
'animation' => [
'type' => 'string',
'label' => t('Animation Type'),
'description' => t('The type of animation for opening/closing panels.'),
],
'transition_speed' => [
'type' => 'float',
'label' => t('Transition Speed'),
'description' => t('The speed of the transition animation.'),
],
'accordion_item_width' => [
'type' => 'integer',
'label' => t('Accordion Item Width'),
'description' => t('The width of each accordion item in pixels.'),
],
'global_toggle' => [
'type' => 'boolean',
'label' => t('Global Toggle'),
'description' => t('Show collapse/expand all button.'),
],
'single_toggle' => [
'type' => 'boolean',
'label' => t('Single Toggle'),
'description' => t('Show individual collapse/expand buttons.'),
],
'first_toggle' => [
'type' => 'boolean',
'label' => t('First Toggle'),
'description' => t('Expand the first item by default.'),
],
'enable_css' => [
'type' => 'boolean',
'label' => t('Enable CSS'),
'description' => t('Include the default CSS library.'),
],
'exclusive_panel' => [
'type' => 'boolean',
'label' => t('Exclusive Panel'),
'description' => t('Only allow one panel to be open at a time.'),
],
],
];
}
