-
Notifications
You must be signed in to change notification settings - Fork 5
/
paragraphs_features.module
81 lines (67 loc) · 2.96 KB
/
paragraphs_features.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
<?php
/**
* @file
* Contains hooks for Paragraphs Feature module.
*/
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget;
use Drupal\paragraphs_features\ParagraphsFeatures;
/**
* Implements hook_field_widget_complete_form_alter().
*/
function paragraphs_features_field_widget_complete_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
/** @var \Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget $widget */
$widget = $context['widget'];
if (!($widget instanceof ParagraphsWidget)) {
return;
}
// For compatibility with Drag & Drop in Paragraphs.
if (!empty($field_widget_complete_form['widget']['#field_name'])) {
$fieldWrapperId = ParagraphsFeatures::getWrapperId($context['form']['#parents'], $field_widget_complete_form['widget']['#field_name']);
ParagraphsFeatures::registerFormWidgetFeatures($field_widget_complete_form['widget'], $widget, $fieldWrapperId);
}
}
/**
* Implements hook_field_widget_third_party_settings_form().
*/
function paragraphs_features_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
$elements = [];
if ($plugin instanceof ParagraphsWidget) {
$elements = ParagraphsFeatures::getThirdPartyForm($plugin, $field_definition->getName());
}
return $elements;
}
/**
* Implements hook_paragraphs_widget_actions_alter().
*/
function paragraphs_features_paragraphs_widget_actions_alter(array &$widget_actions, array &$context) {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraphs_entity */
$paragraphs_entity = $context['paragraphs_entity'];
foreach ($widget_actions as $grouping => $buttons) {
foreach ($buttons as $button_id => $button_element) {
if ($button_id === 'remove_button') {
$widget_actions[$grouping][$button_id]['#attributes']['data-paragraphs-split-text-type'] = $paragraphs_entity->getType();
break 2;
}
}
}
/* Render single option for dropdown as button. */
// Get configuration setting for reducing dropdown to button on single option.
$dropdown_to_button = \Drupal::config('paragraphs_features.settings')->get('dropdown_to_button');
if (!$dropdown_to_button) {
return;
}
// "Add above" feature is added by JS and we don't have it in actions list.
if ($context['element']['top']['#attributes']['class'] && in_array('add-above-on', $context['element']['top']['#attributes']['class'])) {
return;
}
$visible_actions = Element::getVisibleChildren($widget_actions['dropdown_actions']);
if (count($visible_actions) === 1) {
$visible_actions = reset($visible_actions);
$widget_actions['actions'][$visible_actions] = $widget_actions['dropdown_actions'][$visible_actions];
$widget_actions['dropdown_actions'] = [];
}
}