jquery_calendar.module in jQuery World Calendars API 3.x
Same filename and directory in other branches
Implements necessary hooks, API and helpers for jQuery World Calendars.
File
jquery_calendar.moduleView source
<?php
/**
* @file
* Implements necessary hooks, API and helpers for jQuery World Calendars.
*
*/
/**
* @return array|bool
* @throws \Drupal\jquery_calendar\Error\MissingRequirements
*/
function jquery_calendar_requirements_check() {
$library = \Drupal::service('library.discovery')
->getLibraryByName('jquery_calendar', 'jquery_calendars.core');
$library_exists = false;
if ($library) {
$library_exists = file_exists(DRUPAL_ROOT . '/' . $library['js'][0]['data']);
}
if (!$library_exists) {
throw new \Drupal\jquery_calendar\Error\MissingRequirements(t('The Jquery Calendar library needs to be <a href="@url">downloaded</a> and extracted into the /libraries/jquery_calendar folder in your Drupal installation directory.', [
'@url' => 'http://keith-wood.name/calendars.html',
]));
}
return true;
}
/**
* Internal helper to collect all datepicker themes.
*
* @return
* An array of datepicker theme component names.
*/
function jquery_calendar_datepicker_themes() {
$items = \Drupal::service('library.discovery')
->getLibrariesByExtension('jquery_calendar');
$themes = [];
foreach ($items as $key => $info) {
if (preg_match('%jquery_calendars\\.picker\\.theme\\.([^.]+)%i', $key, $matches)) {
$themes[$matches[1]] = ucwords($matches[1]);
}
}
return $themes;
}
/**
* Internal helper to collect all datepicker themes.
*
* @return
* An array of datepicker theme component names.
*/
function jquery_calendar_types() {
$items = \Drupal::service('library.discovery')
->getLibrariesByExtension('jquery_calendar');
$themes = [
'gregorian' => 'Gregorian',
];
foreach ($items as $key => $info) {
if (preg_match('%jquery_calendars\\.type\\.([^.]+)%i', $key, $matches)) {
$themes[$matches[1]] = ucwords($matches[1]);
}
}
return $themes;
}
/**
* @param $format //flat
* @return array
*/
function jquery_calendar_langs($format = NULL) {
$items = \Drupal::service('library.discovery')
->getLibrariesByExtension('jquery_calendar');
$themes = $format !== 'flat' ? [
'en' => [
'core',
'picker',
'type-all',
],
] : [
'en' => 'en, core, picker, type-all',
];
foreach ($items as $key => $info) {
if (preg_match('%jquery_calendars\\.lang\\.([^-]+)-([a-z\\-]+)%i', $key, $matches)) {
if (!isset($themes[$matches[2]])) {
$themes[$matches[2]] = $format !== 'flat' ? [] : $matches[2] . ':';
}
if ($format !== 'flat') {
$themes[$matches[2]][] = $matches[1];
}
else {
$themes[$matches[2]] .= ' ' . $matches[1];
}
}
}
return $themes;
}
/**
* @param $name
* @return array|bool
*/
function jquery_calendar_convert_file_name_to_library_name($name) {
$result = FALSE;
if (preg_match('%jquery\\.calendars(\\.([a-z]+))?(\\.([a-z]+))?(-([a-z\\-]+))?(.min)?\\.js$%i', $name, $matches)) {
$result = [
'libraryName' => NULL,
'component' => NULL,
'type' => NULL,
'lang' => NULL,
'minified' => NULL,
];
$part1 = $matches[1];
if ($part1 !== 'lang' && $part1 !== 'min' && $part1 !== 'picker' && $part1 !== 'all' && $part1 !== 'validation' && $part1 !== 'plus') {
$result['component'] = 'type';
$result['type'] = $part1;
}
}
return $result;
}
/**
* @todo automatcally detect available calendar types, code exists but it's buggy
* Implements hook_library_info_alter
* @param $libraries
* @param $extension
*/
function jquery_calendar_library_info_alter(&$libraries, $extension) {
if (isset($libraries['jquery_calendars.core'])) {
$path = DRUPAL_ROOT . dirname(dirname(key($libraries['jquery_calendars.core']['js'])));
$files = \Drupal::service('file_system')
->scanDirectory($path . '/js', '/.*/');
foreach ($files as $file) {
/*
# Add any calendar type that is missing from the libraries.yml file
if (preg_match('%jquery\.calendars(\.([a-z]+))$%i', $file->name, $matches)) {
$name = $matches[2];
if ($name !== 'lang' && $name !== 'min' && $name !== 'picker' && $name !== 'all' && $name !== 'validation' && $name !== 'plus') {
$libraries['jquery_calendars.type.' . $name] = $libraries['jquery_calendars.type.coptic'];
$oldFile = key($libraries['jquery_calendars.type.' . $name]['js']);
$newFile = str_replace('coptic', $name, $oldFile);
$libraries['jquery_calendars.type.' . $name]['js'][$newFile] = $libraries['jquery_calendars.type.coptic']['js'][$oldFile];
unset($libraries['jquery_calendars.type.' . $name]['js'][$oldFile]);
var_Dump($libraries['jquery_calendars.type.' . $name]);
}
}
*/
# Add any calendar lang that is missing from the libraries.yml file
if (preg_match('%jquery\\.calendars(\\.([a-z]+))?-([a-z\\-]+)%i', $file->name, $matches)) {
$lang = $matches[3];
$type = $matches[2];
$sample = NULL;
if (empty($type)) {
$type = 'core';
$sample = 'core-fa';
}
elseif ($type === 'picker') {
$sample = 'picker-fa';
}
else {
$sample = 'type.persian-fa';
$type = 'type.' . $type;
}
$libraries["jquery_calendars.lang.{$type}-{$lang}"] = $libraries["jquery_calendars.lang.{$sample}"];
$libraries["jquery_calendars.lang.{$type}-{$lang}"]['js'] = [
str_replace(DRUPAL_ROOT, '', $file->uri) => [],
];
}
}
# Find the files with minify version and update the library paths if needed
$compressionLevel = \Drupal::config('jquery_calendar.settings')
->get('compression_level');
if ($compressionLevel === 'min') {
foreach ($libraries as $key => $library) {
foreach ($library['js'] as $file => $info) {
if (preg_match('%jquery_calendars.*%', $key) && isset($library['js'])) {
$minifiedPath = preg_replace('/(.*)\\.js$/', '$1.min.js', $file);
if (file_exists(DRUPAL_ROOT . $minifiedPath)) {
$libraries[$key]['js'][$minifiedPath] = $library['js'][$file];
$libraries[$key]['js'][$minifiedPath]['minified'] = true;
unset($libraries[$key]['js'][$file]);
}
}
}
}
}
}
}
Functions
Name | Description |
---|---|
jquery_calendar_convert_file_name_to_library_name | |
jquery_calendar_datepicker_themes | Internal helper to collect all datepicker themes. |
jquery_calendar_langs | |
jquery_calendar_library_info_alter | @todo automatcally detect available calendar types, code exists but it's buggy Implements hook_library_info_alter |
jquery_calendar_requirements_check | |
jquery_calendar_types | Internal helper to collect all datepicker themes. |