5 * Supports editor integration for the paragraphs module.
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\paragraphs_editor\Utility\RenderUtility;
13 * Implements hook_entity_insert().
15 function paragraphs_editor_entity_insert(EntityInterface $entity) {
16 \Drupal::service('paragraphs_editor.edit_buffer.cache')->processDeletionQueue($entity);
20 * Implements hook_entity_update().
22 function paragraphs_editor_entity_update(EntityInterface $entity) {
23 \Drupal::service('paragraphs_editor.edit_buffer.cache')->processDeletionQueue($entity);
27 * Implements hook_entity_delete().
29 function paragraphs_editor_entity_delete(EntityInterface $entity) {
30 \Drupal::service('paragraphs_editor.edit_buffer.cache')->processDeletionQueue($entity);
34 * Implements hook_help().
36 function paragraphs_editor_help($route_name, RouteMatchInterface $route_match) {
40 * Implements hook_form_FORM_ID_alter().
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'),
52 $filter_format_options = [];
53 foreach (filter_formats() as $filter_format) {
54 $filter_format_options[$filter_format->id()] = $filter_format->label();
56 $default_format = $field_definition->getThirdPartySetting('paragraphs_editor', 'filter_format');
57 if (!$default_format) {
58 $default_format = 'paragraphs_ckeditor';
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'];
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'),
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'),
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',
86 ':input[name="third_party_settings[paragraphs_editor][enabled]"]' => ['checked' => TRUE],
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,
98 ':input[name="third_party_settings[paragraphs_editor][enabled]"]' => ['checked' => TRUE],
105 $form['third_party_settings']['paragraphs_editor'] += [
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>',
113 $form['#entity_builders'][] = 'paragraphs_editor_field_config_edit_form_builder';
119 * Form validator for paragraphs editor settings form.
121 function paragraphs_editor_field_config_edit_form_validate(&$element, $form_state) {
122 $parents = $element['#parents'];
124 $settings = $form_state->getValue($parents);
126 if (empty($settings['enabled'])) {
130 $text_bundle = $settings['text_bundle'];
131 $target_bundles = $form_state->getValue([
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);
140 $parents = array_slice($element['#parents'], 0, -1);
141 $parents[] = 'text_field';
142 $form_state->setValue($parents, $text_field);
147 $form_state->setError($element, t('Selected text bundle is not allowed.'));
151 * Entity form builder for paragraphs forms.
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');
164 * Implements hook_preprocess_field().
166 function paragraphs_editor_preprocess_field(&$variables) {
167 RenderUtility::preprocessField($variables);