You are here

moment.module in Moment.js 7.2

Same filename and directory in other branches
  1. 8.2 moment.module

Moment.js integration.

File

moment.module
View source
<?php

/**
 * @file
 * Moment.js integration.
 */
function moment_lib_version_ranges() {
  return [
    'moment' => [
      [
        'operator' => '>=',
        'value' => '2.9',
      ],
      [
        'operator' => '<',
        'value' => '3.0',
      ],
    ],
    'moment-timezone' => [
      [
        'operator' => '>=',
        'value' => '0.5.17',
      ],
      [
        'operator' => '<',
        'value' => '2',
      ],
    ],
  ];
}

/**
 * @param string $version
 * @param array $constraints
 */
function moment_is_valid_version($version, array $constraints) {
  foreach ($constraints as $c) {
    if (!version_compare($version, $c['value'], $c['operator'])) {
      return FALSE;
    }
  }
  return TRUE;
}

/**
 * Implements hook_libraries_info().
 */
function moment_libraries_info() {
  $libraries = [];
  if ($library_path = libraries_get_path('moment')) {
    $libraries['moment'] = [
      'library path' => $library_path,
      'name' => 'Moment.js',
      'vendor url' => 'http://momentjs.com',
      'download url' => 'https://github.com/moment/moment/releases',
      'version callback' => 'moment_get_library_version',
      'version arguments' => [],
      'callbacks' => [
        'pre-load' => [
          'moment_library_moment_pre_load_locale',
        ],
      ],
      'files' => [
        'js' => [
          'min/moment.min.js',
        ],
      ],
      'variants' => [
        'none' => [
          'files' => [
            'js' => [
              'moment.js',
            ],
          ],
        ],
        'with-locales.none' => [
          'callbacks' => [
            'pre-load' => [],
          ],
          'files' => [
            'js' => [
              'min/moment-with-locales.js',
            ],
          ],
        ],
        'with-locales.min' => [
          'callbacks' => [
            'pre-load' => [],
          ],
          'files' => [
            'js' => [
              'min/moment-with-locales.min.js',
            ],
          ],
        ],
      ],
      'integration files' => [
        'moment' => [
          'js' => [
            'js/moment.js' => [],
            'js/moment.locale.js' => [],
          ],
        ],
      ],
    ];
  }
  if ($library_path = libraries_get_path('moment-timezone')) {
    $libraries['moment-timezone'] = [
      'library path' => $library_path,
      'name' => 'Moment Timezone',
      'vendor url' => 'http://momentjs.com/timezone',
      'download url' => 'https://github.com/moment/moment-timezone/releases',
      'version callback' => 'moment_get_library_version',
      'version arguments' => [],
      'files' => [
        'js' => [
          'builds/moment-timezone-with-data.min.js',
        ],
      ],
      'dependencies' => [
        'moment',
      ],
      'variants' => [
        'without-data.none' => [
          'files' => [
            'js' => [
              'moment-timezone.js',
            ],
          ],
        ],
        'without-data.min' => [
          'files' => [
            'js' => [
              'builds/moment-timezone.min.js',
            ],
          ],
        ],
        'with-data.none' => [
          'files' => [
            'js' => [
              'builds/moment-timezone-with-data.js',
            ],
          ],
        ],
        'with-data.min' => [
          'files' => [
            'js' => [
              'builds/moment-timezone-with-data.min.js',
            ],
          ],
        ],
        'with-data-2010-2020.none' => [
          'files' => [
            'js' => [
              'builds/moment-timezone-with-data-2010-2020.js',
            ],
          ],
        ],
        'with-data-2010-2020.min' => [
          'files' => [
            'js' => [
              'builds/moment-timezone-with-data-2010-2020.min.js',
            ],
          ],
        ],
      ],
    ];
  }
  return $libraries;
}

/**
 * Library info "version callback".
 *
 * @param array $info
 *   Library info.
 *
 * @return string|false
 *   Version number.
 */
function moment_get_library_version(array $info) {
  return moment_get_package_version($info['library path']);
}

/**
 * Get the version number from package JSON.
 *
 * @param string $library_path
 *   Directory.
 *
 * @return string|false
 *   Version number.
 */
