summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.26/sites/all/modules/ctools/plugins/content_types/form/entity_form_field.inc
blob: a030f693ee6b2e0c36a57bc74883cb948a8a7a60 (plain)
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
<?php

/**
 * @file
 * Handle rendering entity fields as panes.
 */

$plugin = array(
  'title' => t('Entity field'),
  'defaults' => array('label' => '', 'formatter' => ''),
  'content type' => 'ctools_entity_form_field_content_type_content_type',
);

/**
 * Just one subtype.
 *
 * Ordinarily this function is meant to get just one subtype. However, we are
 * using it to deal with the fact that we have changed the subtype names. This
 * lets us translate the name properly.
 */
function ctools_entity_form_field_content_type_content_type($subtype) {
  $types = ctools_entity_form_field_content_type_content_types();
  if (isset($types[$subtype])) {
    return $types[$subtype];
  }
}

/**
 * Return all field content types available.
 */
function ctools_entity_form_field_content_type_content_types() {
  // This will hold all the individual field content types.
  $types = &drupal_static(__FUNCTION__);
  if (isset($types)) {
    return $types;
  }

  $types = array();
  $content_types = array();
  $entities = entity_get_info();

  foreach ($entities as $entity_type => $entity) {
    foreach ($entity['bundles'] as $type => $bundle) {
      foreach (field_info_instances($entity_type, $type) as $field_name => $field) {
        if (!isset($types[$entity_type . ':' . $field_name])) {
          $types[$entity_type . ':' . $field_name] = array(
            'category' => t('Form'),
            'icon' => 'icon_field.png',
            'title' => t('Field form: @widget_label', array(
              '@widget_label' => t($field['label']),
            )),
            'description' => t('Field on the referenced entity.'),
          );
        }
        $content_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
      }
    }
  }

  // Create the required context for each field related to the bundle types.
  foreach ($types as $key => $field_content_type) {
    list($entity_type, $field_name) = explode(':', $key, 2);
    $types[$key]['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
      'form' => array('form'),
      'type' => array_keys($content_types[$key]['types']),
    ));
    unset($content_types[$key]['types']);
  }
  return $types;
}

/**
* Render the custom content type.
*/
function ctools_entity_form_field_content_type_render($subtype, $conf, $panel_args, $context) {
  if (empty($context) || empty($context->data)) {
    return;
  }

  // Get a shortcut to the entity.
  $entity = $context->data;
  list($entity_type, $field_name) = explode(':', $subtype, 2);

  // Load the entity type's information for this field.
  $ids = entity_extract_ids($entity_type, $entity);
  $field = field_info_instance($entity_type, $field_name, $ids[2]);

  // Do not render if the entity type does not have this field.
  if (empty($field)) {
    return;
  }
  $block = new stdClass();

  if (isset($context->form)) {
    $block->content = array();
    $block->content[$field_name] = $context->form[$field_name];
    unset($context->form[$field_name]);
  }
  else {
    $block->content = t('Entity info.');
  }

  return $block;
}

/**
* Returns the administrative title for a type.
*/
function ctools_entity_form_field_content_type_admin_title($subtype, $conf, $context) {
  list($entity_type, $field_name) = explode(':', $subtype, 2);

  if (!empty($context->restrictions)) {
    $field = field_info_instance($entity_type, $field_name, $context->restrictions['type'][0]);
  }
  else {
    $field = array('label' => $subtype);
  }

  return t('"@s" @field form', array('@s' => $context->identifier, '@field' => $field['label']));
}

function ctools_entity_form_field_content_type_edit_form($form, &$form_state) {
  // provide a blank form so we have a place to have context setting.
  return $form;
}