You are here

function hijri_element_info_alter in Hijri 3.0.x

Same name and namespace in other branches
  1. 8.2 hijri.module \hijri_element_info_alter()
  2. 1.0.x hijri.module \hijri_element_info_alter()

Implements hook_element_info_alter().

Swap drupal elements with calendar_systems elements.

File

./hijri.module, line 39
This module provides Hijri Date integration with Drupal core date field and with other Drupal contributions such as Views and Date.

Code

function hijri_element_info_alter(array &$info) {
  $rep = function ($find_for) {
    switch ($find_for) {
      case Datelist::class:
        $rep = HijriDateList::class;
        break;
      case Datetime::class:
        $rep = NULL;
        break;
      case Date::class:
        $rep = NULL;
        break;
      default:
        $rep = NULL;
    }
    return $rep;
  };
  foreach ([
    'date',
    'datelist',
    'datetime',
  ] as $el_) {
    if (!isset($info[$el_])) {
      continue;
    }
    $el =& $info[$el_];
    if ($el_ === 'date' || $el_ === 'datetime') {
      $el['#attached']['library'][] = 'calendar_systems/picker';
    }
    foreach ([
      '#process',
      '#pre_render',
      '#element_validate',
      '#value_callback',
    ] as $attr_) {
      if (!isset($el[$attr_])) {
        continue;
      }
      foreach ($el[$attr_] as $ai => &$a) {
        if (is_array($a)) {
          $a[0] = $rep($a[0]) ?: $a[0];
        }
        else {
          $el[$attr_][$ai] = $rep($a) ?: $a;
        }
      }
    }
  }
}