You are here

function datex_element_info_alter in Datex 8

Same name and namespace in other branches
  1. 7.3 datex_date.inc \datex_element_info_alter()
  2. 7.2 datex_date.inc \datex_element_info_alter()

Implements hook_element_info_alter().

Swap drupal elements with datex elements.

File

./datex.module, line 135

Code

function datex_element_info_alter(array &$info) {
  $rep = function ($find_for) {
    switch ($find_for) {
      case \Drupal\Core\Datetime\Element\Datelist::class:
        $rep = \Drupal\datex\Element\DatexDateList::class;
        break;
      case \Drupal\Core\Datetime\Element\Datetime::class:
        $rep = \Drupal\datex\Element\DatexDateTime::class;
        break;
      case \Drupal\Core\Render\Element\Date::class:
        $rep = \Drupal\datex\Element\DatexDate::class;
        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'][] = 'datex/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;
        }
      }
    }
  }
}