countdown-8.x-1.8/modules/countdown_field/countdown_field.module
modules/countdown_field/countdown_field.module
<?php
/**
* @file
* Countdown Field module implementation.
*/
declare(strict_types=1);
use Drupal\Core\Field\FieldTypeCategoryManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function countdown_field_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.countdown_field':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Countdown Field module provides a field type for adding countdown timers to content entities. It integrates with the main Countdown module to provide a consistent countdown experience across your site.') . '</p>';
$output .= '<h3>' . t('Features') . '</h3>';
$output .= '<ul>';
$output .= '<li>' . t('Field type for countdown timers') . '</li>';
$output .= '<li>' . t('Configurable widget with library selection') . '</li>';
$output .= '<li>' . t('Flexible formatter with display options') . '</li>';
$output .= '<li>' . t('Support for all countdown libraries') . '</li>';
$output .= '<li>' . t('AJAX-powered configuration') . '</li>';
$output .= '</ul>';
return $output;
}
}
/**
* Implements hook_field_formatter_info_alter().
*/
function countdown_field_field_formatter_info_alter(&$info) {
$info['string']['field_types'][] = 'countdown';
}
/**
* Implements hook_field_type_category_info_alter().
*/
function countdown_field_field_type_category_info_alter(&$definitions) {
// The `countdown` field type belongs in the `general` category, so the
// libraries need to be attached using an alter hook.
$definitions[FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY]['libraries'][] = 'countdown_field/countdown_field.countdown-icon';
}
/**
* Implements hook_theme().
*/
function countdown_field_theme() {
return [
'countdown_field' => [
'variables' => [
'library' => NULL,
'target_date' => NULL,
'event_name' => NULL,
'event_url' => NULL,
'finish_message' => NULL,
'attributes' => [],
'field_id' => NULL,
'delta' => 0,
],
],
'countdown_field_preview' => [
'variables' => [
'library' => NULL,
'target_date' => NULL,
'preview_id' => NULL,
],
],
];
}
/**
* Prepares variables for countdown field templates.
*
* Default template: countdown-field.html.twig.
*
* @param array $variables
* An associative array containing:
* - library: The countdown library ID.
* - target_date: The target date/time.
* - event_name: Optional event name.
* - event_url: Optional event URL.
* - finish_message: Message when countdown finishes.
* - attributes: HTML attributes for the element.
* - field_id: Unique field identifier.
* - delta: Field delta for multivalue fields.
*/
function template_preprocess_countdown_field(&$variables) {
// Ensure attributes array exists.
if (!isset($variables['attributes'])) {
$variables['attributes'] = [];
}
// Add required classes.
$variables['attributes']['class'][] = 'countdown-field';
$variables['attributes']['class'][] = 'countdown-timer';
// Add library-specific class.
if (!empty($variables['library'])) {
$library_class = 'countdown-' . str_replace('_', '-', $variables['library']);
$variables['attributes']['class'][] = $library_class;
}
// Add data attributes for JavaScript initialization.
$variables['attributes']['data-countdown-field-id'] = $variables['field_id'];
$variables['attributes']['data-countdown-delta'] = $variables['delta'];
$variables['attributes']['data-countdown-library'] = $variables['library'];
$variables['attributes']['data-countdown-target'] = $variables['target_date'];
// Add finish message if provided.
if (!empty($variables['finish_message'])) {
$variables['attributes']['data-countdown-finish'] = $variables['finish_message'];
}
// Generate unique ID if not provided.
if (empty($variables['attributes']['id'])) {
$variables['attributes']['id'] = 'countdown-field-' . $variables['field_id'] . '-' . $variables['delta'];
}
// Prepare event link if both name and URL are provided.
if (!empty($variables['event_name']) && !empty($variables['event_url'])) {
$variables['has_event_link'] = TRUE;
}
else {
$variables['has_event_link'] = FALSE;
}
}
/**
* Prepares variables for countdown field preview templates.
*
* Default template: countdown-field-preview.html.twig.
*
* @param array $variables
* An associative array containing preview variables.
*/
function template_preprocess_countdown_field_preview(&$variables) {
// Add preview-specific classes.
$variables['attributes']['class'][] = 'countdown-field-preview';
$variables['attributes']['class'][] = 'countdown-preview';
// Add preview ID.
if (!empty($variables['preview_id'])) {
$variables['attributes']['id'] = $variables['preview_id'];
}
// Add data attributes for preview initialization.
$variables['attributes']['data-countdown-preview'] = 'true';
$variables['attributes']['data-countdown-library'] = $variables['library'];
$variables['attributes']['data-countdown-target'] = $variables['target_date'];
}
