-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathParagraphsFeatures.php
More file actions
219 lines (198 loc) · 8.42 KB
/
ParagraphsFeatures.php
File metadata and controls
219 lines (198 loc) · 8.42 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
<?php
namespace Drupal\paragraphs_features;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InsertCommand;
use Drupal\paragraphs_features\Ajax\ScrollToElementCommand;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget;
/**
* Paragraphs features class.
*/
class ParagraphsFeatures {
/**
* List of available paragraphs features.
*
* @var array
*/
public static $availableFeatures = [
'add_in_between',
'behaviors_action_button',
'delete_confirmation',
'split_text',
];
/**
* Getting paragraphs widget wrapper ID.
*
* Logic is copied from paragraphs module.
*
* @param array $parents
* List of parents for widget.
* @param string $field_name
* Widget field name.
*
* @return string
* Returns widget wrapper ID.
*/
public static function getWrapperId(array $parents, $field_name) {
return Html::getId(implode('-', array_merge($parents, [$field_name])) . '-add-more-wrapper');
}
/**
* Register features for paragraphs field widget.
*
* @param array $elements
* Render array for the field widget.
* @param \Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget $widget
* Field widget object.
* @param string $fieldWrapperId
* Field Wrapper ID, usually provided by ::getWrapperId().
*/
public static function registerFormWidgetFeatures(array &$elements, ParagraphsWidget $widget, $fieldWrapperId) {
foreach (static::$availableFeatures as $feature) {
if ($widget->getThirdPartySetting('paragraphs_features', $feature)) {
$elements['add_more']['#attached']['library'][] = 'paragraphs_features/drupal.paragraphs_features.' . $feature;
$elements['add_more']['#attached']['drupalSettings']['paragraphs_features'][$feature][$fieldWrapperId] = ['wrapperId' => $fieldWrapperId];
}
if ($feature === 'add_in_between') {
$elements['add_more']['#attached']['drupalSettings']['paragraphs_features'][$feature][$fieldWrapperId]['linkCount'] =
$widget->getThirdPartySetting('paragraphs_features', 'add_in_between_link_count');
}
if ($feature === 'behaviors_action_button') {
// Remove paragraphs tabs.
// @see ParagraphsWidget->formMultipleElements().
$elements['#prefix'] = '<div class="is-horizontal paragraphs-tabs-wrapper" id="' . $fieldWrapperId . '">';
// Add a button for each subform.
foreach (Element::children($elements) as $key) {
if (isset($elements[$key]['behavior_plugins']) && Element::children($elements[$key]['behavior_plugins'])) {
$elements[$key]['top']['actions']['actions']['behaviors_button'] = [
'#type' => 'button',
'#value' => t('Settings'),
'#weight' => -100,
'#limit_validation_errors' => [],
'#delta' => $elements[$key]['#delta'],
'#access' => \Drupal::currentUser()->hasPermission('edit behavior plugin settings'),
'#attributes' => [
'class' => [
'js-paragraphs-button-behaviors',
],
'title' => t('Settings'),
],
];
}
}
}
// Set module path for split_text feature.
$elements['add_more']['#attached']['drupalSettings']['paragraphs_features']['_path'] = drupal_get_path('module', 'paragraphs_features');
}
$elements['add_more']['#attached']['library'][] = 'paragraphs_features/drupal.paragraphs_features.scroll_to_element';
foreach (Element::children($elements['add_more']) as $button) {
$elements['add_more'][$button]['#ajax']['callback'] = [
static::class, 'addMoreAjax',
];
}
// This feature is not part of of the foreach above, since it is not a
// javascript feature, it is a direct modification of the form. If the
// feature is not set, it defaults back to paragraphs behavior.
if (!empty($elements['header_actions']['dropdown_actions']['dragdrop_mode'])) {
$elements['header_actions']['dropdown_actions']['dragdrop_mode']['#access'] = (bool) $widget->getThirdPartySetting('paragraphs_features', 'show_drag_and_drop', TRUE);
}
}
/**
* Adds a scroll event to the ajax response.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The ajax response with the paragraph to add.
*/
public static function addMoreAjax(array $form, FormStateInterface $form_state) {
$element = ParagraphsWidget::addMoreAjax($form, $form_state);
$response = new AjaxResponse();
$response->addCommand(new InsertCommand(NULL, $element));
$response->addCommand(new ScrollToElementCommand($element[$element['#max_delta']]['#attributes']['data-drupal-selector'], $element['#attributes']['data-drupal-selector']));
return $response;
}
/**
* Get 3rd party setting form for paragraphs features.
*
* @param \Drupal\Core\Field\WidgetInterface $plugin
* Widget plugin.
* @param string $field_name
* Field name.
*
* @return array
* Returns 3rd party form elements.
*/
public static function getThirdPartyForm(WidgetInterface $plugin, $field_name) {
$elements = [];
$elements['delete_confirmation'] = [
'#type' => 'checkbox',
'#title' => t('Enable confirmation on paragraphs remove'),
'#default_value' => $plugin->getThirdPartySetting('paragraphs_features', 'delete_confirmation'),
'#attributes' => ['class' => ['paragraphs-features__delete-confirmation__option']],
];
// Define rule for enabling/disabling options that depend on modal add mode.
$modal_related_options_rule = [
':input[name="fields[' . $field_name . '][settings_edit_form][settings][add_mode]"]' => [
'value' => 'modal',
],
];
$elements['add_in_between'] = [
'#type' => 'checkbox',
'#title' => t('Enable add in between buttons'),
'#default_value' => $plugin->getThirdPartySetting('paragraphs_features', 'add_in_between'),
'#attributes' => ['class' => ['paragraphs-features__add-in-between__option']],
'#states' => [
'enabled' => $modal_related_options_rule,
'visible' => $modal_related_options_rule,
],
];
$elements['add_in_between_link_count'] = [
'#type' => 'number',
'#title' => t('Number of add in between links', [], ['context' => 'Paragraphs Editor Enhancements']),
'#default_value' => $plugin->getThirdPartySetting('paragraphs_features', 'add_in_between_link_count', 3),
'#min' => 0,
'#attributes' => ['class' => ['paragraphs-features__add-in-between__option']],
'#states' => [
'enabled' => [
':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][paragraphs_features][add_in_between]"]' => [
'checked' => TRUE,
],
],
'visible' => $modal_related_options_rule,
],
'#description' => t('Set the number of buttons available to directly add a paragraph.'),
];
$elements['split_text'] = [
'#type' => 'checkbox',
'#title' => t('Enable split text for text paragraphs'),
'#default_value' => $plugin->getThirdPartySetting('paragraphs_features', 'split_text'),
'#attributes' => ['class' => ['paragraphs-features__split-text__option']],
'#states' => [
'enabled' => $modal_related_options_rule,
'visible' => $modal_related_options_rule,
],
];
// Only show the drag & drop feature if we can find the sortable library.
$library_discovery = \Drupal::service('library.discovery');
$library = $library_discovery->getLibraryByName('paragraphs', 'paragraphs-dragdrop');
$elements['show_drag_and_drop'] = [
'#type' => 'checkbox',
'#title' => t('Show drag & drop button'),
'#default_value' => $plugin->getThirdPartySetting('paragraphs_features', 'show_drag_and_drop', TRUE),
'#access' => !empty($library),
];
$elements['behaviors_action_button'] = [
'#type' => 'checkbox',
'#title' => t('Switch paragraphs content/behavior tabs to a behaviors action button'),
'#default_value' => $plugin->getThirdPartySetting('paragraphs_features', 'behaviors_action_button', FALSE),
'#access' => !empty($library),
];
return $elements;
}
}