5 * Handle rendering entity fields as panes.
9 'title' => t('Entity field'),
10 'defaults' => array('label' => '', 'formatter' => ''),
11 'content type' => 'ctools_entity_form_field_content_type_content_type',
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.
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];
29 * Return all field content types available.
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__);
39 $content_types = array();
40 $entities = entity_get_info();
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']),
52 'description' => t('Field on the referenced entity.'),
55 $content_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
60 if (module_exists('field_group')) {
61 foreach ($entities as $entity_type => $entity) {
62 foreach ($entity['bundles'] as $type => $bundle) {
63 if ($group_info = field_group_info_groups($entity_type, $type, "form")) {
64 foreach ($group_info as $group_name => $group) {
65 if (!isset($types[$entity_type . ':' . $group_name])) {
66 $types[$entity_type . ':' . $group_name] = array(
67 'category' => t('Form'),
68 'icon' => 'icon_field.png',
69 'title' => t('Group form: @widget_label', array('@widget_label' => $group->label)),
70 'description' => t('Field group on the referenced entity.'),
73 $content_types[$entity_type . ':' . $group_name]['types'][$type] = $bundle['label'];
80 // Create the required context for each field related to the bundle types.
81 foreach ($types as $key => $field_content_type) {
82 list($entity_type, $field_name) = explode(':', $key, 2);
83 $types[$key]['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
84 'form' => array('form'),
85 'type' => array_keys($content_types[$key]['types']),
87 unset($content_types[$key]['types']);
93 * Render the custom content type.
95 function ctools_entity_form_field_content_type_render($subtype, $conf, $panel_args, $context) {
96 if (empty($context) || empty($context->data)) {
100 // Get a shortcut to the entity.
101 $entity = $context->data;
102 list($entity_type, $field_name) = explode(':', $subtype, 2);
104 // Load the entity type's information for this field.
105 $ids = entity_extract_ids($entity_type, $entity);
106 $field = field_info_instance($entity_type, $field_name, $ids[2]);
108 // Check for field groups.
109 if (empty($field) && module_exists('field_group')) {
110 $groups = field_group_info_groups($entity_type, $entity->type, "form");
111 $group = !empty($groups[$field_name]) ? $groups[$field_name] : NULL;
114 // Do not render if the entity type does not have this field or group.
115 if (empty($field) && empty($group)) {
119 $block = new stdClass();
120 if (isset($context->form)) {
121 $block->content = array();
122 if (!empty($field)) {
123 $block->content[$field_name] = $context->form[$field_name];
124 unset($context->form[$field_name]);
127 // Pre-render the form to populate field groups.
128 if (isset($context->form['#pre_render'])) {
129 foreach ($context->form['#pre_render'] as $function) {
130 if (function_exists($function)) {
131 $context->form = $function($context->form);
134 unset($context->form['#pre_render']);
137 $block->content[$field_name] = $context->form[$field_name];
138 unset($context->form[$field_name]);
142 $block->content = t('Entity info.');
149 * Returns the administrative title for a type.
151 function ctools_entity_form_field_content_type_admin_title($subtype, $conf, $context) {
152 list($entity_type, $field_name) = explode(':', $subtype, 2);
154 if (!empty($context->restrictions)) {
155 $field = field_info_instance($entity_type, $field_name, $context->restrictions['type'][0]);
158 $field = array('label' => $subtype);
161 return t('"@s" @field form', array('@s' => $context->identifier, '@field' => $field['label']));
164 function ctools_entity_form_field_content_type_edit_form($form, &$form_state) {
165 // provide a blank form so we have a place to have context setting.