-
Notifications
You must be signed in to change notification settings - Fork 4
/
er_viewmode.module
186 lines (168 loc) · 6.59 KB
/
er_viewmode.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
<?php
/**
* Implements hook_ctools_plugin_directory().
*/
function er_viewmode_ctools_plugin_directory($module, $plugin) {
if ($module == 'entityreference') {
return 'plugins/' . $plugin;
}
}
/**
* Implements hook_field_widget_form_alter().
*/
function er_viewmode_field_widget_form_alter(&$element, &$form_state, $context) {
$field = $context['field'];
$instance = $context['instance'];
if (er_viewmode_supported_field_widget($field)) {
$item = isset($context['items'][$context['delta']]) ? $context['items'][$context['delta']] : NULL;
$target_type = $field['settings']['target_type'];
$view_modes = er_viewmode_get_view_modes($field, $instance);
$instance = $context['instance'];
if (in_array($instance['widget']['type'], array(
'entityreference_autocomplete',
'options_select',
'options_buttons'
))) {
$element['view_mode'] = array(
'#type' => 'select',
'#title' => 'View Mode',
'#title_display' => 'invisible',
'#options' => $view_modes,
'#default_value' => isset($item) ? $item['view_mode'] : 'full',
'#weight' => 10,
'#id' => 'view-mode-' . $context['delta'],
);
}
}
}
/**
* Fetches view_modes based of the field and instance.
*
* @param $field array - the field definition
* @param $instance array - the field instance definition
* @param $ignore_whitelist boolean - if TRUE it will return the full list of available view modes.
* @return array - list of view modes
*/
function er_viewmode_get_view_modes($field, $instance, $ignore_whitelist = FALSE) {
$target_type = $field['settings']['target_type'];
$entity_info = entity_get_info($target_type);
$view_modes = array('full' => 'Default');
foreach ($entity_info['view modes'] as $view_key => $view_mode) {
if ($ignore_whitelist || !empty($field['settings']['handler_settings']['behaviors']['er_viewmode_behavior']['enabled_viewmodes'][$view_key])) {
$view_modes[$view_key] = $view_mode['label'];
}
}
if (!$ignore_whitelist) {
drupal_alter('er_viewmode_get_view_modes', $view_modes, $field, $instance);
}
return $view_modes;
}
/**
* Check if a field is supported by inline entity form view mode
*
* @param $field array - definition of a field
* @return bool - true if the given field is a supported field.
*/
function er_viewmode_supported_field_widget($field) {
return $field['type'] == 'entityreference' && isset($field['settings']['handler_settings']['behaviors']['er_viewmode_behavior']) && $field['settings']['handler_settings']['behaviors']['er_viewmode_behavior']['status'] == 1;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function er_viewmode_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$element = entityreference_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
if (er_viewmode_supported_field_widget($field)) {
unset($element['view_mode']);
$element['view_mode'] = array(
'#type' => 'markup',
'#markup' => '<p>' . t('View modes are selected per referenced entity') . '</p>',
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function er_viewmode_field_formatter_settings_summary($field, $instance, $view_mode) {
return entityreference_field_formatter_settings_summary($field, $instance, $view_mode);
}
/**
* Overwrite entityreference modules field_formatter implementation.
*
* @todo research less invasive way of archiving this.
*
* Implements hook_field_formatter_info_alter().
*/
function er_viewmode_field_formatter_info_alter(&$info) {
$info['entityreference_entity_view']['module'] = 'er_viewmode';
}
/**
* Implements hook_field_formatter_prepare_view().
*/
function er_viewmode_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
return entityreference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
}
/**
* Implements hook_field_formatter_view().
*/
function er_viewmode_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
if (!er_viewmode_supported_field_widget($field)) {
return entityreference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
}
$result = array();
$settings = $display['settings'];
// Rebuild the items list to contain only those with access.
foreach ($items as $key => $item) {
if (empty($item['access'])) {
unset($items[$key]);
}
}
switch ($display['type']) {
case 'entityreference_entity_view':
foreach ($items as $delta => $item) {
// Protect ourselves from recursive rendering.
static $depth = 0;
$depth++;
if ($depth > 20) {
throw new EntityReferenceRecursiveRenderingException(t('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.',
array(
'@entity_type' => $entity_type,
'@entity_id' => $item['target_id']
)));
}
$entity = clone $item['entity'];
unset($entity->content);
$result[$delta] = entity_view($field['settings']['target_type'],
array($item['target_id'] => $entity), $item['view_mode'],
$langcode, FALSE);
if (empty($settings['links']) && isset($result[$delta][$field['settings']['target_type']][$item['target_id']]['links'])) {
$result[$delta][$field['settings']['target_type']][$item['target_id']]['links']['#access'] = FALSE;
}
$depth = 0;
}
break;
}
return $result;
}
/**
* Implements hook_uuid_node_features_export_render_alter().
*/
function er_viewmode_uuid_node_features_export_render_alter(&$export, $node, $module) {
$fields = uuid_features_get_field_items_iterator($export, 'node', 'entityreference');
foreach ($fields as $field_name => &$field_languages) {
foreach ($field_languages as $language => &$field_values) {
foreach ($field_values as $i => &$field_value) {
$field_info = field_info_field($field_name);
// If er_viewmode behavior is being used
// and the uuid_features module wrongly turned the view mode into a UUID
// change the view mode back to its original value.
if (
!empty($field_info['settings']['handler_settings']['behaviors']['er_viewmode_behavior'])
&& (!empty($field_value['view_mode']) && uuid_is_valid($field_value['view_mode']))
) {
$field_value['view_mode'] = $node->{$field_name}[$language][$i]['view_mode'];
}
}
}
}
}