-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypographer.module
More file actions
231 lines (196 loc) · 6.59 KB
/
typographer.module
File metadata and controls
231 lines (196 loc) · 6.59 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* @file
* Define hook implementations for the typographer module.
*/
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Entity\EntityForm;
use \Drupal\Core\Form\FormStateInterface;
use Drupal\field\FieldConfigInterface;
use \Drupal\field_ui\Form\FieldConfigEditForm;
/**
* Text fields
* @return string[]
*/
function typographer_field_types(): array {
return ['text_with_summary', 'string', 'string_long', 'text', 'text_long'];
};
/**
* Implements hook_form_alter() for 'typographer'
* @param $form
* @param FormStateInterface $form_state
* @param $form_id
*/
function typographer_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (!\Drupal::currentUser()->hasPermission('access typographer')) {
return;
}
$formObject = $form_state->getFormObject();
// Add checkboxes to field config
if ($formObject instanceof FieldConfigEditForm) {
$entity = $form_state->getFormObject()->getEntity();
$type = $entity->getType();
if (!in_array($type, typographer_field_types())) {
return;
}
$form['typography'] = [
'#type' => 'fieldset',
'#title' => t('Typography'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 99,
'#group' => 'additional_settings',
'#tree' => TRUE,
'#attributes' => [
'class' => [
'typography',
],
],
];
if ($type === 'text_with_summary') {
$form['typography']['do_typography_summary'] = [
'#type' => 'checkbox',
'#title' => t('Enable typography on summary'),
'#default_value' => $entity->getSetting('do_typography_summary'),
];
}
$form['typography']['do_typography'] = [
'#type' => 'checkbox',
'#title' => t('Enable typography'),
'#default_value' => $entity->getSetting('do_typography'),
];
$form['typography']['do_cleanword'] = [
'#type' => 'checkbox',
'#title' => t('Enable Word Cleaning'),
'#default_value' => $entity->getSetting('do_cleanword'),
];
}
// Add checkboxes to edit form
if ($formObject instanceof EntityForm && !($formObject instanceof EntityDeleteForm)) {
$entity = $form_state->getFormObject()->getEntity();
if ($entity instanceof ContentEntityBase) {
$form['node_typography'] = [
'#type' => 'fieldset',
'#title' => t('Node Typography'),
'#description' => t('Process text typographing and cleaning'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 9999,
'#group' => 'additional_settings',
'#tree' => TRUE,
'#attributes' => ['class' => ['node-typography']]
];
$form['node_typography']['do_typography'] = [
'#type' => 'checkbox',
'#title' => t('Do typography'),
'#description' => t('Enable text typographer processing on node save')
];
$form['node_typography']['do_cleanword'] = [
'#type' => 'checkbox',
'#title' => t('Do Word Cleaning'),
'#description' => t('Enable Microsoft Word tags and styles cleaning processing on node save')
];
if (!isset($form['#entity_builders'])) {
$form['#entity_builders'] = [];
}
$form['#entity_builders'][] = 'typographer_entity_builder';
}
}
}
/**
* Transforms entity values based on settings and submit
* @param string $type
* @param \Drupal\Core\Entity\ContentEntityBase $entity
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return \Drupal\Core\Entity\ContentEntityBase
* @throws \Drupal\Core\TypedData\Exception\ReadOnlyException
*/
function typographer_entity_builder(string $type, ContentEntityBase $entity, &$form, FormStateInterface &$form_state): ContentEntityBase {
$values = $form_state->getValues();
if (
!(isset($values['node_typography']['do_cleanword']) && $values['node_typography']['do_cleanword']
|| isset($values['node_typography']['do_typography']) && $values['node_typography']['do_typography'])
) {
return $entity;
}
$titleFields = ['title', 'name'];
$fields = $entity->getFields();
$defs = $entity->getFieldDefinitions();
foreach ($defs as $fieldDefinition) {
$type = $fieldDefinition->getType();
$name = $fieldDefinition->getName();
$settings = $fieldDefinition->getSettings();
if (!in_array($type, typographer_field_types()) && !in_array($name, $titleFields)) {
continue;
}
$val = $fields[$name]->getValue();
if (
isset($values['node_typography']['do_cleanword']) && $values['node_typography']['do_cleanword']
&& isset($settings['do_cleanword']) && $settings['do_cleanword']
) {
foreach ($val as $i => $value) {
$val[$i]['value'] = typographer_cleanHtml($value['value']);
}
}
if (
isset($values['node_typography']['do_typography']) && $values['node_typography']['do_typography']
&& (isset($settings['do_typography']) && $settings['do_typography'] || in_array($name, $titleFields))
) {
foreach ($val as $i => $value) {
$val[$i]['value'] = typographer_doTypography($value['value']);
if (isset($settings['do_typography_summary'], $val[$i]['summary'])) {
$val[$i]['summary'] = typographer_doTypography($val[$i]['summary']);
}
}
}
$fields[$name]->setValue($val);
}
return $entity;
}
/**
* Implements hook_ENTITY_TYPE_presave() on 'field_config'
* @param \Drupal\field\FieldConfigInterface $field
*
* @return void
*/
function typographer_field_config_presave(FieldConfigInterface $field) {
if ($field->isSyncing()) {
return;
}
if (isset($field->typography['do_typography_summary'])) {
$field->setSetting('do_typography_summary', $field->typography['do_typography_summary']);
}
if (isset($field->typography['do_typography'])) {
$field->setSetting('do_typography', $field->typography['do_typography']);
}
if (isset($field->typography['do_cleanword'])) {
$field->setSetting('do_cleanword', $field->typography['do_cleanword']);
}
}
/**
*
*
* @param string $input
*
* @return string
*/
function typographer_doTypography(string $input): string {
require_once(__DIR__.'/EMT.php');
return EMTypograph::fast_apply($input, [
'Text.paragraphs' => 'off',
'OptAlign.all' => 'off',
'Etc.unicode_convert' => 'on',
]);
}
function typographer_cleanHtml(string $text): string {
require_once(__DIR__.'/HTMLCleaner.php');
$cleaner = new HTMLCleaner();
$cleaner->Options['UseTidy'] = FALSE;
$cleaner->Options['OutputXHTML'] = FALSE;
$cleaner->Options['Optimize'] = TRUE;
$cleaner->html = $text;
return @$cleaner->cleanUp('utf-8');
}