Projet

Général

Profil

Paste
Télécharger (8,71 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / content_types / entity_context / entity_field.inc @ e4c061ad

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' => 'title', 'formatter' => '', 'delta_limit' => 0, 'delta_offset' => '0', 'delta_reversed' => FALSE),
11
  'content type' => 'ctools_entity_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_field_content_type_content_type($subtype) {
22
  $types = ctools_entity_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_field_content_type_content_types() {
32
  $types = &drupal_static(__FUNCTION__, array());
33
  if (!empty($types)) {
34
    return $types;
35
  }
36

    
37
  // This will hold all the individual field content types.
38
  $context_types = array();
39
  $entities = entity_get_info();
40

    
41
  $description = t('Field on the referenced entity.');
42
  $styles = t('Formatter Styles');
43
  $categories = array();
44
  foreach ($entities as $entity_type => $entity) {
45
    $category = t(ucfirst($entity_type));
46
    $categories[$entity_type] = $category;
47
    foreach ($entity['bundles'] as $type => $bundle) {
48
      foreach (field_info_instances($entity_type, $type) as $field_name => $field) {
49
        if (!isset($types[$entity_type . ':' . $field_name])) {
50
          $label = t($field['label']);
51
          $types[$entity_type . ':' . $field_name] = array(
52
            'category' => $category,
53
            'icon' => 'icon_field.png',
54
            'title' => t('Field: @widget_label (@field_name)', array(
55
              '@widget_label' => $label,
56
              '@field_name' => $field_name,
57
            )),
58
            'description' => $description,
59
            'edit form' => array(
60
              'ctools_entity_field_content_type_formatter_options' => array(
61
                'default' => TRUE,
62
                'title' => t('Formatter options for: @widget_label (@field_name)', array(
63
                  '@widget_label' => $label,
64
                  '@field_name' => $field_name,
65
                )),
66
              ),
67
              'ctools_entity_field_content_type_formatter_styles' => $styles,
68
            ),
69
          );
70
        }
71
        $context_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
72
      }
73
    }
74
  }
75

    
76
  // Create the required context for each field related to the bundle types.
77
  foreach ($types as $key => $field_content_type) {
78
    list($entity_type, $field_name) = explode(':', $key, 2);
79
    $types[$key]['required context'] = new ctools_context_required($categories[$entity_type], $entity_type, array(
80
      'type' => array_keys($context_types[$key]['types']),
81
    ));
82
    unset($context_types[$key]['types']);
83
  }
84

    
85
  return $types;
86
}
87

    
88
/**
89
* Render the custom content type.
90
*/
91
function ctools_entity_field_content_type_render($subtype, $conf, $panel_args, $context) {
92
  if (empty($context) || empty($context->data)) {
93
    return;
94
  }
95

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

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

    
104
  // Do not render if the entity type does not have this field.
105
  if (empty($field)) {
106
    return;
107
  }
108
  $language = field_language($entity_type, $entity, $field_name);
109

    
110
  if (empty($conf['label']) || $conf['label'] == 'title') {
111
    $label = 'hidden';
112
    $conf['label'] = 'title';
113
  }
114
  else {
115
    $label = $conf['label'];
116
  }
117

    
118
  $field_settings = array(
119
    'label' => $label,
120
    'type' => $conf['formatter'],
121
    // Pass all entity field panes settings to field display settings.
122
    'pane_settings' => $conf,
123
  );
124

    
125
  // Get the field output, and the title.
126
  if (!empty($conf['formatter_settings'])) {
127
    $field_settings['settings'] = $conf['formatter_settings'];
128
  }
129

    
130
  $all_values = field_get_items($entity_type, $entity, $field_name, $language);
131
  if (!is_array($all_values)) {
132
    // Do not render if the field is empty.
133
    return;
134
  }
135

    
136
  // Reverse values.
137
  if (isset($conf['delta_reversed']) && $conf['delta_reversed']) {
138
    $all_values = array_reverse($all_values, TRUE);
139
  }
140

    
141
  if (isset($conf['delta_limit'])) {
142
    $offset = intval($conf['delta_offset']);
143
    $limit = !empty($conf['delta_limit']) ? $conf['delta_limit'] : NULL;
144
    $all_values = array_slice($all_values, $offset, $limit, TRUE);
145
  }
146

    
147
  $clone = clone $entity;
148
  $clone->{$field_name}[$language] = $all_values;
149
  $field_output = field_view_field($entity_type, $clone, $field_name, $field_settings, $language);
150

    
151
  if (!empty($field_output) && !empty($conf['override_title'])) {
152
    $field_output['#title'] = filter_xss_admin($conf['override_title_text']);
153
  }
154

    
155
  // Build the content type block.
156
  $block = new stdClass();
157
  $block->module  = 'entity_field';
158
  if ($conf['label'] == 'title' && isset($field_output['#title'])) {
159
    $block->title = $field_output['#title'];
160
  }
161

    
162
  $block->content = $field_output;
163
  $block->delta   = $ids[0];
164

    
165
  return $block;
166
}
167

    
168
/**
169
* Returns an edit form for custom type settings.
170
*/
171
function ctools_entity_field_content_type_formatter_options($form, &$form_state) {
172
  if (empty($form_state['conf']['formatter_settings'])) {
173
    $form_state['conf']['formatter_settings'] = array();
174
  }
175
  $conf = $form_state['conf'];
176
  $subtype = $form_state['subtype_name'];
177
  list($entity_type, $field_name) = explode(':', $subtype, 2);
178

    
179
  $field = field_info_field($field_name);
180
  module_load_include('inc', 'field_ui', 'field_ui.admin');
181
  $formatter_options = field_ui_formatter_options($field['type']);
182

    
183
  $field_label_options = array(
184
    'title' => t('Pane title'),
185
    'above' => t('Above'),
186
    'inline' => t('Inline'),
187
    'hidden' => t('Hidden'),
188
  );
189

    
190
  $form['label'] = array(
191
    '#type' => 'select',
192
    '#title' => t('Label'),
193
    '#options' => $field_label_options,
194
    '#default_value' => $conf['label'],
195
  );
196

    
197
  $form['formatter'] = array(
198
    '#type' => 'select',
199
    '#title' => t('Select a formatter'),
200
    '#options' => $formatter_options,
201
    '#default_value' => $conf['formatter'],
202
  );
203

    
204
  return $form;
205
}
206

    
207
function ctools_entity_field_content_type_formatter_options_submit($form, &$form_state) {
208
  $form_state['conf']['formatter'] = $form_state['values']['formatter'];
209
  $form_state['conf']['label'] = $form_state['values']['label'];
210
}
211

    
212
function ctools_entity_field_content_type_formatter_styles($form, &$form_state) {
213
  if (!$form_state['conf']['formatter_settings']) {
214
    $form_state['conf']['formatter_settings'] = array();
215
  }
216
  $conf = $form_state['conf'];
217
  $subtype = $form_state['subtype_name'];
218
  list($entity_type, $field_name) = explode(':', $subtype, 2);
219
  $field = field_info_field($field_name);
220

    
221
  ctools_form_include($form_state, 'field_ui.admin', 'field_ui', '');
222
  ctools_form_include($form_state, 'fields');
223

    
224
  $form['ctools_field_list'] = array(
225
    '#type' => 'value',
226
    '#value' => array(),
227
  );
228

    
229
  ctools_fields_get_field_formatter_settings_form($field, $conf['formatter'], $form, $form_state);
230
  return $form;
231
}
232

    
233
function ctools_entity_field_content_type_formatter_styles_submit($form, &$form_state) {
234
  $fields = $form_state['values']['ctools_field_list'];
235
  $formatter_info = ctools_fields_get_field_formatter_info($fields);
236
  foreach ($formatter_info as $info) {
237
    if (!empty($info['settings'])) {
238
      foreach ($info['settings'] as $field_name => $value) {
239
        if (isset($form_state['values'][$field_name])) {
240
          $form_state['conf']['formatter_settings'][$field_name] = $form_state['values'][$field_name];
241
        }
242
      }
243
    }
244
  }
245

    
246
  if (isset($form_state['values']['delta_limit'])) {
247
    $form_state['conf']['delta_limit'] = $form_state['values']['delta_limit'];
248
    $form_state['conf']['delta_offset'] = $form_state['values']['delta_offset'];
249
    $form_state['conf']['delta_reversed'] = $form_state['values']['delta_reversed'];
250
  }
251
}
252

    
253
/**
254
 * Returns the administrative title for a type.
255
 */
256
function ctools_entity_field_content_type_admin_title($subtype, $conf, $context) {
257
  list($bundle, $field_name) = explode(':', $subtype);
258
  ctools_include('fields');
259
  if (is_object($context) && isset($context->identifier)) {
260
    $identifier = $context->identifier;
261
  }
262
  else {
263
    $type = 'ctools_entity_field_content_type_admin_title';
264
    $message = t('Context is missing for field: @name', array('@name' => $subtype));
265
    $variables = array($subtype, $conf, $context);
266
    watchdog($type, $message, $variables, $severity = WATCHDOG_NOTICE);
267
    $identifier = t('Unknown');
268
  }
269

    
270
  return t('"@s" @field', array('@s' => $identifier, '@field' => ctools_field_label($field_name)));
271
}