You are here

smart_date.module in Smart Date 3.x

Field hooks for a field that stores a start and end date as timestamps.

File

smart_date.module
View source
<?php

/**
 * @file
 * Field hooks for a field that stores a start and end date as timestamps.
 */
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function smart_date_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.smart_date':
      if (\Drupal::moduleHandler()
        ->moduleExists('field_ui')) {
        $field_ui_link = Url::fromRoute('help.page', [
          'name' => 'field_ui',
        ]);
      }
      else {
        $field_ui_link = '#';
      }
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Smart Date 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',
        ]),
        ':field_ui' => $field_ui_link,
        ':datetime_do' => 'https://www.drupal.org/documentation/modules/smart_date',
      ]) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $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 Smart 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' => $field_ui_link,
      ]) . '</dd>';
      $output .= '<dt>' . t('Displaying dates') . '</dt>';
      $output .= '<dd>' . t('Smart 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 configure the format at a granular level by altering the display options to meet your needs.') . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_entity_access().
 *
 * Prevent deletion of the default configuration.
 */
function smart_date_entity_access($entity, $operation, $account) {
  $info = $entity
    ->getEntityType();
  if ($info
    ->id() == 'smart_date_format') {
    if ($entity
      ->id() == 'default' && $operation == 'delete') {
      return AccessResult::forbidden();
    }
    else {
      return AccessResult::neutral();
    }
  }
}

/**
 * Helper function to sort a multidimensional array on multiple values.
 */
function smart_date_array_orderby() {
  $args = func_get_args();
  $data = array_shift($args);
  foreach ($args as $n => $field) {
    if (is_string($field)) {
      $tmp = [];
      foreach ($data as $key => $row) {
        $tmp[$key] = $row[$field];
      }
      $args[$n] = $tmp;
    }
  }
  $args[] =& $data;
  call_user_func_array('array_multisort', $args);
  return array_pop($args);
}

Functions

Namesort descending Description
smart_date_array_orderby Helper function to sort a multidimensional array on multiple values.
smart_date_entity_access Implements hook_entity_access().
smart_date_help Implements hook_help().