-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathParagraphsFeatures.php
More file actions
198 lines (178 loc) · 6.94 KB
/
ParagraphsFeatures.php
File metadata and controls
198 lines (178 loc) · 6.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
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
<?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',
'delete_confirmation',
'hide_row_weight_sort',
'sorting',
'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) {
if (!in_array(
\Drupal::theme()->getActiveTheme()->getName(),
['claro', 'gin']
)) {
return;
}
foreach (static::$availableFeatures as $feature) {
if ($widget->getThirdPartySetting('paragraphs_features', $feature)) {
$elements['add_more']['#attached']['library'][] = 'paragraphs_features/' . $feature;
$elements['add_more']['#attached']['drupalSettings']['paragraphs_features'][$feature][$fieldWrapperId] = TRUE;
$elements['add_more']['#attached']['drupalSettings']['paragraphs_features'][$feature]['_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 = [];
$disabled = FALSE;
if (!in_array(
\Drupal::theme()->getActiveTheme()->getName(),
['claro', 'gin']
)) {
$disabled = TRUE;
}
$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['sorting'] = [
'#type' => 'checkbox',
'#title' => t('Enable checkbox sorting'),
'#default_value' => $plugin->getThirdPartySetting('paragraphs_features', 'sorting'),
'#attributes' => ['class' => ['paragraphs-features__sorting__option']],
'#states' => [
'enabled' => !$modal_related_options_rule && !$disabled,
'visible' => $modal_related_options_rule,
],
'#disabled' => $disabled,
'#description' => $disabled ? t('This feature only works with claro / gin themes.') : '',
];
$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['hide_row_weight_sort'] = [
'#type' => 'checkbox',
'#title' => t('Hide row weight sorting'),
'#default_value' => $plugin->getThirdPartySetting('paragraphs_features', 'hide_row_weight_sort'),
];
return $elements;
}
}