-
Notifications
You must be signed in to change notification settings - Fork 5
/
multiselect.module
124 lines (112 loc) · 4.35 KB
/
multiselect.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
<?php
/**
* @file
* Allows users to select multiple items in an easier way than the normal node-reference widget.
*/
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Drupal\multiselect\Element\Multiselect;
/**
* Implements hook_help().
*/
function multiselect_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.multiselect':
$output = '';
$output .= '<p>' . t('Provides a CCK and Form API widget for editing fields that allows users to select from a list of options in a left box and have them visually moved into the right box when options are chosen.') . '</p>';
$output .= '<h3>' . t('Methods of Implementing a Multiselect Widget') . '</h3>';
$output .= '<dl>';
$output .= '<dt><h5>' . t('Method 1: Using CCK') . '</h5></dt>';
$output .= '<dd>' . t('When creating a new content field, select "Multiselect" as your widget type. You can use Multiselect on fields of type "list", "list_text", "list_number", "node_reference", "taxonomy_term_reference", and "user_reference".') . '</dd>';
$output .= '<dt><h5>' . t('Method 2: Coding Your Own Module') . '</h5></dt>';
$output .= '<dd>' . t('If you\'re developing a custom module and wish to use the Multiselect widget in place of a traditional "select" widget, you may use the Drupal 8 Form API.') . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_page_build().
*/
function multiselect_page_build(&$page) {
$config = \Drupal::config('multiselect.settings');
$page['#attached']['js'][] = array(
'data' => array(
'multiselect' => array('widths' => $config->get('multiselect.widths')),
),
'type' => 'setting',
);
}
/**
* Implements hook_theme().
*/
function multiselect_theme() {
return array(
'multiselect' => array(
'arguments' => array('element' => NULL),
'render element' => 'element',
'template' => 'multiselect',
),
);
}
/**
* Prepares variables for multiselect element templates.
*
* Default template: multiselect.html.twig.
*
* @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 template_preprocess_multiselect(&$variables) {
$element = $variables['element'];
Element::setAttributes($element, array('id', 'name', 'size', 'required'));
$available_options = Multiselect::getOptions('available', $element);
$available_size = min(count($available_options), 10);
$selected_options = Multiselect::getOptions('selected', $element);
$selected_size = min(count($selected_options), 10);
$total_size = $available_size + $selected_size;
$variables['multiselect'] = array(
'available' => array(
'id' => $element['#attributes']['id'] . '-available',
'label' => t('Available Options'),
'attributes' => array(
'id' => $element['#attributes']['id'] . '-available',
'size' => $total_size,
),
'options' => $available_options,
),
'selected' => array(
'id' => $element['#attributes']['id'],
'label' => t('Selected Options'),
'attributes' => $element['#attributes'],
'options' => $selected_options,
),
'labels' => array(
'add' => t('Add'),
'remove' => t('Remove'),
),
);
// Prepare selected attributes.
$variables['multiselect']['selected']['attributes']['size'] = $total_size;
// Prepare attributes for available select.
foreach (array('multiple', 'required', 'class') as $key) {
$element_key = "#{$key}";
if (isset($element[$element_key])) {
$variables['multiselect']['available']['attributes'][$key] = $element[$element_key];
}
}
// Prepare attributes.
$multiselect = &$variables['multiselect'];
foreach (array('available', 'selected') as $key) {
$multiselect[$key]['attributes']['class'][] = 'multiselect-' . $key;
$multiselect[$key]['attributes']['class'][] = 'form-multiselect';
if (isset($multiselect[$key]['attributes']) && !($multiselect[$key]['attributes'] instanceof Attribute)) {
if ($multiselect[$key]['attributes']) {
$multiselect[$key]['attributes'] = new Attribute($multiselect[$key]['attributes']);
}
}
}
}