-
Notifications
You must be signed in to change notification settings - Fork 4
/
grid_field_formatter.module
202 lines (177 loc) · 8.05 KB
/
grid_field_formatter.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
<?php
/**
* @file
* Module that gives you a field formatter, which enables field layout with
* Grid contents to a destination.
*/
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Template\Attribute;
define('GRID_FIELD_FORMATTER_DEFAULT_COLUMNS', 1);
/**
* Implements hook_field_formatter_third_party_settings_form().
*/
function grid_field_formatter_field_formatter_third_party_settings_form(FormatterInterface $plugin, FieldDefinitionInterface $field_definition, $view_mode, $form, FormStateInterface $form_state) {
$element = [];
// Add Grid Field Formatter if matching an enabled field type.
if (grid_field_formatter_is_eligible($field_definition)) {
$default_values = $plugin->getThirdPartySetting('grid_field_formatter', 'grid_field_formatter');
$element['grid_field_formatter'] = [
'#type' => 'container',
'#attributes' => ['class' => ['grided-field-wrapper']],
];
// Add a checkbox to the formatter settings form to enabled a grid layout.
$element['grid_field_formatter']['grid_enable'] = [
'#title' => t('Enable multi-value field display with a grid layout.'),
'#type' => 'checkbox',
'#default_value' => isset($default_values['grid_enable']) ? $default_values['grid_enable'] : NULL,
];
// Add a conditional text field for the number of columns of the grid.
$element['grid_field_formatter']['columns'] = [
'#title' => t('Grid layout number of columns'),
'#type' => 'textfield',
'#default_value' => isset($default_values['columns']) ? $default_values['columns'] : GRID_FIELD_FORMATTER_DEFAULT_COLUMNS,
'#description' => t('Enter the number of columns for each row for the grid table layout.'),
'#element_validate' => ['grid_field_formatter_element_validate_columns'],
'#states' => [
'visible' => [
'input[name$="[grid_field_formatter][grid_field_formatter][grid_enable]"]' => ['checked' => TRUE],
],
],
];
}
return $element;
}
/**
* Form element validation handler for columns field in settings form.
*/
function grid_field_formatter_element_validate_columns($element, FormStateInterface $form_state) {
$value = $element['#value'];
// The number of columns must be valid number and between 1 and 12.
if (!(is_numeric($value) && $value > 0 && $value < 13)) {
$form_state->setError($element, t('The number of columns must be valid number and between 1 and 12.'));
}
}
/**
* Implements hook_field_formatter_settings_summary_alter().
*/
function grid_field_formatter_field_formatter_settings_summary_alter(&$summary, $context) {
if (grid_field_formatter_is_eligible($context['field_definition'])) {
$settings = $context['formatter']->getThirdPartySetting('grid_field_formatter', 'grid_field_formatter');
if (isset($settings['grid_enable']) && $settings['grid_enable']) {
$summary[] = t('Multi-value Grid display: enabled');
$summary[] = t('Number of columns: @columns', ['@columns' => $settings['columns']]);
}
}
}
/**
* Implements hook_entity_display_build_alter().
*/
function grid_field_formatter_entity_display_build_alter(&$build, $context) {
$entity = $context['entity'];
// Only act on content entities.
if (!($entity instanceof FieldableEntityInterface)) {
return;
}
// Append RDF term mappings on displayed taxonomy links.
foreach (Element::children($build) as $field_name) {
$field = &$build[$field_name];
$field_definition = $entity->getFieldDefinition($field_name);
if (grid_field_formatter_is_eligible($field_definition)) {
// get the field formatter settings...
$entity_display = EntityViewDisplay::collectRenderDisplay($entity, $context['view_mode']);
$field_display = $entity_display->getComponent($field_name);
// Get Grid Field Formatter settings.
$grid_enabled = isset($field_display['third_party_settings']['grid_field_formatter']['grid_field_formatter']['grid_enable']) ? $field_display['third_party_settings']['grid_field_formatter']['grid_field_formatter']['grid_enable'] : FALSE;
$columns = isset($field_display['third_party_settings']['grid_field_formatter']['grid_field_formatter']['columns']) ? $field_display['third_party_settings']['grid_field_formatter']['grid_field_formatter']['columns'] : GRID_FIELD_FORMATTER_DEFAULT_COLUMNS;
if ($grid_enabled) {
$field['#theme'] = 'grid_field_formatter';
$field['#columns'] = $columns;
}
}
}
}
/**
* Theme preprocess function for grid_field_formatter.
*
* This function essentially extends the core field module theming methods, in
* particular template_preprocess_field. It also introduces new field template
* suggestions such as:
* - grid-field-formatter.tpl.php
* - grid-field-formatter--[FIELD_TYPE].tpl.php
* - grid-field-formatter--[FIELD_NAME].tpl.php
* - grid-field-formatter--[FIELD_NAME]--[BUNDLE].tpl.php
*
* @see grid-field-formatter.tpl.php
*/
function template_preprocess_grid_field_formatter(&$variables, $hook) {
// Extend the core field module theming functions.
template_preprocess_field($variables, $hook);
// Add colummns number.
$number_of_columns = $variables['element']['#columns'];
$variables['columns'] = $number_of_columns;
// Build row attributes.
$variables['row_attributes'] = new Attribute([
'class' => ['row'],
]);
// Build column layout number.
$variables['column_layout'] = floor(12 / $number_of_columns);
// Add Grid Field Formatter css if bootstrap is not enabled.
$bootstrap_enabled = \Drupal::config('grid_field_formatter.settings')->get('bootstrap');
if (!$bootstrap_enabled) {
$variables['#attached']['library'][] = 'grid_field_formatter/grid_field_formatter';
}
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function grid_field_formatter_theme_suggestions_grid_field_formatter(array $variables) {
$suggestions = array();
$element = $variables['element'];
$suggestions[] = 'grid_field_formatter__' . $element['#field_type'];
$suggestions[] = 'grid_field_formatter__' . $element['#field_name'];
$suggestions[] = 'grid_field_formatter__' . $element['#entity_type'] . '__' . $element['#bundle'];
$suggestions[] = 'grid_field_formatter__' . $element['#entity_type'] . '__' . $element['#field_name'];
$suggestions[] = 'grid_field_formatter__' . $element['#entity_type'] . '__' . $element['#field_name'] . '__' . $element['#bundle'];
return $suggestions;
}
/**
* Check if the given field is eligible to Grid Field Formatter options.
*/
function grid_field_formatter_is_eligible(FieldDefinitionInterface $field_definition) {
// Grid Field Formatter is useless on single value fields.
if ($field_definition->getFieldStorageDefinition()->getCardinality() == 1) {
return FALSE;
}
// Get the Grid Field Formatter enabled field types.
$field_types = \Drupal::config('grid_field_formatter.settings')->get('field_types');
// Add Grid Field Formatter if matching an enabled field type.
if ($field_types[$field_definition->getType()] != $field_definition->getType()) {
// Check preconfigured fields.
$field_type_class = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_definition->getType())['class'];
if (is_subclass_of($field_type_class, '\Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface')) {
foreach ($field_type_class::getPreconfiguredOptions() as $key => $option) {
if ($field_types['field_ui:' . $field_definition->getType() . ':' . $key]) {
return TRUE;
}
}
}
return FALSE;
}
return TRUE;
}
/**
* Implements hook_theme().
*/
function grid_field_formatter_theme() {
return [
'grid_field_formatter' => [
'render element' => 'element',
],
];
}