datetime_range.module

Same filename in other branches
  1. 9 core/modules/datetime_range/datetime_range.module
  2. 8.9.x core/modules/datetime_range/datetime_range.module
  3. 11.x core/modules/datetime_range/datetime_range.module

Field hooks to implement a datetime field that stores a start and end date.

File

core/modules/datetime_range/datetime_range.module

View source
<?php


/**
 * @file
 * Field hooks to implement a datetime field that stores a start and end date.
 */
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\datetime_range\DateTimeRangeConstantsInterface;
use Drupal\datetime_range\Plugin\Field\FieldFormatter\DateRangeCustomFormatter;
use Drupal\datetime_range\Plugin\Field\FieldFormatter\DateRangeDefaultFormatter;
use Drupal\datetime_range\Plugin\Field\FieldFormatter\DateRangePlainFormatter;

/**
 * Implements hook_help().
 */
function datetime_range_help($route_name, RouteMatchInterface $route_match) {
    switch ($route_name) {
        case 'help.page.datetime_range':
            $output = '';
            $output .= '<h2>' . t('About') . '</h2>';
            $output .= '<p>' . t('The Datetime Range module provides a Date field that stores start dates and times, as well as end dates and times. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI module help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":datetime_do">online documentation for the Datetime Range module</a>.', [
                ':field' => Url::fromRoute('help.page', [
                    'name' => 'field',
                ])->toString(),
                ':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [
                    'name' => 'field_ui',
                ])->toString() : '#',
                ':datetime_do' => 'https://www.drupal.org/documentation/modules/datetime_range',
            ]) . '</p>';
            $output .= '<h2>' . t('Uses') . '</h2>';
            $output .= '<dl>';
            $output .= '<dt>' . t('Managing and displaying date fields') . '</dt>';
            $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the Date field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [
                ':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [
                    'name' => 'field_ui',
                ])->toString() : '#',
            ]) . '</dd>';
            $output .= '<dt>' . t('Displaying dates') . '</dt>';
            $output .= '<dd>' . t('Dates can be displayed using the <em>Plain</em> or the <em>Default</em> formatter. The <em>Plain</em> formatter displays the date in the <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format. If you choose the <em>Default</em> formatter, you can choose a format from a predefined list that can be managed on the <a href=":date_format_list">Date and time formats</a> page.', [
                ':date_format_list' => Url::fromRoute('entity.date_format.collection')->toString(),
            ]) . '</dd>';
            $output .= '</dl>';
            return $output;
    }
}

/**
 * Implements hook_ENTITY_TYPE_presave() for entity_view_display entities.
 *
 * @todo Remove this when datetime_range_post_update_from_to_configuration is removed.
 */
function datetime_range_entity_view_display_presave(EntityViewDisplayInterface $entity_view_display) : void {
    
    /** @var \Drupal\Core\Field\FormatterPluginManager $field_formatter_manager */
    $field_formatter_manager = \Drupal::service('plugin.manager.field.formatter');
    foreach ($entity_view_display->getComponents() as $name => $component) {
        if (empty($component['type'])) {
            continue;
        }
        $plugin_definition = $field_formatter_manager->getDefinition($component['type'], FALSE);
        $daterange_formatter_classes = [
            DateRangeCustomFormatter::class,
            DateRangeDefaultFormatter::class,
            DateRangePlainFormatter::class,
        ];
        if (!in_array($plugin_definition['class'], $daterange_formatter_classes, FALSE)) {
            continue;
        }
        if (!isset($component['settings']['from_to'])) {
            // Existing daterange formatters don't have 'from_to'.
            $component['settings']['from_to'] = DateTimeRangeConstantsInterface::BOTH;
            $entity_view_display->setComponent($name, $component);
        }
    }
}

Functions

Title Deprecated Summary
datetime_range_entity_view_display_presave Implements hook_ENTITY_TYPE_presave() for entity_view_display entities.
datetime_range_help Implements hook_help().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.