function moment_get_package_version($library_path) {
  static $versions = [];
  if (!$library_path) {
    return FALSE;
  }
  if (!isset($versions[$library_path])) {
    $versions[$library_path] = FALSE;
    if (is_readable("{$library_path}/package.json")) {
      $package = drupal_json_decode(file_get_contents("{$library_path}/package.json"));
      if ($package && !empty($package['version'])) {
        $versions[$library_path] = $package['version'];
      }
    }
  }
  return $versions[$library_path];
}

/**
 * Implements callback_libraries_info__callbacks__pre-load().
 */
function moment_library_moment_pre_load_locale(array &$library) {
  if (!variable_get('preprocess_js', 0)) {

    // Drupal.t() does not work without JS aggregation.
    if (isset($library['integration files']['moment']['js']['js/moment.locale.js'])) {
      unset($library['integration files']['moment']['js']['js/moment.locale.js']);
      if (empty($library['integration files']['moment']['js'])) {
        unset($library['integration files']['moment']['js']);
        if (empty($library['integration files']['moment'])) {
          unset($library['integration files']['moment']);
        }
      }
    }
  }
  $has_locales = FALSE;
  foreach (array_keys($library['files']['js']) as $file_path) {
    if (preg_match('/locales(\\.min){0,1}\\.js$/', $file_path)) {
      $has_locales = TRUE;
      break;
    }
  }
  if (!$has_locales) {

    // Language code, e.g. ’hu’ or ’en-us’.
    $lang_code = drupal_strtolower($GLOBALS['language']->language);
    $file_path = "locale/{$lang_code}.js";
    if (file_exists("{$library['library path']}/{$file_path}")) {
      $library['files']['js'][$file_path] = [];
    }
    elseif (strpos($lang_code, '-') !== FALSE) {
      list($lang_code) = explode('-', $lang_code);
      $file_path = "locale/{$lang_code}.js";
      if (file_exists("{$library['library path']}/{$file_path}")) {
        $library['files']['js'][$file_path] = [];
      }
    }
  }
}

/**
 * The format replacement patterns for the Moment.js library.
 *
 * @see http://php.net/date
 * @see http://momentjs.com/docs/#/displaying/format
 *
 * @return array
 *   Key is the PHP date format character, the value is the corresponding
 *   Moment.js format string.
 */
function moment_get_date_format_replacements() {

  // PHP => Moment.js.
  // PHP does not support the following formats:
  // @todo Complete this list.
  // Seconds              0 1 ... 58 59.
  // '?' => 's'.
  // Day of Week        Su Mo ... Fr Sa.
  // '?' => 'dd'.
  return [
    // Hour             01 02 ... 11 12.
    'h' => 'hh',
    // Hour             00 01 ... 22 23.
    'H' => 'HH',
    // Hour              1  2 ... 11 12.
    'g' => 'h',
    // Hour              0  1 ... 22 23.
    'G' => 'H',
    // Minute           00 01 ... 58 59.
    'i' => 'mm',
    // Seconds.         00 01 ... 58 59.
    's' => 'ss',
    // Year         1970 1971 ... 2029 2030.
    'Y' => 'YYYY',
    // Year           70   71 ...   29   30.
    'y' => 'YY',
    // Month             1  2 ... 11 12.
    'n' => 'M',
    // Month            01 02 ... 11 12.
    'm' => 'MM',
    // Month          Jan Feb ... Nov Dec.
    'M' => 'MMM',
    // Month January February ... November December.
    'F' => 'MMMM',
    // Day of Year    001 002 ... 364 365.
    'z' => 'DDDD',
    // Day of Month      1  2 ... 30 31.
    'j' => 'D',
    // Day of Month     01 02 ... 30 31.
    'd' => 'DD',
    // Day of Month   1st 2nd ... 30th 31st.
    'jS' => 'Do',
    // Day of Week        0 1 ... 5 6.
    'w' => 'd',
    // Day of Week (ISO)  1 2 ... 6 7.
    'N' => 'E',
    // Day of Week    Sun Mon ... Fri Sat.
    'D' => 'ddd',
    // Day of W Sunday Monday ... Friday Saturday.
    'l' => 'dddd',
    // Week of Year       1 2 ... 52 53.
    'W' => 'w',
    // Timezone -07:00 -06:00 ... +06:00 +07:00.
    'P' => 'Z',
    // Timezone   -0700 -0600 ... +0600 +0700.
    'O' => 'ZZ',
    // Shortcut to: "Y-m-d\TH:i:sP".
    // Example:     "2004-02-12T15:19:21+00:00".
    'c' => 'YYYY-MM-DDTHH:mm:ssZ',
    // Shortcut to: "D, d M Y H:i:s O".
    // Example:     "Thu, 21 Dec 2000 16:01:07 +0200".
    'r' => 'ddd, DD MMM YYYY HH:mm:ss ZZ',
    // Unix Timestamp.
    'U' => 'X',
  ];
}

