Projet

Général

Profil

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

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

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
  $cache_key = 'ctools_entity_field_content_type_content_types';
38
  if ($cache = cache_get($cache_key)) {
39
    $types = $cache->data;
40
    if (!empty($types)) {
41
      return $types;
42
    }
43
  }
44

    
45
  // This will hold all the individual field content types.
46
  $context_types = array();
47
  $entities = entity_get_info();
48

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

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

    
93
  cache_set($cache_key, $types);
94

    
95
  return $types;
96
}
97

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

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

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

    
114
  // Do not render if the entity type does not have this field.
115
  if (empty($field)) {
116
    return;
117
  }
118
  $language = field_language($entity_type, $entity, $field_name);
119

    
120
  if (empty($conf['label']) || $conf['label'] == 'title') {
121
    $label = 'hidden';
122
    $conf['label'] = 'title';
123
  }
124
  else {
125
    $label = $conf['label'];
126
  }
127

    
128
  $field_settings = array(
129
    'label' => $label,
130
    'type' => $conf['formatter'],
131
    // Pass all entity field panes settings to field display settings.
132
    'pane_settings' => $conf,
133
  );
134

    
135
  // Get the field output, and the title.
136
  if (!empty($conf['formatter_settings'])) {
137
    $field_settings['settings'] = $conf['formatter_settings'];
138
  }
139

    
140
  $clone = clone $entity;
141
  $all_values = field_get_items($entity_type, $entity, $field_name, $language);
142

    
143
  if (is_array($all_values)) {
144
    // Reverse values.
145
    if (isset($conf['delta_reversed']) && $conf['delta_reversed']) {
146
      $all_values = array_reverse($all_values, TRUE);
147
    }
148

    
149
    if (isset($conf['delta_limit'])) {
150
      $offset = intval($conf['delta_offset']);
151
      $limit = !empty($conf['delta_limit']) ? $conf['delta_limit'] : NULL;
152
      $all_values = array_slice($all_values, $offset, $limit, TRUE);
153
    }
154

    
155
    $clone->{$field_name}[$language] = $all_values;
156
  }
157

    
158
  $field_output = field_view_field($entity_type, $clone, $field_name, $field_settings, $language);
159

    
160
  if (!empty($field_output)) {
161
    if (!empty($conf['override_title'])) {
162
      $field_output['#title'] = filter_xss_admin($conf['override_title_text']);
163
    }
164
    $field_output['#ctools_context'] = $context;
165
    $field_output['#post_render'][] = 'ctools_entity_field_content_type_substitute_keywords';
166
  }
167

    
168
  // Build the content type block.
169
  $block = new stdClass();
170
  $block->module  = 'entity_field';
171
  if ($conf['label'] == 'title' && isset($field_output['#title'])) {
172
    $block->title = $field_output['#title'];
173
  }
174

    
175
  $block->content = $field_output;
176
  $block->delta   = $ids[0];
177

    
178
  return $block;
179
}
180

    
181
/**
182
 * Replace context keywords.
183
 */
184
function ctools_entity_field_content_type_substitute_keywords($markup, array $element) {
185
  return ctools_context_keyword_substitute($markup, array(), array($element['#ctools_context']));
186
}
187

    
188
/**
189
* Returns an edit form for custom type settings.
190
*/
191
function ctools_entity_field_content_type_formatter_options($form, &$form_state) {
192
  if (empty($form_state['conf']['formatter_settings'])) {
193
    $form_state['conf']['formatter_settings'] = array();
194
  }
195
  $conf = $form_state['conf'];
196
  $subtype = $form_state['subtype_name'];
197
  list($entity_type, $field_name) = explode(':', $subtype, 2);
198

    
199
  $field = field_info_field($field_name);
200
  module_load_include('inc', 'field_ui', 'field_ui.admin');
201
  $formatter_options = field_ui_formatter_options($field['type']);
202

    
203
  $field_label_options = array(
204
    'title' => t('Pane title'),
205
    'above' => t('Above'),
206
    'inline' => t('Inline'),
207
    'hidden' => t('Hidden'),
208
  );
209

    
210
  $form['label'] = array(
211
    '#type' => 'select',
212
    '#title' => t('Label'),
213
    '#options' => $field_label_options,
214
    '#default_value' => $conf['label'],
215
  );
216

    
217
  $form['formatter'] = array(
218
    '#type' => 'select',
219
    '#title' => t('Select a formatter'),
220
    '#options' => $formatter_options,
221
    '#default_value' => $conf['formatter'],
222
  );
223

    
224
  return $form;
225
}
226

    
227
function ctools_entity_field_content_type_formatter_options_submit($form, &$form_state) {
228
  $form_state['conf']['formatter'] = $form_state['values']['formatter'];
229
  $form_state['conf']['label'] = $form_state['values']['label'];
230
}
231

    
232
function ctools_entity_field_content_type_formatter_styles($form, &$form_state) {
233
  if (!$form_state['conf']['formatter_settings']) {
234
    $form_state['conf']['formatter_settings'] = array();
235
  }
236
  $conf = $form_state['conf'];
237
  $subtype = $form_state['subtype_name'];
238
  list($entity_type, $field_name) = explode(':', $subtype, 2);
239
  $field = field_info_field($field_name);
240

    
241
  ctools_form_include($form_state, 'field_ui.admin', 'field_ui', '');
242
  ctools_form_include($form_state, 'fields');
243

    
244
  $form['ctools_keywords'] = array(
245
    '#type' => 'item',
246
    '#description' => t('You may use keywords for substitutions.'),
247
  );
248

    
249
  $form['ctools_field_list'] = array(
250
    '#type' => 'value',
251
    '#value' => array(),
252
  );
253

    
254
  ctools_fields_get_field_formatter_settings_form($field, $conf['formatter'], $form, $form_state);
255
  return $form;
256
}
257

    
258
function ctools_entity_field_content_type_formatter_styles_submit($form, &$form_state) {
259
  $fields = $form_state['values']['ctools_field_list'];
260
  $formatter_info = ctools_fields_get_field_formatter_info($fields);
261
  foreach ($formatter_info as $info) {
262
    if (!empty($info['settings'])) {
263
      foreach ($info['settings'] as $field_name => $value) {
264
        if (isset($form_state['values'][$field_name])) {
265
          $form_state['conf']['formatter_settings'][$field_name] = $form_state['values'][$field_name];
266
        }
267
      }
268
    }
269
  }
270

    
271
  if (isset($form_state['values']['delta_limit'])) {
272
    $form_state['conf']['delta_limit'] = $form_state['values']['delta_limit'];
273
    $form_state['conf']['delta_offset'] = $form_state['values']['delta_offset'];
274
    $form_state['conf']['delta_reversed'] = $form_state['values']['delta_reversed'];
275
  }
276
}
277

    
278
/**
279
 * Returns the administrative title for a type.
280
 */
281
function ctools_entity_field_content_type_admin_title($subtype, $conf, $context) {
282
  list($bundle, $field_name) = explode(':', $subtype);
283
  ctools_include('fields');
284
  if (is_object($context) && isset($context->identifier)) {
285
    $identifier = $context->identifier;
286
  }
287
  else {
288
    watchdog('ctools_entity_field_content_type_admin_title', 'Context is missing for field: @name', array('@name' => $subtype), WATCHDOG_NOTICE);
289
    $identifier = t('Unknown');
290
  }
291

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