/
d_geysir.module
347 lines (314 loc) · 12.3 KB
/
d_geysir.module
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
* @file
* Contains d_geysir.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Form\ViewsForm;
use Drupal\views\ViewExecutable;
/**
* Implements hook_form_alter().
*/
function d_geysir_form_alter(&$form, FormStateInterface $form_state, $form_id) {
switch ($form_id) {
case 'geysir.modal.add_select_type_form':
$root_path = DRUPAL_ROOT;
$base_url = \Drupal::service('router.request_context')->getBaseUrl();
$module_path = drupal_get_path('module', 'd_p');
foreach ($form['description'] as $paragraph_name => $attributes) {
$file_server_path = $root_path . '/' . $module_path . '/images/' . $paragraph_name . '.png';
if (file_exists($file_server_path)) {
$file_url = $base_url . $module_path . '/images/' . $paragraph_name . '.png';
$form['description'][$paragraph_name]['#src'] = $file_url;
}
}
break;
}
$form_object = $form_state->getFormObject();
if ($form_object instanceof ViewsForm && strpos($form_object->getBaseFormId(), 'views_form_media_library') === 0) {
if (isset($form['header'])) {
$form['header']['#attributes']['class'][] = 'media-library-views-form__header';
$form['header']['media_bulk_form']['#attributes']['class'][] = 'media-library-views-form__bulk_form';
}
$form['actions']['submit']['#attributes']['class'] = ['media-library-select'];
}
// Add after build to add a CSS class to the form actions.
if ($form_id === 'views_exposed_form' && strpos($form['#id'], 'views-exposed-form-media-library-widget') === 0) {
$form['actions']['#attributes']['class'][] = 'media-library-view--form-actions';
}
}
/**
* Implements hook_page_attachments().
*/
function d_geysir_page_attachments(array &$page) {
if (!\Drupal::currentUser()->hasPermission('geysir manage paragraphs from front-end')) {
return;
}
$page['#attached']['library'][] = 'd_geysir/media';
}
/**
* Implements hook_field_widget_link_attributes_form_alter().
*/
function d_geysir_field_widget_link_attributes_form_alter(&$element, &$form_state, $context) {
// Link attributes always closed.
$element["options"]["attributes"]["#open"] = 0;
}
/**
* Implements hook_field_widget_form_alter().
* Disable premature autofocus at modal's forms.
*/
function d_geysir_field_widget_form_alter(&$element, &$form_state, $context) {
$element["open_button"]["#attributes"]["data-disabled-focus"] = 'false';
}
/**
* Implements hook_toolbar().
*/
function d_geysir_toolbar() {
$items = [];
$items['geysir_toggle'] = [
'#cache' => [
'contexts' => [
'user.permissions',
],
],
];
$route = \Drupal::routeMatch()->getRouteObject();
$is_admin = \Drupal::service('router.admin_context')->isAdminRoute($route);
if (!\Drupal::currentUser()
->hasPermission('geysir manage paragraphs from front-end') || $is_admin) {
return $items;
}
$items['geysir_toggle'] += [
'#type' => 'toolbar_item',
'tab' => [
'#type' => 'html_tag',
'#tag' => 'button',
'#value' => t('Paragraph overlay'),
'#attributes' => [
'aria-pressed' => 'true',
'type' => 'button',
'class' => [
'toolbar-icon',
'toolbar-icon-edit',
'toolbar-geysir-toggle',
'is-active',
],
],
],
'#wrapper_attributes' => [
'class' => ['hidden', 'd-geysir-toolbar-tab'],
],
'#attached' => [
'library' => [
'd_geysir/toggle_link',
],
],
];
return $items;
}
/**
* Implements hook_module_implements_alter().
*/
function d_geysir_module_implements_alter(&$implementations, $hook) {
// Move d_geysir implementation of hook_entity_type_build() to the end of list
// to override geysir_modal_edit form class.
if ($hook == 'entity_type_build') {
$group = $implementations['d_geysir'];
unset($implementations['d_geysir']);
$implementations['d_geysir'] = $group;
}
}
/**
* Implements hook_entity_type_build().
*/
function d_geysir_entity_type_build(array &$entity_types) {
/* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
// Override geysir_modal_edit form class.
$entity_types['paragraph']->setFormClass('geysir_modal_edit', '\Drupal\d_geysir\Form\GeysirCustomModalParagraphForm');
}
/**
* Implements hook_preprocess_media_library_item__widget().
*
* This targets each media item selected in an entity reference field.
*
* @see seven_preprocess_media_library_item__widget()
*/
function d_geysir_preprocess_media_library_item__widget(array &$variables) {
$variables['content']['remove_button']['#attributes']['class'][] = 'media-library-item__remove';
$variables['content']['remove_button']['#attributes']['class'][] = 'no-bootstrap';
}
/**
* Implements hook_preprocess_views_view_fields().
*
* This targets each rendered media item in the grid display of the media
* library's modal dialog.
*
* @see seven_preprocess_views_view_fields__media_library()
*/
function d_geysir_preprocess_views_view_fields__media_library(array &$variables) {
// Add classes to media rendered entity field so it can be targeted for
// styling. Adding this class in a template is very difficult to do.
if (isset($variables['fields']['rendered_entity']->wrapper_attributes)) {
$variables['fields']['rendered_entity']->wrapper_attributes->addClass('media-library-item__click-to-select-trigger');
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* @see seven_form_media_library_add_form_alter()
*/
function d_geysir_form_media_library_add_form_alter(array &$form, FormStateInterface $form_state) {
$form['#attributes']['class'][] = 'media-library-add-form';
// If there are unsaved media items, apply styling classes to various parts
// of the form.
if (isset($form['media'])) {
$form['#attributes']['class'][] = 'media-library-add-form--with-input';
// Put a wrapper around the informational message above the unsaved media
// items.
$form['description']['#template'] = '<p class="media-library-add-form__description">{{ text }}</p>';
}
else {
$form['#attributes']['class'][] = 'media-library-add-form--without-input';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @see seven_form_media_library_add_form_upload_alter()
*/
function d_geysir_form_media_library_add_form_upload_alter(array &$form, FormStateInterface $form_state) {
$form['#attributes']['class'][] = 'media-library-add-form--upload';
if (isset($form['container'])) {
$form['container']['#attributes']['class'][] = 'media-library-add-form__input-wrapper';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @see seven_form_media_library_add_form_oembed_alter()
*/
function d_geysir_form_media_library_add_form_oembed_alter(array &$form, FormStateInterface $form_state) {
$form['attributes']['class'][] = 'media-library-add-form--oembed';
// If no media items have been added yet, add a couple of styling classes
// to the initial URL form.
if (isset($form['container'])) {
$form['container']['#attributes']['class'][] = 'media-library-add-form__input-wrapper';
$form['container']['url']['#attributes']['class'][] = 'media-library-add-form-oembed-url';
$form['container']['submit']['#attributes']['class'][] = 'media-library-add-form-oembed-submit';
}
}
/**
* Implements hook_preprocess_item_list__media_library_add_form_media_list().
*
* This targets each new, unsaved media item added to the media library, before
* they are saved.
*
* @see seven_preprocess_item_list__media_library_add_form_media_list()
*/
function d_geysir_preprocess_item_list__media_library_add_form_media_list(array &$variables) {
foreach ($variables['items'] as &$item) {
$item['value']['preview']['#attributes']['class'][] = 'media-library-add-form__preview';
$item['value']['fields']['#attributes']['class'][] = 'media-library-add-form__fields';
$item['value']['remove_button']['#attributes']['class'][] = 'media-library-add-form__remove-button';
$item['value']['remove_button']['#attributes']['class'][] = 'no-bootstrap';
// #source_field_name is set by AddFormBase::buildEntityFormElement()
// to help themes and form_alter hooks identify the source field.
$fields = &$item['value']['fields'];
$source_field_name = $fields['#source_field_name'];
if (isset($fields[$source_field_name])) {
$fields[$source_field_name]['#attributes']['class'][] = 'media-library-add-form__source-field';
}
}
}
/**
* Implements hook_preprocess_media_library_item__small().
*
* This targets each pre-selected media item selected when adding new media in
* the modal media library dialog.
*
* @see seven_preprocess_media_library_item__small()
*/
function d_geysir_preprocess_media_library_item__small(array &$variables) {
$variables['content']['select']['#attributes']['class'][] = 'media-library-item__click-to-select-checkbox';
}
/**
* @todo Remove this when https://www.drupal.org/project/drupal/issues/2999549
* lands.
*
* @see \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::formElement()
* @see seven_preprocess_fieldset__media_library_widget
*/
function d_geysir_preprocess_fieldset__media_library_widget(array &$variables) {
if (isset($variables['prefix']['weight_toggle'])) {
$variables['prefix']['weight_toggle']['#attributes']['class'][] = 'media-library-widget__toggle-weight';
}
if (isset($variables['suffix']['open_button'])) {
$variables['suffix']['open_button']['#attributes']['class'][] = 'media-library-open-button';
$variables['suffix']['open_button']['#attributes']['class'][] = 'button-override';
}
}
/**
* Implements hook_views_pre_render().
*
* @see seven_views_pre_render();
*/
function d_geysir_views_pre_render(ViewExecutable $view) {
$add_classes = function (&$option, array $classes_to_add) {
$classes = preg_split('/\s+/', $option);
$classes = array_filter($classes);
$classes = array_merge($classes, $classes_to_add);
$option = implode(' ', array_unique($classes));
};
if ($view->id() === 'media_library') {
if ($view->display_handler->options['defaults']['css_class']) {
$add_classes($view->displayHandlers->get('default')->options['css_class'], ['media-library-view']);
}
else {
$add_classes($view->display_handler->options['css_class'], ['media-library-view']);
}
if ($view->current_display === 'page') {
if (array_key_exists('media_bulk_form', $view->field)) {
$add_classes($view->field['media_bulk_form']->options['element_class'], ['media-library-item__click-to-select-checkbox']);
}
if (array_key_exists('rendered_entity', $view->field)) {
$add_classes($view->field['rendered_entity']->options['element_class'], ['media-library-item__content']);
}
if (array_key_exists('edit_media', $view->field)) {
$add_classes($view->field['edit_media']->options['alter']['link_class'], ['media-library-item__edit']);
}
if (array_key_exists('delete_media', $view->field)) {
$add_classes($view->field['delete_media']->options['alter']['link_class'], ['media-library-item__remove']);
}
}
elseif (strpos($view->current_display, 'widget') === 0) {
if (array_key_exists('rendered_entity', $view->field)) {
$add_classes($view->field['rendered_entity']->options['element_class'], ['media-library-item__content']);
}
if (array_key_exists('media_library_select_form', $view->field)) {
$add_classes($view->field['media_library_select_form']->options['element_wrapper_class'], ['media-library-item__click-to-select-checkbox']);
}
if ($view->display_handler->options['defaults']['css_class']) {
$add_classes($view->displayHandlers->get('default')->options['css_class'], ['media-library-view--widget']);
}
else {
$add_classes($view->display_handler->options['css_class'], ['media-library-view--widget']);
}
}
}
}
/**
* Implements hook_preprocess_links__media_library_menu().
*
* This targets the menu of available media types in the media library's modal
* dialog.
*
* @todo Do this in the relevant template once
* https://www.drupal.org/project/drupal/issues/3088856 is resolved.
*
* @see classy_preprocess_links__media_library_menu()
*/
function d_geysir_preprocess_links__media_library_menu(array &$variables) {
foreach ($variables['links'] as &$link) {
$link['link']['#options']['attributes']['class'][] = 'media-library-menu__link';
}
}