This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfallback_formatter.module
95 lines (84 loc) · 2.35 KB
/
fallback_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
<?php
/**
* @file
* Hook implementations for the Fallback formatter module.
*/
use Drupal\Core\Render\Element;
/**
* Implements hook_theme().
*/
function fallback_formatter_theme() {
return [
'fallback_formatter_settings_order' => [
'render element' => 'element',
'function' => 'theme_fallback_formatter_settings_order',
],
];
}
/**
* Implements hook_field_formatter_info_alter().
*/
function fallback_formatter_field_formatter_info_alter(&$info) {
// Only enable the fallback formatter for field_types that already have one
// or more formatters.
$field_types = [];
foreach ($info as $formatter) {
$field_types = array_merge($field_types, $formatter['field_types']);
}
$counts = array_count_values($field_types);
foreach ($counts as $field_type => $count) {
if ($count > 1) {
$info['fallback']['field_types'][] = $field_type;
}
}
}
/**
* Themes the formatter settings form.
*
* @param array $variables
* Theme variables.
*
* @return string
* Rendered HTML of the settings form part.
*/
function theme_fallback_formatter_settings_order($variables) {
$element = $variables['element'];
// Fallback formatter order (tabledrag).
$rows = [];
foreach (Element::children($element, TRUE) as $name) {
$element[$name]['weight']['#attributes']['class'][] = 'fallback-formatter-order-weight';
$rows[] = [
'data' => [
\Drupal::service('renderer')->render($element[$name]['label']),
\Drupal::service('renderer')->render($element[$name]['weight']),
],
'class' => ['draggable'],
];
}
$table = &$element[];
$table = [
'#theme' => 'table',
'#rows' => $rows,
'#attributes' => ['id' => 'fallback-formatter-order'],
];
drupal_attach_tabledrag($table, [
'table_id' => 'fallback-formatter-order',
'action' => 'order',
'relationship' => 'sibling',
'group' => 'fallback-formatter-order-weight',
]);
return \Drupal::service('renderer')->render($element);
}
/**
* Implements hook_entity_embed_display_plugins_alter().
*
* Do not allow the fallback formatter to be used as an entity embed display
* plugin.
*/
function fallback_formatter_entity_embed_display_plugins_alter(array &$plugins) {
foreach ($plugins as $id => $plugin) {
if (!empty($plugin['field_type']) && substr($id, -9) === ':fallback') {
unset($plugins[$id]);
}
}
}