This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icon_select_field.module
82 lines (72 loc) · 2.3 KB
/
icon_select_field.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
<?php
/**
* @file
* Contains icon_select_field.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\RenderElement;
/**
* Implements hook_help().
*/
function icon_select_field_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the icon_select_field module.
case 'help.page.icon_select_field':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Use jQuery plugin Select2 dropdown to select an icon from a given list. See the settings page to enter some values') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function icon_select_field_theme() {
return [
'icon_select_select' => [
'render element' => 'element',
'template' => 'icon-select-select',
'base hook' => 'select',
],
];
}
/**
* Implements hook_page_attachments().
*
* Add library to the edit node page.
* Fields may be loaded lazy resulting in missing library, if only defined in
* twig template.
*
* @see contextual_preprocess()
*/
function icon_select_field_page_attachments(array &$page) {
if (!\Drupal::currentUser()->isAuthenticated()) {
return;
}
$page['#attached']['library'][] = 'icon_select_field/select';
}
/**
* Prepares variables for select element templates.
*
* Default template: select.html.twig.
*
* It is possible to group options together; to do this, change the format of
* $options to an associative array in which the keys are group labels, and the
* values are associative arrays in the normal $options format.
*
* @param $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #extra,
* #multiple, #required, #name, #attributes, #size.
*/
function icon_select_field_preprocess_icon_select_select(&$variables) {
$element = $variables['element'];
Element::setAttributes($element, ['id', 'name', 'size']);
RenderElement::setAttributes($element, ['form-select']);
$variables['icon_select_tag_list'] = $element['#icon_select_tag_list'];
$variables['attributes'] = $element['#attributes'];
$variables['options'] = form_select_options($element);
}