-
Notifications
You must be signed in to change notification settings - Fork 0
/
persian_date.module
executable file
·106 lines (91 loc) · 3.36 KB
/
persian_date.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* @file
* Contains persian_date.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\persian_date\PersianLanguageDiscovery;
/**
* 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';
}