1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Handle rendering entity fields as panes.
|
6
|
*/
|
7
|
|
8
|
$plugin = array(
|
9
|
'title' => t('Entity field'),
|
10
|
'defaults' => array('label' => 'title', 'formatter' => '', 'delta_limit' => 0, 'delta_offset' => '0', 'delta_reversed' => FALSE),
|
11
|
'content type' => 'ctools_entity_field_content_type_content_type',
|
12
|
);
|
13
|
|
14
|
/**
|
15
|
* Just one subtype.
|
16
|
*
|
17
|
* Ordinarily this function is meant to get just one subtype. However, we are
|
18
|
* using it to deal with the fact that we have changed the subtype names. This
|
19
|
* lets us translate the name properly.
|
20
|
*/
|
21
|
function ctools_entity_field_content_type_content_type($subtype) {
|
22
|
$types = ctools_entity_field_content_type_content_types();
|
23
|
if (isset($types[$subtype])) {
|
24
|
return $types[$subtype];
|
25
|
}
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* Return all field content types available.
|
30
|
*/
|
31
|
function ctools_entity_field_content_type_content_types() {
|
32
|
$types = &drupal_static(__FUNCTION__, array());
|
33
|
if (!empty($types)) {
|
34
|
return $types;
|
35
|
}
|
36
|
|
37
|
$cache_key = 'ctools_entity_field_content_type_content_types';
|
38
|
if ($cache = cache_get($cache_key)) {
|
39
|
$types = $cache->data;
|
40
|
if (!empty($types)) {
|
41
|
return $types;
|
42
|
}
|
43
|
}
|
44
|
|
45
|
// This will hold all the individual field content types.
|
46
|
$context_types = array();
|
47
|
$entities = entity_get_info();
|
48
|
|
49
|
$description = t('Field on the referenced entity.');
|
50
|
$styles = t('Formatter Styles');
|
51
|
$categories = array();
|
52
|
foreach ($entities as $entity_type => $entity) {
|
53
|
$category = t(ucfirst($entity_type));
|
54
|
$categories[$entity_type] = $category;
|
55
|
foreach ($entity['bundles'] as $type => $bundle) {
|
56
|
foreach (field_info_instances($entity_type, $type) as $field_name => $field) {
|
57
|
if (!isset($types[$entity_type . ':' . $field_name])) {
|
58
|
$label = t($field['label']);
|
59
|
$types[$entity_type . ':' . $field_name] = array(
|
60
|
'category' => $category,
|
61
|
'icon' => 'icon_field.png',
|
62
|
'title' => t('Field: @widget_label (@field_name)', array(
|
63
|
'@widget_label' => $label,
|
64
|
'@field_name' => $field_name,
|
65
|
)),
|
66
|
'description' => $description,
|
67
|
'edit form' => array(
|
68
|
'ctools_entity_field_content_type_formatter_options' => array(
|
69
|
'default' => TRUE,
|
70
|
'title' => t('Formatter options for: @widget_label (@field_name)', array(
|
71
|
'@widget_label' => $label,
|
72
|
'@field_name' => $field_name,
|
73
|
)),
|
74
|
),
|
75
|
'ctools_entity_field_content_type_formatter_styles' => $styles,
|
76
|
),
|
77
|
);
|
78
|
}
|
79
|
$context_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
|
80
|
}
|
81
|
}
|
82
|
}
|
83
|
|
84
|
// Create the required context for each field related to the bundle types.
|
85
|
foreach ($types as $key => $field_content_type) {
|
86
|
list($entity_type, $field_name) = explode(':', $key, 2);
|
87
|
$types[$key]['required context'] = new ctools_context_required($categories[$entity_type], $entity_type, array(
|
88
|
'type' => array_keys($context_types[$key]['types']),
|
89
|
));
|
90
|
unset($context_types[$key]['types']);
|
91
|
}
|
92
|
|
93
|
cache_set($cache_key, $types);
|
94
|
|
95
|
return $types;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Render the custom content type.
|
100
|
*/
|
101
|
function ctools_entity_field_content_type_render($subtype, $conf, $panel_args, $context) {
|
102
|
if (empty($context) || empty($context->data)) {
|
103
|
return;
|
104
|
}
|
105
|
|
106
|
// Get a shortcut to the entity.
|
107
|
$entity = $context->data;
|
108
|
list($entity_type, $field_name) = explode(':', $subtype, 2);
|
109
|
|
110
|
// Load the entity type's information for this field.
|
111
|
$ids = entity_extract_ids($entity_type, $entity);
|
112
|
$field = field_info_instance($entity_type, $field_name, $ids[2]);
|
113
|
|
114
|
// Do not render if the entity type does not have this field.
|
115
|
if (empty($field)) {
|
116
|
return;
|
117
|
}
|
118
|
$language = field_language($entity_type, $entity, $field_name);
|
119
|
|
120
|
if (empty($conf['label']) || $conf['label'] == 'title') {
|
121
|
$label = 'hidden';
|
122
|
$conf['label'] = 'title';
|
123
|
}
|
124
|
else {
|
125
|
$label = $conf['label'];
|
126
|
}
|
127
|
|
128
|
$field_settings = array(
|
129
|
'label' => $label,
|
130
|
'type' => $conf['formatter'],
|
131
|
// Pass all entity field panes settings to field display settings.
|
132
|
'pane_settings' => $conf,
|
133
|
);
|
134
|
|
135
|
// Get the field output, and the title.
|
136
|
if (!empty($conf['formatter_settings'])) {
|
137
|
$field_settings['settings'] = $conf['formatter_settings'];
|
138
|
}
|
139
|
|
140
|
$all_values = field_get_items($entity_type, $entity, $field_name, $language);
|
141
|
if (!is_array($all_values)) {
|
142
|
// Do not render if the field is empty.
|
143
|
return;
|
144
|
}
|
145
|
|
146
|
// Reverse values.
|
147
|
if (isset($conf['delta_reversed']) && $conf['delta_reversed']) {
|
148
|
$all_values = array_reverse($all_values, TRUE);
|
149
|
}
|
150
|
|
151
|
if (isset($conf['delta_limit'])) {
|
152
|
$offset = intval($conf['delta_offset']);
|
153
|
$limit = !empty($conf['delta_limit']) ? $conf['delta_limit'] : NULL;
|
154
|
$all_values = array_slice($all_values, $offset, $limit, TRUE);
|
155
|
}
|
156
|
|
157
|
$clone = clone $entity;
|
158
|
$clone->{$field_name}[$language] = $all_values;
|
159
|
$field_output = field_view_field($entity_type, $clone, $field_name, $field_settings, $language);
|
160
|
|
161
|
if (!empty($field_output) && !empty($conf['override_title'])) {
|
162
|
$field_output['#title'] = filter_xss_admin($conf['override_title_text']);
|
163
|
}
|
164
|
|
165
|
// Build the content type block.
|
166
|
$block = new stdClass();
|
167
|
$block->module = 'entity_field';
|
168
|
if ($conf['label'] == 'title' && isset($field_output['#title'])) {
|
169
|
$block->title = $field_output['#title'];
|
170
|
}
|
171
|
|
172
|
$block->content = $field_output;
|
173
|
$block->delta = $ids[0];
|
174
|
|
175
|
return $block;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Returns an edit form for custom type settings.
|
180
|
*/
|
181
|
function ctools_entity_field_content_type_formatter_options($form, &$form_state) {
|
182
|
if (empty($form_state['conf']['formatter_settings'])) {
|
183
|
$form_state['conf']['formatter_settings'] = array();
|
184
|
}
|
185
|
$conf = $form_state['conf'];
|
186
|
$subtype = $form_state['subtype_name'];
|
187
|
list($entity_type, $field_name) = explode(':', $subtype, 2);
|
188
|
|
189
|
$field = field_info_field($field_name);
|
190
|
module_load_include('inc', 'field_ui', 'field_ui.admin');
|
191
|
$formatter_options = field_ui_formatter_options($field['type']);
|
192
|
|
193
|
$field_label_options = array(
|
194
|
'title' => t('Pane title'),
|
195
|
'above' => t('Above'),
|
196
|
'inline' => t('Inline'),
|
197
|
'hidden' => t('Hidden'),
|
198
|
);
|
199
|
|
200
|
$form['label'] = array(
|
201
|
'#type' => 'select',
|
202
|
'#title' => t('Label'),
|
203
|
'#options' => $field_label_options,
|
204
|
'#default_value' => $conf['label'],
|
205
|
);
|
206
|
|
207
|
$form['formatter'] = array(
|
208
|
'#type' => 'select',
|
209
|
'#title' => t('Select a formatter'),
|
210
|
'#options' => $formatter_options,
|
211
|
'#default_value' => $conf['formatter'],
|
212
|
);
|
213
|
|
214
|
return $form;
|
215
|
}
|
216
|
|
217
|
function ctools_entity_field_content_type_formatter_options_submit($form, &$form_state) {
|
218
|
$form_state['conf']['formatter'] = $form_state['values']['formatter'];
|
219
|
$form_state['conf']['label'] = $form_state['values']['label'];
|
220
|
}
|
221
|
|
222
|
function ctools_entity_field_content_type_formatter_styles($form, &$form_state) {
|
223
|
if (!$form_state['conf']['formatter_settings']) {
|
224
|
$form_state['conf']['formatter_settings'] = array();
|
225
|
}
|
226
|
$conf = $form_state['conf'];
|
227
|
$subtype = $form_state['subtype_name'];
|
228
|
list($entity_type, $field_name) = explode(':', $subtype, 2);
|
229
|
$field = field_info_field($field_name);
|
230
|
|
231
|
ctools_form_include($form_state, 'field_ui.admin', 'field_ui', '');
|
232
|
ctools_form_include($form_state, 'fields');
|
233
|
|
234
|
$form['ctools_field_list'] = array(
|
235
|
'#type' => 'value',
|
236
|
'#value' => array(),
|
237
|
);
|
238
|
|
239
|
ctools_fields_get_field_formatter_settings_form($field, $conf['formatter'], $form, $form_state);
|
240
|
return $form;
|
241
|
}
|
242
|
|
243
|
function ctools_entity_field_content_type_formatter_styles_submit($form, &$form_state) {
|
244
|
$fields = $form_state['values']['ctools_field_list'];
|
245
|
$formatter_info = ctools_fields_get_field_formatter_info($fields);
|
246
|
foreach ($formatter_info as $info) {
|
247
|
if (!empty($info['settings'])) {
|
248
|
foreach ($info['settings'] as $field_name => $value) {
|
249
|
if (isset($form_state['values'][$field_name])) {
|
250
|
$form_state['conf']['formatter_settings'][$field_name] = $form_state['values'][$field_name];
|
251
|
}
|
252
|
}
|
253
|
}
|
254
|
}
|
255
|
|
256
|
if (isset($form_state['values']['delta_limit'])) {
|
257
|
$form_state['conf']['delta_limit'] = $form_state['values']['delta_limit'];
|
258
|
$form_state['conf']['delta_offset'] = $form_state['values']['delta_offset'];
|
259
|
$form_state['conf']['delta_reversed'] = $form_state['values']['delta_reversed'];
|
260
|
}
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* Returns the administrative title for a type.
|
265
|
*/
|
266
|
function ctools_entity_field_content_type_admin_title($subtype, $conf, $context) {
|
267
|
list($bundle, $field_name) = explode(':', $subtype);
|
268
|
ctools_include('fields');
|
269
|
if (is_object($context) && isset($context->identifier)) {
|
270
|
$identifier = $context->identifier;
|
271
|
}
|
272
|
else {
|
273
|
$type = 'ctools_entity_field_content_type_admin_title';
|
274
|
$message = t('Context is missing for field: @name', array('@name' => $subtype));
|
275
|
$variables = array($subtype, $conf, $context);
|
276
|
watchdog($type, $message, $variables, $severity = WATCHDOG_NOTICE);
|
277
|
$identifier = t('Unknown');
|
278
|
}
|
279
|
|
280
|
return t('"@s" @field', array('@s' => $identifier, '@field' => ctools_field_label($field_name)));
|
281
|
}
|