-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparagraphs_editor.module
More file actions
168 lines (152 loc) · 5.94 KB
/
Copy pathparagraphs_editor.module
File metadata and controls
168 lines (152 loc) · 5.94 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
<?php
/**
* @file
* Supports editor integration for the paragraphs module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\paragraphs_editor\Utility\RenderUtility;
/**
* Implements hook_entity_insert().
*/
function paragraphs_editor_entity_insert(EntityInterface $entity) {
\Drupal::service('paragraphs_editor.edit_buffer.cache')->processDeletionQueue($entity);
}
/**
* Implements hook_entity_update().
*/
function paragraphs_editor_entity_update(EntityInterface $entity) {
\Drupal::service('paragraphs_editor.edit_buffer.cache')->processDeletionQueue($entity);
}
/**
* Implements hook_entity_delete().
*/
function paragraphs_editor_entity_delete(EntityInterface $entity) {
\Drupal::service('paragraphs_editor.edit_buffer.cache')->processDeletionQueue($entity);
}
/**
* Implements hook_help().
*/
function paragraphs_editor_help($route_name, RouteMatchInterface $route_match) {
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function paragraphs_editor_form_field_config_edit_form_alter(&$form, $form_state) {
$field_definition = $form_state->getBuildInfo()['callback_object']->getEntity();
if ($field_definition->getType() == 'entity_reference_revisions') {
$target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
if ($target_type == 'paragraph') {
$form['third_party_settings']['paragraphs_editor'] = [
'#type' => 'fieldset',
'#title' => t('Paragraphs Editor'),
];
$filter_format_options = [];
foreach (filter_formats() as $filter_format) {
$filter_format_options[$filter_format->id()] = $filter_format->label();
}
$default_format = $field_definition->getThirdPartySetting('paragraphs_editor', 'filter_format');
if (!$default_format) {
$default_format = 'paragraphs_ckeditor';
}
$text_bundle_options = \Drupal::service('paragraphs_editor.field_value.manager')->getTextBundles();
foreach ($text_bundle_options as &$text_bundle_option) {
$text_bundle_option = $text_bundle_option['label'];
}
if (!empty($text_bundle_options) && !empty($filter_format_options)) {
$form['third_party_settings']['paragraphs_editor'] += [
'#type' => 'fieldset',
'#title' => t('Paragraphs Editor'),
'enabled' => [
'#type' => 'checkbox',
'#title' => t('Editor Enabled'),
'#description' => 'Enables Editor integration for this field.',
'#default_value' => $field_definition->getThirdPartySetting('paragraphs_editor', 'enabled'),
],
'text_bundle' => [
'#type' => 'select',
'#title' => t('Text Bundle'),
'#description' => t('The bundle to use as text (markup) input.'),
'#options' => $text_bundle_options,
'#default_value' => $field_definition->getThirdPartySetting('paragraphs_editor', 'text_bundle'),
'#element_validate' => [
'paragraphs_editor_field_config_edit_form_validate',
],
'#states' => [
'visible' => [
':input[name="third_party_settings[paragraphs_editor][enabled]"]' => ['checked' => TRUE],
],
],
],
'filter_format' => [
'#type' => 'select',
'#title' => 'Default Filter Format',
'#description' => t('The default filter format to use for the editor.'),
'#options' => $filter_format_options,
'#default_value' => $default_format,
'#states' => [
'visible' => [
':input[name="third_party_settings[paragraphs_editor][enabled]"]' => ['checked' => TRUE],
],
],
],
];
}
else {
$form['third_party_settings']['paragraphs_editor'] += [
'message' => [
'#markup' => '<p>' . t('You must create a paragraph type that includes a single field of type "Text (formatted, long)", and make it available to this field in order to use Editor integration with this field.') . '</p>' .
'<p>' . t('This type will be used to save plaintext content entered in the wysiwyg.') . '</p>',
],
];
}
$form['#entity_builders'][] = 'paragraphs_editor_field_config_edit_form_builder';
}
}
}
/**
* Form validator for paragraphs editor settings form.
*/
function paragraphs_editor_field_config_edit_form_validate(&$element, $form_state) {
$parents = $element['#parents'];
array_pop($parents);
$settings = $form_state->getValue($parents);
if (empty($settings['enabled'])) {
return;
}
$text_bundle = $settings['text_bundle'];
$target_bundles = $form_state->getValue([
'settings',
'handler_settings',
'target_bundles',
]);
if (!$target_bundles || !empty($target_bundles[$text_bundle])) {
$text_fields = \Drupal::service('paragraphs_editor.field_value.manager')->getTextFields($text_bundle);
$text_field = reset($text_fields);
if ($text_field) {
$parents = array_slice($element['#parents'], 0, -1);
$parents[] = 'text_field';
$form_state->setValue($parents, $text_field);
return;
}
}
$form_state->setError($element, t('Selected text bundle is not allowed.'));
}
/**
* Entity form builder for paragraphs forms.
*/
function paragraphs_editor_field_config_edit_form_builder($entity_type, $entity, &$form, $form_state) {
$settings = $form_state->getValue(['third_party_settings', 'paragraphs_editor']);
if (empty($settings['enabled'])) {
$entity->unsetThirdPartySetting('paragraphs_editor', 'enabled');
$entity->unsetThirdPartySetting('paragraphs_editor', 'text_bundle');
$entity->unsetThirdPartySetting('paragraphs_editor', 'text_field');
$entity->unsetThirdPartySetting('paragraphs_editor', 'filter_format');
}
}
/**
* Implements hook_preprocess_field().
*/
function paragraphs_editor_preprocess_field(&$variables) {
RenderUtility::preprocessField($variables);
}