Paragraphs Editor
/home/docs/checkouts/readthedocs.org/user_builds/paragraphs-editor/checkouts/8.x/paragraphs_editor.module
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * Supports editor integration for the paragraphs module.
6  */
7 
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\paragraphs_editor\Utility\RenderUtility;
11 
12 /**
13  * Implements hook_entity_insert().
14  */
15 function paragraphs_editor_entity_insert(EntityInterface $entity) {
16  \Drupal::service('paragraphs_editor.edit_buffer.cache')->processDeletionQueue($entity);
17 }
18 
19 /**
20  * Implements hook_entity_update().
21  */
22 function paragraphs_editor_entity_update(EntityInterface $entity) {
23  \Drupal::service('paragraphs_editor.edit_buffer.cache')->processDeletionQueue($entity);
24 }
25 
26 /**
27  * Implements hook_entity_delete().
28  */
29 function paragraphs_editor_entity_delete(EntityInterface $entity) {
30  \Drupal::service('paragraphs_editor.edit_buffer.cache')->processDeletionQueue($entity);
31 }
32 
33 /**
34  * Implements hook_help().
35  */
36 function paragraphs_editor_help($route_name, RouteMatchInterface $route_match) {
37 }
38 
39 /**
40  * Implements hook_form_FORM_ID_alter().
41  */
42 function paragraphs_editor_form_field_config_edit_form_alter(&$form, $form_state) {
43  $field_definition = $form_state->getBuildInfo()['callback_object']->getEntity();
44  if ($field_definition->getType() == 'entity_reference_revisions') {
45  $target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
46  if ($target_type == 'paragraph') {
47  $form['third_party_settings']['paragraphs_editor'] = [
48  '#type' => 'fieldset',
49  '#title' => t('Paragraphs Editor'),
50  ];
51 
52  $filter_format_options = [];
53  foreach (filter_formats() as $filter_format) {
54  $filter_format_options[$filter_format->id()] = $filter_format->label();
55  }
56  $default_format = $field_definition->getThirdPartySetting('paragraphs_editor', 'filter_format');
57  if (!$default_format) {
58  $default_format = 'paragraphs_ckeditor';
59  }
60 
61  $text_bundle_options = \Drupal::service('paragraphs_editor.field_value.manager')->getTextBundles();
62  foreach ($text_bundle_options as &$text_bundle_option) {
63  $text_bundle_option = $text_bundle_option['label'];
64  }
65  if (!empty($text_bundle_options) && !empty($filter_format_options)) {
66  $form['third_party_settings']['paragraphs_editor'] += [
67  '#type' => 'fieldset',
68  '#title' => t('Paragraphs Editor'),
69  'enabled' => [
70  '#type' => 'checkbox',
71  '#title' => t('Editor Enabled'),
72  '#description' => 'Enables Editor integration for this field.',
73  '#default_value' => $field_definition->getThirdPartySetting('paragraphs_editor', 'enabled'),
74  ],
75  'text_bundle' => [
76  '#type' => 'select',
77  '#title' => t('Text Bundle'),
78  '#description' => t('The bundle to use as text (markup) input.'),
79  '#options' => $text_bundle_options,
80  '#default_value' => $field_definition->getThirdPartySetting('paragraphs_editor', 'text_bundle'),
81  '#element_validate' => [
82  'paragraphs_editor_field_config_edit_form_validate',
83  ],
84  '#states' => [
85  'visible' => [
86  ':input[name="third_party_settings[paragraphs_editor][enabled]"]' => ['checked' => TRUE],
87  ],
88  ],
89  ],
90  'filter_format' => [
91  '#type' => 'select',
92  '#title' => 'Default Filter Format',
93  '#description' => t('The default filter format to use for the editor.'),
94  '#options' => $filter_format_options,
95  '#default_value' => $default_format,
96  '#states' => [
97  'visible' => [
98  ':input[name="third_party_settings[paragraphs_editor][enabled]"]' => ['checked' => TRUE],
99  ],
100  ],
101  ],
102  ];
103  }
104  else {
105  $form['third_party_settings']['paragraphs_editor'] += [
106  'message' => [
107  '#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>' .
108  '<p>' . t('This type will be used to save plaintext content entered in the wysiwyg.') . '</p>',
109  ],
110  ];
111  }
112 
113  $form['#entity_builders'][] = 'paragraphs_editor_field_config_edit_form_builder';
114  }
115  }
116 }
117 
118 /**
119  * Form validator for paragraphs editor settings form.
120  */
121 function paragraphs_editor_field_config_edit_form_validate(&$element, $form_state) {
122  $parents = $element['#parents'];
123  array_pop($parents);
124  $settings = $form_state->getValue($parents);
125 
126  if (empty($settings['enabled'])) {
127  return;
128  }
129 
130  $text_bundle = $settings['text_bundle'];
131  $target_bundles = $form_state->getValue([
132  'settings',
133  'handler_settings',
134  'target_bundles',
135  ]);
136  if (!$target_bundles || !empty($target_bundles[$text_bundle])) {
137  $text_fields = \Drupal::service('paragraphs_editor.field_value.manager')->getTextFields($text_bundle);
138  $text_field = reset($text_fields);
139  if ($text_field) {
140  $parents = array_slice($element['#parents'], 0, -1);
141  $parents[] = 'text_field';
142  $form_state->setValue($parents, $text_field);
143  return;
144  }
145  }
146 
147  $form_state->setError($element, t('Selected text bundle is not allowed.'));
148 }
149 
150 /**
151  * Entity form builder for paragraphs forms.
152  */
153 function paragraphs_editor_field_config_edit_form_builder($entity_type, $entity, &$form, $form_state) {
154  $settings = $form_state->getValue(['third_party_settings', 'paragraphs_editor']);
155  if (empty($settings['enabled'])) {
156  $entity->unsetThirdPartySetting('paragraphs_editor', 'enabled');
157  $entity->unsetThirdPartySetting('paragraphs_editor', 'text_bundle');
158  $entity->unsetThirdPartySetting('paragraphs_editor', 'text_field');
159  $entity->unsetThirdPartySetting('paragraphs_editor', 'filter_format');
160  }
161 }
162 
163 /**
164  * Implements hook_preprocess_field().
165  */
166 function paragraphs_editor_preprocess_field(&$variables) {
167  RenderUtility::preprocessField($variables);
168 }