/**
 * Convert a date format type to Moment.js compatible date format.
 *
 * @param string $date_format_type
 *   Examples: "short", "medium", "long".
 * @param null|string|object $lang
 *   Language code or language object.
 *
 * @return string
 *   Date format which is suitable for Moment.js.
 */
function moment_date_format_type_to_moment_date_format($date_format_type, $lang = NULL) {
  return moment_date_format_to_moment_date_format(date_format_type_format($date_format_type, $lang));
}

/**
 * Convert a date format to Moment.js compatible date format.
 *
 * @todo Care about the escaped symbols.
 *
 * @param string $date_format
 *   PHP date format that is suitable for date().
 *
 * @return string
 *   Date format which is suitable for Moment.js.
 */
function moment_date_format_to_moment_date_format($date_format) {
  return strtr($date_format, moment_get_date_format_replacements());
}

/**
 * Provide information about weekdays.
 *
 * @return array
 *   Zero based numeric indexes. Values are array with the following keys:
 *   - numeric: int Numeric identifier.
 *   - name: string Machine name.
 *   - label: string Localized name of the day.
 */
function moment_info_weekdays() {
  return [
    [
      'numeric' => 0,
      'name' => 'sunday',
      'label' => t('Sunday'),
    ],
    [
      'numeric' => 1,
      'name' => 'monday',
      'label' => t('Monday'),
    ],
    [
      'numeric' => 2,
      'name' => 'tuesday',
      'label' => t('Tuesday'),
    ],
    [
      'numeric' => 3,
      'name' => 'wednesday',
      'label' => t('Wednesday'),
    ],
    [
      'numeric' => 4,
      'name' => 'thursday',
      'label' => t('Thursday'),
    ],
    [
      'numeric' => 5,
      'name' => 'friday',
      'label' => t('Friday'),
    ],
    [
      'numeric' => 6,
      'name' => 'saturday',
      'label' => t('Saturday'),
    ],
  ];
}

/**
 * Option list with numeric keys.
 *
 * @return array
 *   Array of key-value pairs.
 */
function moment_weekday_number_options() {
  return moment_weekday_options('numeric');
}

/**
 * Option list with machine readable keys.
 *
 * @return array
 *   Array of key-value pairs.
 */
function moment_weekday_name_options() {
  return moment_weekday_options('name');
}

/**
 * Build an option list from weekdays.
 *
 * @param string $key
 *   The $key property will be used as key.
 * @param int|null $first_day
 *   First day of the week. Sunday = 0.
 * @param string $label
 *   The $label property will be used as label.
 *
 * @return array
 *   Array of key-value pairs.
 */
function moment_weekday_options($key = 'name', $first_day = NULL, $label = 'label') {
  if ($first_day === NULL) {
    $first_day = variable_get('date_first_day', 0);
  }
  $weekdays = moment_info_weekdays();
  if ($first_day != 0) {
    $weekdays = array_merge(array_slice($weekdays, $first_day), array_slice($weekdays, 0, $first_day));
  }
  $options = [];
  foreach ($weekdays as $weekday) {
    $options[$weekday[$key]] = $weekday[$label];
  }
  return $options;
}

Functions

Namesort descending Description
moment_date_format_to_moment_date_format Convert a date format to Moment.js compatible date format.
moment_date_format_type_to_moment_date_format Convert a date format type to Moment.js compatible date format.
moment_get_date_format_replacements The format replacement patterns for the Moment.js library.
moment_get_library_version Library info "version callback".
moment_get_package_version Get the version number from package JSON.
moment_info_weekdays Provide information about weekdays.
moment_is_valid_version
moment_libraries_info Implements hook_libraries_info().
moment_library_moment_pre_load_locale Implements callback_libraries_info__callbacks__pre-load().
moment_lib_version_ranges @file Moment.js integration.
moment_weekday_name_options Option list with machine readable keys.
moment_weekday_number_options Option list with numeric keys.
moment_weekday_options Build an option list from weekdays.