summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.15/sites/all/modules/ctools/plugins/content_types/entity_context/entity_field.inc
blob: fb3fad200471a8c398a060b91a9e9de22c5f2e61 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php

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

$plugin = array(
  'title' => t('Entity field'),
  'defaults' => array('label' => 'title', 'formatter' => '', 'delta_limit' => 0, 'delta_offset' => '0', 'delta_reversed' => FALSE),
  'content type' => 'ctools_entity_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_field_content_type_content_type($subtype) {
  $types = ctools_entity_field_content_type_content_types();
  if (isset($types[$subtype])) {
    return $types[$subtype];
  }
}

/**
 * Return all field content types available.
 */
function ctools_entity_field_content_type_content_types() {
  $types = &drupal_static(__FUNCTION__, array());
  if (!empty($types)) {
    return $types;
  }

  // This will hold all the individual field content types.
  $context_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(ucfirst($entity_type)),
            'icon' => 'icon_field.png',
            'title' => t('Field: @widget_label (@field_name)', array(
              '@widget_label' => t($field['label']),
              '@field_name' => $field_name,
            )),
            'description' => t('Field on the referenced entity.'),
            'edit form' => array(
              'ctools_entity_field_content_type_formatter_options' => array(
                'default' => TRUE,
                'title' => t('Formatter options for: @widget_label (@field_name)', array(
                  '@widget_label' => t($field['label']),
                  '@field_name' => $field_name,
                )),
              ),
              'ctools_entity_field_content_type_formatter_styles' => t('Formatter Styles'),
            ),
          );
        }
        $context_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(
      'type' => array_keys($context_types[$key]['types']),
    ));
    unset($context_types[$key]['types']);
  }

  return $types;
}

/**
* Render the custom content type.
*/
function ctools_entity_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;
  }
  $language = field_language($entity_type, $entity, $field_name);

  if (empty($conf['label']) || $conf['label'] == 'title') {
    $label = 'hidden';
    $conf['label'] = 'title';
  }
  else {
    $label = $conf['label'];
  }

  $field_settings = array(
    'label' => $label,
    'type' => $conf['formatter'],
  );

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

  $all_values = field_get_items($entity_type, $entity, $field_name, $language);
  if (!is_array($all_values)) {
    $all_values = array();
  }

  // Reverse values.
  if (isset($conf['delta_reversed']) && $conf['delta_reversed']) {
    $all_values = array_reverse($all_values);
  }

  if (isset($conf['delta_limit'])) {
    $delta_limit = $conf['delta_limit'];
    $offset = intval($conf['delta_offset']);
    $total = count($all_values);

    if ($delta_limit == 0) {
      $delta_limit = $total - $offset;
    }

    $new_values = array();
    for ($i = 0; $i < $delta_limit; $i++) {
      $new_delta = $offset + $i;

      if (isset($all_values[$new_delta])) {
        $new_values[] = $all_values[$new_delta];
      }
    }

    $all_values = $new_values;
  }

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

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

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

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

  return $block;
}

/**
* Returns an edit form for custom type settings.
*/
function ctools_entity_field_content_type_formatter_options($form, &$form_state) {
  if (empty($form_state['conf']['formatter_settings'])) {
    $form_state['conf']['formatter_settings'] = array();
  }
  $conf = $form_state['conf'];
  $subtype = $form_state['subtype_name'];
  list($entity_type, $field_name) = explode(':', $subtype, 2);

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

  $field_label_options = array(
    'title' => t('Pane title'),
    'above' => t('Above'),
    'inline' => t('Inline'),
    'hidden' => t('Hidden'),
  );

  $form['label'] = array(
    '#type' => 'select',
    '#title' => t('Label'),
    '#options' => $field_label_options,
    '#default_value' => $conf['label'],
  );

  $form['formatter'] = array(
    '#type' => 'select',
    '#title' => t('Select a formatter'),
    '#options' => $formatter_options,
    '#default_value' => $conf['formatter'],
  );

  return $form;
}

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

function ctools_entity_field_content_type_formatter_styles($form, &$form_state) {
  if (!$form_state['conf']['formatter_settings']) {
    $form_state['conf']['formatter_settings'] = array();
  }
  $conf = $form_state['conf'];
  $subtype = $form_state['subtype_name'];
  list($entity_type, $field_name) = explode(':', $subtype, 2);
  $field = field_info_field($field_name);

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

  $form['ctools_field_list'] = array(
    '#type' => 'value',
    '#value' => array(),
  );

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

function ctools_entity_field_content_type_formatter_styles_submit($form, &$form_state) {
  $fields = $form_state['values']['ctools_field_list'];
  $formatter_info = ctools_fields_get_field_formatter_info($fields);
  foreach ($formatter_info as $info) {
    if (!empty($info['settings'])) {
      foreach ($info['settings'] as $field_name => $value) {
        if (isset($form_state['values'][$field_name])) {
          $form_state['conf']['formatter_settings'][$field_name] = $form_state['values'][$field_name];
        }
      }
    }
  }

  if (isset($form_state['values']['delta_limit'])) {
    $form_state['conf']['delta_limit'] = $form_state['values']['delta_limit'];
    $form_state['conf']['delta_offset'] = $form_state['values']['delta_offset'];
    $form_state['conf']['delta_reversed'] = $form_state['values']['delta_reversed'];
  }
}

/**
* Returns the administrative title for a type.
*/
function ctools_entity_field_content_type_admin_title($subtype, $conf, $context) {
  list($bundle, $field_name) = explode(':', $subtype);
  ctools_include('fields');
  return t('"@s" @field', array('@s' => $context->identifier, '@field' => ctools_field_label($field_name)));
}