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' => '', 'formatter' => ''),
|
11
|
'content type' => 'ctools_entity_form_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_form_field_content_type_content_type($subtype) {
|
22
|
$types = ctools_entity_form_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_form_field_content_type_content_types() {
|
32
|
// This will hold all the individual field content types.
|
33
|
$types = &drupal_static(__FUNCTION__);
|
34
|
if (isset($types)) {
|
35
|
return $types;
|
36
|
}
|
37
|
|
38
|
$types = array();
|
39
|
$content_types = array();
|
40
|
$entities = entity_get_info();
|
41
|
|
42
|
foreach ($entities as $entity_type => $entity) {
|
43
|
foreach ($entity['bundles'] as $type => $bundle) {
|
44
|
foreach (field_info_instances($entity_type, $type) as $field_name => $field) {
|
45
|
if (!isset($types[$entity_type . ':' . $field_name])) {
|
46
|
$types[$entity_type . ':' . $field_name] = array(
|
47
|
'category' => t('Form'),
|
48
|
'icon' => 'icon_field.png',
|
49
|
'title' => t('Field form: @widget_label', array(
|
50
|
'@widget_label' => t($field['label']),
|
51
|
)),
|
52
|
'description' => t('Field on the referenced entity.'),
|
53
|
);
|
54
|
}
|
55
|
$content_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
|
56
|
}
|
57
|
}
|
58
|
}
|
59
|
|
60
|
// Create the required context for each field related to the bundle types.
|
61
|
foreach ($types as $key => $field_content_type) {
|
62
|
list($entity_type, $field_name) = explode(':', $key, 2);
|
63
|
$types[$key]['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
|
64
|
'form' => array('form'),
|
65
|
'type' => array_keys($content_types[$key]['types']),
|
66
|
));
|
67
|
unset($content_types[$key]['types']);
|
68
|
}
|
69
|
return $types;
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Render the custom content type.
|
74
|
*/
|
75
|
function ctools_entity_form_field_content_type_render($subtype, $conf, $panel_args, $context) {
|
76
|
if (empty($context) || empty($context->data)) {
|
77
|
return;
|
78
|
}
|
79
|
|
80
|
// Get a shortcut to the entity.
|
81
|
$entity = $context->data;
|
82
|
list($entity_type, $field_name) = explode(':', $subtype, 2);
|
83
|
|
84
|
// Load the entity type's information for this field.
|
85
|
$ids = entity_extract_ids($entity_type, $entity);
|
86
|
$field = field_info_instance($entity_type, $field_name, $ids[2]);
|
87
|
|
88
|
// Do not render if the entity type does not have this field.
|
89
|
if (empty($field)) {
|
90
|
return;
|
91
|
}
|
92
|
$block = new stdClass();
|
93
|
|
94
|
if (isset($context->form)) {
|
95
|
$block->content = array();
|
96
|
$block->content[$field_name] = $context->form[$field_name];
|
97
|
unset($context->form[$field_name]);
|
98
|
}
|
99
|
else {
|
100
|
$block->content = t('Entity info.');
|
101
|
}
|
102
|
|
103
|
return $block;
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Returns the administrative title for a type.
|
108
|
*/
|
109
|
function ctools_entity_form_field_content_type_admin_title($subtype, $conf, $context) {
|
110
|
list($entity_type, $field_name) = explode(':', $subtype, 2);
|
111
|
|
112
|
if (!empty($context->restrictions)) {
|
113
|
$field = field_info_instance($entity_type, $field_name, $context->restrictions['type'][0]);
|
114
|
}
|
115
|
else {
|
116
|
$field = array('label' => $subtype);
|
117
|
}
|
118
|
|
119
|
return t('"@s" @field form', array('@s' => $context->identifier, '@field' => $field['label']));
|
120
|
}
|
121
|
|
122
|
function ctools_entity_form_field_content_type_edit_form($form, &$form_state) {
|
123
|
// provide a blank form so we have a place to have context setting.
|
124
|
return $form;
|
125
|
}
|