Projet

Général

Profil

Paste
Télécharger (5,28 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / content_types / form / entity_form_field.inc @ 560c3060

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
  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.'),
71
              );
72
            }
73
            $content_types[$entity_type . ':' . $group_name]['types'][$type] = $bundle['label'];
74
          }
75
      }
76
      }
77
    }
78
  }
79

    
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']),
86
    ));
87
    unset($content_types[$key]['types']);
88
  }
89
  return $types;
90
}
91

    
92
/**
93
* Render the custom content type.
94
*/
95
function ctools_entity_form_field_content_type_render($subtype, $conf, $panel_args, $context) {
96
  if (empty($context) || empty($context->data)) {
97
    return;
98
  }
99

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

    
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]);
107

    
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;
112
  }
113

    
114
  // Do not render if the entity type does not have this field or group.
115
  if (empty($field) && empty($group)) {
116
    return;
117
  }
118

    
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]);
125
    }
126
    else {
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);
132
          }
133
        }
134
        unset($context->form['#pre_render']);
135
      }
136

    
137
      $block->content[$field_name] = $context->form[$field_name];
138
      unset($context->form[$field_name]);
139
    }
140
  }
141
  else {
142
    $block->content = t('Entity info.');
143
  }
144

    
145
  return $block;
146
}
147

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

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

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

    
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.
166
  return $form;
167
}