-
Notifications
You must be signed in to change notification settings - Fork 1
/
classier_paragraphs.module
74 lines (65 loc) · 2.25 KB
/
classier_paragraphs.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
<?php
/**
* @file
* Main module file for Classy Paragraphs.
*/
use \Drupal\Component\Utility\Html;
use \Drupal\paragraphs\Entity\Paragraph;
/**
* Implements hook_field_widget_PLUGIN_ID_form_alter().
*/
function classier_paragraphs_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, &$context) {
$element['#attached']['library'][] = 'classier_paragraphs/classier_paragraphs.admin';
}
/**
* Prepares variables for paragraph templates.
*
* Default template: paragraph.html.twig.
*
* Most themes use their own copy of paragraph.html.twig. The default is located
* inside "templates/paragraph.html.twig". Look in there for the
* full list of variables.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
* - paragraph: The paragraph object.
* - view_mode: View mode; e.g., 'full', 'teaser'...
*/
function classier_paragraphs_preprocess_paragraph(&$variables) {
if (!isset($variables['attributes']['class'])) {
$variables['attributes']['class'] = [];
}
$classes = classier_paragraphs_get_classes($variables['paragraph']);
$variables['attributes']['class'] = array_merge($variables['attributes']['class'], $classes);
}
/**
* Get class names from paragraph item.
*
* @param Paragraph $paragraph
* @return array
*/
function classier_paragraphs_get_classes(Paragraph $paragraph) {
$field_definitions = $paragraph->getFieldDefinitions();
$instances = field_entity_bundle_field_info($paragraph->getEntityType(), $paragraph->getType(), $field_definitions);
foreach ($instances as $instance) {
if ($instance->getType() == 'classier_paragraphs') {
$field = $paragraph->get($instance->getName());
if ($field->count()) {
$items = $field->get(0)->getValue();
// Merge arrays so we can use checkbox field with multiple values
foreach ($items as $key => $item) {
if (is_array($item)) {
$items = array_merge($items, $item);
unset($items[$key]);
}
}
// Filter out internal keys starting with '_'.
return array_filter($items, function ($key) {
return substr($key, 0, 1) !== '_';
}, ARRAY_FILTER_USE_KEY);
}
}
}
return array();
}