You are here

function datex_factory in Datex 8

Same name and namespace in other branches
  1. 7.3 datex.module \datex_factory()
  2. 7.2 datex_api/datex_api.class.inc \datex_factory()
18 calls to datex_factory()
DatexArgHandlerTrait::query in src/Plugin/views/argument/DatexArgHandlerTrait.php
DatexBlock::build in src/Plugin/Block/DatexBlock.php
Builds and returns the renderable array for this block plugin.
DatexDateList::datexDatetimeRangeYears in src/Element/DatexDateList.php
DatexDateList::processDatelist in src/Element/DatexDateList.php
Expands a date element into an array of individual elements.
DatexDateList::valueCallback in src/Element/DatexDateList.php
Validates the date type to adjust 12 hour time and prevent invalid dates. If the date is valid, the date is set in the form.

... See full list

File

./datex.module, line 27

Code

function datex_factory($tz = NULL, $lang_code = '', $calendar_name = '') {

  // COPY FROM common.inc::format_date() (drupal 7).
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['timezones'] = [];
  }
  $timezones =& $drupal_static_fast['timezones'];
  if (!$tz) {
    $tz = drupal_get_user_timezone();
  }
  if (is_string($tz)) {
    if (!isset($timezones[$tz])) {
      $timezones[$tz] = timezone_open($tz);
    }
    $tz = $timezones[$tz];
  }
  if (!$calendar_name) {
    $for_lang = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
    if (count(\Drupal::languageManager()
      ->getLanguages()) === 1 || $for_lang === 'fa') {
      $calendar_name = 'persian';
    }
    elseif ($for_lang === 'en') {
      $calendar_name = 'gregorian';
    }
  }
  if (!$calendar_name) {
    return NULL;
  }
  if (!$lang_code) {
    if (count(\Drupal::languageManager()
      ->getLanguages()) === 1) {
      $lang_code = 'fa';
    }
    else {
      $lang_code = \Drupal::languageManager()
        ->getCurrentLanguage()
        ->getId();
    }
  }
  if (!DATEX_USE_INTL) {
    switch ($calendar_name) {
      case 'persian':
        return new DatexPoorMansJaliliCalendar(is_string($tz) ? timezone_open($tz) : $tz, $lang_code);
      default:
        return new DatexPoorMansGregorianCalendar(is_string($tz) ? timezone_open($tz) : $tz, $lang_code);
    }
  }
  switch ($calendar_name) {
    case 'persian':
      return new DatexPersianIntlCalendar($tz, $calendar_name, $lang_code);
    default:
      return new DatexIntlCalendar($tz, $calendar_name, $lang_code);
  }
}