forked from summitmedia/pmmi_fields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmmi_fields.module
220 lines (201 loc) · 6.78 KB
/
pmmi_fields.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* @file
* Contains pmmi_fields.module.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function pmmi_fields_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the pmmi_fields module.
case 'help.page.pmmi_fields':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('PMMI Fields') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_entity_base_field_info().
*/
function pmmi_fields_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() === 'menu_link_content') {
$fields['submenu_background'] = BaseFieldDefinition::create('image')
->setLabel(t('Submenu background'))
->setDescription(t('Submenu background for megaMenu.'))
->setSettings([
'file_directory' => 'menu_image',
'alt_field_required' => FALSE,
'file_extensions' => 'png jpg jpeg',
])
->setDisplayOptions('form', [
'type' => 'hidden',
])
->setDisplayOptions('form', [
'label' => 'hidden',
'type' => 'image_image',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
return $fields;
}
}
/**
* Implements hook_theme().
*/
function pmmi_fields_theme() {
return [
'pmmi_fields_entity_table' => [
'render element' => 'form',
'function' => 'theme_pmmi_fields_entity_table',
],
'double_field_mail_link' => [
'render element' => 'element',
'variables' => ['item' => NULL],
],
];
}
/**
* Themes the table showing existing entity references in the widget.
*
* @NOTE This is 100% copy paste from theme_inline_entity_form_entity_table(),
* except few lines.
* You can find custom code by comments 'Start custom code' and 'End custom code'.
* Unfortunately there is no way to reuse existing theme_inline_entity_form_entity_table() function.
* The only way to do it is mark view_mode as a field type and added it as a field to all entities.
* This will be a big overhead. For details see this line in this function.
* @code
* elseif ($field['type'] == 'field' && $entity->hasField($field_name)) {
* @endcode
* That's why we have only one way to add view mode to this table.
* Just copy paste and modify.
*
* Important: Fill free to update this code with the latest one from theme_inline_entity_form_entity_table(),
* after that put back custom code after update.
*
* @param array $variables
* Contains the form element data from $element['entities'].
*
* @return array
* Render array.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
function theme_pmmi_fields_entity_table($variables) {
/** @var \Drupal\Core\Render\Renderer $renderer */
$renderer = \Drupal::service('renderer');
$form = $variables['form'];
$entity_type = $form['#entity_type'];
$fields = $form['#table_fields'];
$has_tabledrag = \Drupal::entityTypeManager()->getHandler($entity_type, 'inline_form')->isTableDragEnabled($form);
// Sort the fields by weight.
uasort($fields, '\Drupal\Component\Utility\SortArray::sortByWeightElement');
$header = [];
if ($has_tabledrag) {
$header[] = ['data' => '', 'class' => ['ief-tabledrag-header']];
$header[] = ['data' => t('Sort order'), 'class' => ['ief-sort-order-header']];
}
// Add header columns for each field.
$first = TRUE;
foreach ($fields as $field_name => $field) {
$column = ['data' => $field['label'], 'class' => ['inline-entity-form-' . $entity_type . '-' . $field_name]];
// The first column gets a special class.
if ($first) {
$column['class'][] = 'ief-first-column-header';
$first = FALSE;
}
$header[] = $column;
}
$header[] = t('Operations');
// Build an array of entity rows for the table.
$rows = [];
foreach (Element::children($form) as $key) {
/** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
$entity = $form[$key]['#entity'];
$row_classes = ['ief-row-entity'];
$cells = [];
if ($has_tabledrag) {
$cells[] = ['data' => '', 'class' => ['ief-tabledrag-handle']];
$cells[] = $renderer->render($form[$key]['delta']);
$row_classes[] = 'draggable';
}
// Add a special class to rows that have a form underneath, to allow
// for additional styling.
if (!empty($form[$key]['form'])) {
$row_classes[] = 'ief-row-entity-form';
}
foreach ($fields as $field_name => $field) {
if ($field['type'] == 'label') {
$data = $variables['form'][$key]['#label'];
}
elseif ($field['type'] == 'field' && $entity->hasField($field_name)) {
$display_options = ['label' => 'hidden'];
if (isset($field['display_options'])) {
$display_options += $field['display_options'];
}
$data = $entity->get($field_name)->view($display_options);
}
elseif ($field['type'] == 'callback') {
$arguments = [
'entity' => $entity,
'variables' => $variables,
];
if (isset($field['callback_arguments'])) {
$arguments = array_merge($arguments, $field['callback_arguments']);
}
$data = call_user_func_array($field['callback'], $arguments);
}
// Start custom.
elseif ($field['type'] == 'view_mode') {
// Add the column belonging to the "View mode" column.
$data = $renderer->render($form[$key]['view_mode']);
}
// End custom code.
else {
$data = t('N/A');
}
$cells[] = ['data' => $data, 'class' => ['inline-entity-form-' . $entity_type . '-' . $field_name]];
}
// Add the buttons belonging to the "Operations" column.
$cells[] = $renderer->render($form[$key]['actions']);
// Create the row.
$rows[] = ['data' => $cells, 'class' => $row_classes];
// If the current entity array specifies a form, output it in the next row.
if (!empty($form[$key]['form'])) {
$row = [
['data' => $renderer->render($form[$key]['form']), 'colspan' => count($fields) + 1],
];
$rows[] = ['data' => $row, 'class' => ['ief-row-form'], 'no_striping' => TRUE];
}
}
if (!empty($rows)) {
$tabledrag = [];
if ($has_tabledrag) {
$tabledrag = [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'ief-entity-delta',
],
];
}
$table = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'id' => 'ief-entity-table-' . $form['#id'],
'class' => ['ief-entity-table'],
],
'#tabledrag' => $tabledrag,
];
return $renderer->render($table);
}
}