This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_counter.module
94 lines (86 loc) · 3.37 KB
/
word_counter.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
<?php
/**
* @file
* Provides word counter to the body of the article.
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function word_counter_help($route_name, RouteMatchInterface $route_match) {
if ($route_name !== 'help.page.word_counter') {
return '';
}
return '<h3>' . t('About') . '</h3>' .
'<p>' . t('As you can understand from the module\'s name, this functionality is created to analyze the text in the "Body" field from the nodes that belong to the "Article" content type. A word is any sequence of letters (the language of the text does not matter), which is separated from the rest of the content by spaces or punctuation marks. In addition to letters, a word can include numbers and hyphens, however, if the sequence consists of only the last two types of symbols, then this sequence is not considered a word.') . '</p>' .
'<p>' . t('The calculation process is performed automatically when a node is created or edited. It should be noted that this functionality must be enabled on the settings page (if it was previously disabled), however, after allowing the module to, its functionality will be enabled by default.') . '</p>';
}
/**
* Implements hook_entity_view_display_alter().
*/
function word_counter_entity_view_display_alter(
EntityViewDisplayInterface $display,
array $context
) {
if (
$context['entity_type'] === 'node' &&
$context['bundle'] === 'article' &&
($component = $display->getComponent('body')) !== NULL
) {
if (\Drupal::config('word_counter.settings')->get('status')) {
$display->setComponent('field_word_counter', [
'type' => 'number_integer',
'label' => 'inline',
'settings' => [
'thousand_separator' => ' ',
'prefix_suffix' => FALSE,
],
'third_party_settings' => [],
// Placing the counter directly in front of the field that contains the
// text in which the words are searched.
'weight' => ($component['weight'] ?? 0) - 1,
'region' => 'content',
]);
}
else {
$display->removeComponent('field_word_counter');
}
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function word_counter_node_presave(EntityInterface $entity) {
if (
$entity->bundle() === 'article' &&
$entity->hasField('body') &&
\Drupal::config('word_counter.settings')->get('status') &&
(
$entity->isNew() ||
// For an existing node, the number of words is recalculated only if the
// text that was before and the text that is now differ.
$entity->body->value !== $entity->original->body->value
)
) {
// First, all HTML tags are removed, and then - spaces at the beginning and
// end of the text.
$value = trim(strip_tags($entity->body->value));
if (!empty($value)) {
preg_match_all(
// Search in the text for sequences of letters (in any language), among
// which there may be hyphens.
'/\p{L}+(?:-\p{L}+)*/u',
// Converting special characters, such as the copyright symbol, to plain
//text before performing a word search.
html_entity_decode($value),
$matches,
);
$entity->field_word_counter->value = count($matches[0]);
}
else {
$entity->field_word_counter->value = 0;
}
}
}