-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpersian_alterchar.module
executable file
·58 lines (46 loc) · 1.52 KB
/
persian_alterchar.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
<?php
/**
* @file
* Contains persian_alterchar.module.
*/
use Drupal\field\Entity\FieldConfig;
function persian_alterchar_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
/* Check whether it's a node */
if ($entity instanceof \Drupal\node\NodeInterface) {
// Replacement function
function replaceChar($string) {
$replacement_pattern = array ("ي", "ك", "٤", "٥", "٦");
$persian_replacement = array("ی", "ک", "۴", "۵", "۶");
$result = $string;
$result = str_replace($replacement_pattern, $persian_replacement, $string);
return $result;
}
// Node title
$nodeTitle = $entity->getTitle();
$entity->setTitle(replaceChar($nodeTitle));
// Body main
if(!empty($entity->body->value)) {
$entity->body->value = replaceChar($entity->body->value);
}
// Body Summary
if(!empty($entity->body->summary)) {
$entity->body->summary = replaceChar($entity->body->summary);
}
// Only custom text fields
$fields = $entity->getFieldDefinitions();
foreach ($fields as $field) {
$fieldType = $field->getType();
if ($fieldType == 'string') {
$field = replaceChar($field);
} else
return;
}
}
/* Check whether it's a taxonomy term */
if ($entity instanceof \Drupal\taxonomy\Entity\Term) {
$entity->name->value = replaceChar($entity->name->value);
if(!empty($entity->description->value)) {
$entity->description->value = replaceChar($entity->description->value);
}
}
}