persian_date.module in Persian Date for Drupal 8 8.4
Same filename and directory in other branches
Contains persian_date.module.
File
persian_date.moduleView source
<?php
/**
* @file
* Contains persian_date.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\persian_date\PersianLanguageDiscovery;
use Drupal\views\Plugin\views\display\Page;
use Drupal\views\Plugin\views\filter\Date;
/**
* @param int $year
*
* @return bool
*/
function is_georgian_year($year) {
return $year > 1800;
}
/**
* Implements hook_help().
*/
function persian_date_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the persian_date module.
case 'help.page.persian_date':
$twig = \Drupal::getContainer()
->get('twig');
$output = $twig
->renderInline(file_get_contents(__DIR__ . '/resources/templates/help.html.twig'));
return $output;
default:
}
}
/**
* Replace form elements with persian ones
*
* Implements hook_element_info_alter().
*/
function persian_date_element_info_alter(array &$info) {
if (!PersianLanguageDiscovery::isPersian()) {
return;
}
$elements = array_intersect_key($info, array_flip([
'date',
'datetime',
'datelist',
]));
foreach ($elements as $name => &$config) {
if (isset($config['#process'])) {
foreach ($config['#process'] as &$process) {
if (is_array($process)) {
$process[0] = replace_with_equivalent_module_class($process[0]);
}
}
}
if (isset($config['#pre_render'])) {
foreach ($config['#pre_render'] as &$process) {
if (is_array($process)) {
$process[0] = replace_with_equivalent_module_class($process[0]);
}
}
}
if (isset($config['#element_validate'])) {
foreach ($config['#element_validate'] as &$process) {
if (is_array($process)) {
$process[0] = replace_with_equivalent_module_class($process[0]);
}
}
}
if (isset($config['#value_callback'])) {
if (is_array($config['#value_callback'])) {
$config['#value_callback'][0] = replace_with_equivalent_module_class($config['#value_callback'][0]);
}
}
}
$info = array_merge($info, $elements);
}
// helper method to replace classes
function replace_with_equivalent_module_class($string) {
$replacements = [
\Drupal\Core\Datetime\Element\Datelist::class => \Drupal\persian_date\Element\PersianDateList::class,
\Drupal\Core\Render\Element\Date::class => \Drupal\persian_date\Element\PersianDate::class,
\Drupal\Core\Datetime\Element\Datetime::class => \Drupal\persian_date\Element\PersianDateTime::class,
];
foreach ($replacements as $search => $replacement) {
$string = str_replace($search, $replacement, $string);
}
return $string;
}
/**
* Replace date widgets with persian widgets
*
* Implements hook_field_widget_info_alter().
*/
function persian_date_field_widget_info_alter(array &$info) {
if (!PersianLanguageDiscovery::isPersian()) {
return;
}
// Let a new field type re-use an existing widget.
$info['datetime_default']['class'] = \Drupal\persian_date\Plugin\Field\FieldWidget\PersianDateTimeDefaultWidget::class;
$info['datetime_datelist']['class'] = \Drupal\persian_date\Plugin\Field\FieldWidget\PersianDateTimeDatelistWidget::class;
$info['datetime_timestamp']['class'] = \Drupal\persian_date\Plugin\Field\FieldWidget\PersianTimestampDateTimeDefaultWidget::class;
$info['datetime_timestamp']['provider'] = 'persian_date';
// integration with "scheduler" module
if (\Drupal::moduleHandler()
->moduleExists('scheduler') && isset($info['datetime_timestamp_no_default']) && is_array($info['datetime_timestamp_no_default'])) {
$info['datetime_timestamp_no_default']['class'] = \Drupal\persian_date\Plugin\Field\FieldWidget\TimestampDatetimeNoDefaultWidget::class;
}
}
/******************************************************
* *
* Integration with "Better Exposed Filters" module *
* *
******************************************************/
function persian_date_better_exposed_filters_display_options_alter(&$display_options, $filter) {
if (!PersianLanguageDiscovery::isPersian()) {
return;
}
if (in_array('bef_datepicker', array_keys($display_options))) {
$display_options['persian_datepicker'] = t('Persian jQuery UI Datepicker');
}
}
$persian_date_widgets = [];
function persian_date_better_exposed_filters_settings_alter(&$settings, $view, $page) {
if (!PersianLanguageDiscovery::isPersian()) {
return;
}
foreach ($view->filter as $name => $filter) {
if ($filter instanceof Date && $settings[$name]['bef_format'] === 'persian_datepicker') {
global $persian_date_widgets;
$persian_date_widgets = is_array($persian_date_widgets) ? $persian_date_widgets : [];
$persian_date_widgets[] = $name;
}
}
}
/**
* Implements hook_form_alter().
*/
function persian_date_form_alter(&$form, $form_state, $form_id) {
if (!PersianLanguageDiscovery::isPersian()) {
return;
}
// integration with better_exposed_filters
global $persian_date_widgets;
if (isset($persian_date_widgets)) {
if (isset($form['#attached']) && isset($form['#attached']['library']) && is_array($form['#attached']['library']) && in_array('better_exposed_filters/general', $form['#attached']['library'])) {
foreach ($persian_date_widgets as $field_id) {
$form[$field_id]['#attributes']['type'] = 'date';
$form[$field_id]['#attributes']['class'][] = 'persian-datepicker';
$form[$field_id]['value']['#attributes']['type'] = 'date';
$form[$field_id]['value']['#attributes']['class'][] = 'persian-datepicker';
$form[$field_id]['min']['#attributes']['class'][] = 'persian-datepicker';
$form[$field_id]['max']['#attributes']['class'][] = 'persian-datepicker';
}
$form['#attached']['library'][] = 'persian_date/core';
}
}
}
function persian_date_views_plugins_filter_alter(array &$plugins) {
if (!PersianLanguageDiscovery::isPersian()) {
return;
}
// Change the 'title' handler class.
$plugins['date']['class'] = \Drupal\persian_date\Plugin\views\filter\Date::class;
}
// replace translation handlers
function persian_date_entity_type_alter(array &$entity_types) {
if (!\Drupal::moduleHandler()
->moduleExists('content_translation')) {
return;
}
// Provide defaults for translation info.
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
foreach ($entity_types as $entity_type) {
if ($entity_type
->isTranslatable()) {
if (!$entity_type
->hasHandlerClass('translation')) {
$entity_type
->setHandlerClass('translation', \Drupal\persian_date\TranslationHandler\ContentTranslationHandler::class);
}
else {
// if has translation handler replace it with correct equivalent
$handlerClass = $entity_type
->getHandlerClass('translation');
switch ($handlerClass) {
case \Drupal\block_content\BlockContentTranslationHandler::class:
$entity_type
->setHandlerClass('translation', \Drupal\persian_date\TranslationHandler\BlockContentTranslationHandler::class);
break;
case \Drupal\comment\CommentTranslationHandler::class:
$entity_type
->setHandlerClass('translation', \Drupal\persian_date\TranslationHandler\CommentTranslationHandler::class);
break;
case \Drupal\content_translation\ContentTranslationHandler::class:
$entity_type
->setHandlerClass('translation', \Drupal\persian_date\TranslationHandler\ContentTranslationHandler::class);
break;
case \Drupal\node\NodeTranslationHandler::class:
$entity_type
->setHandlerClass('translation', \Drupal\persian_date\TranslationHandler\NodeTranslationHandler::class);
break;
case \Drupal\user\ProfileTranslationHandler::class:
$entity_type
->setHandlerClass('translation', \Drupal\persian_date\TranslationHandler\ProfileTranslationHandler::class);
break;
case \Drupal\taxonomy\TermTranslationHandler::class:
$entity_type
->setHandlerClass('translation', \Drupal\persian_date\TranslationHandler\TermTranslationHandler::class);
break;
}
}
}
}
}
Functions
Name![]() |
Description |
---|---|
is_georgian_year | |
persian_date_better_exposed_filters_display_options_alter | |
persian_date_better_exposed_filters_settings_alter | |
persian_date_element_info_alter | Replace form elements with persian ones |
persian_date_entity_type_alter | |
persian_date_field_widget_info_alter | Replace date widgets with persian widgets |
persian_date_form_alter | Implements hook_form_alter(). |
persian_date_help | Implements hook_help(). |
persian_date_views_plugins_filter_alter | |
replace_with_equivalent_module_class |