persian_date.module in Persian Date for Drupal 8 8
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;
/**
* 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) {
$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) {
// 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';
}
Functions
Name | Description |
---|---|
persian_date_element_info_alter | Replace form elements with persian ones |
persian_date_field_widget_info_alter | Replace date widgets with persian widgets |
persian_date_help | Implements hook_help(). |
replace_with_equivalent_module_class |