Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / plugins / content_types / form / entity_form_field.inc @ 6e3ce7c2

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
  $field_instances = field_info_instances();
42
  foreach ($entities as $entity_type => $entity) {
43
    foreach ($entity['bundles'] as $type => $bundle) {
44
      if (!isset($field_instances[$entity_type][$type])) {
45
        continue;
46
      }
47
      foreach ($field_instances[$entity_type][$type] as $field_name => $field) {
48
        if (!isset($types[$entity_type . ':' . $field_name])) {
49
          $types[$entity_type . ':' . $field_name] = array(
50
            'category' => t('Form'),
51
            'icon' => 'icon_field.png',
52
            'title' => t('Field form: @widget_label', array(
53
              '@widget_label' => t($field['label']),
54
            )),
55
            'description' => t('Field on the referenced entity.'),
56
          );
57
        }
58
        $content_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
59
      }
60
    }
61
  }
62

    
63
  if (module_exists('field_group')) {
64
    foreach ($entities as $entity_type => $entity) {
65
      foreach ($entity['bundles'] as $type => $bundle) {
66
        if ($group_info = field_group_info_groups($entity_type, $type, "form")) {
67
          foreach ($group_info as $group_name => $group) {
68
            if (!isset($types[$entity_type . ':' . $group_name])) {
69
              $types[$entity_type . ':' . $group_name] = array(
70
                'category' => t('Form'),
71
                'icon' => 'icon_field.png',
72
                'title' => t('Group form: @widget_label', array('@widget_label' => $group->label)),
73
                'description' => t('Field group on the referenced entity.'),
74
              );
75
            }
76
            $content_types[$entity_type . ':' . $group_name]['types'][$type] = $bundle['label'];
77
          }
78
        }
79
      }
80
    }
81
  }
82

    
83
  // Create the required context for each field related to the bundle types.
84
  foreach ($types as $key => $field_content_type) {
85
    list($entity_type, $field_name) = explode(':', $key, 2);
86
    $types[$key]['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
87
      'form' => array('form'),
88
      'type' => array_keys($content_types[$key]['types']),
89
    ));
90
    unset($content_types[$key]['types']);
91
  }
92
  return $types;
93
}
94

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

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

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

    
111
  // Check for field groups.
112
  if (empty($field) && module_exists('field_group')) {
113
    $groups = field_group_info_groups($entity_type, $entity->type, "form");
114
    $group = !empty($groups[$field_name]) ? $groups[$field_name] : NULL;
115
  }
116

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

    
122
  $block = new stdClass();
123
  if (isset($context->form)) {
124
    $block->content = array();
125
    if (!empty($field)) {
126
      $block->content[$field_name] = $context->form[$field_name];
127
      unset($context->form[$field_name]);
128
    }
129
    else {
130
      // Pre-render the form to populate field groups.
131
      if (isset($context->form['#pre_render'])) {
132
        foreach ($context->form['#pre_render'] as $function) {
133
          if (function_exists($function)) {
134
            $context->form = $function($context->form);
135
          }
136
        }
137
        unset($context->form['#pre_render']);
138
      }
139

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

    
148
  return $block;
149
}
150

    
151
/**
152
 * Returns the administrative title for a type.
153
 */
154
function ctools_entity_form_field_content_type_admin_title($subtype, $conf, $context) {
155
  // Return early because we don't have context to build this field from.
156
  if (!$context || !isset($context->identifier)) {
157
    watchdog('ctools_entity_form_field_content_type_admin_title', 'Context is missing for field: @name', array('@name' => $subtype), WATCHDOG_NOTICE);
158
    return t('Deleted/missing field @name', array('@name' => $subtype));
159
  }
160

    
161
  list($entity_type, $field_name) = explode(':', $subtype, 2);
162

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

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

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