Projet

Général

Profil

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

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

1
<?php
2

    
3
$plugin = array(
4
  'title' => t('Entity extra field'),
5
  'defaults' => array('view_mode' => NULL),
6
  'content type' => 'ctools_entity_field_extra_content_type_content_type',
7
);
8

    
9
/**
10
 * Just one subtype.
11
 *
12
 * Ordinarily this function is meant to get just one subtype. However, we are
13
 * using it to deal with the fact that we have changed the subtype names. This
14
 * lets us translate the name properly.
15
 */
16
function ctools_entity_field_extra_content_type_content_type($subtype) {
17
  $types = ctools_entity_field_extra_content_type_content_types();
18
  if (isset($types[$subtype])) {
19
    return $types[$subtype];
20
  }
21
}
22

    
23
/**
24
 * Return all extra field content types available.
25
 */
26
function ctools_entity_field_extra_content_type_content_types() {
27
  // This will hold all the individual field content types.
28
  $types = &drupal_static(__FUNCTION__);
29
  if (isset($types)) {
30
    return $types;
31
  }
32

    
33
  $types = array();
34
  $context_types = array();
35
  $entities = entity_get_info();
36

    
37
  foreach ($entities as $entity_type => $entity) {
38
    foreach ($entity['bundles'] as $type => $bundle) {
39
      foreach (field_info_extra_fields($entity_type, $type, 'display') as $field_name => $info) {
40
        if (!isset($types[$entity_type . ':' . $field_name])) {
41
          $types[$entity_type . ':' . $field_name] = array(
42
            'category' => t(ucfirst($entity_type)),
43
            'icon' => 'icon_field.png',
44
            'title' => $info['label'],
45
            'description' => isset($info['description']) ? $info['description'] : '',
46
          );
47
        }
48
        $context_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
49
      }
50
    }
51
  }
52

    
53
  // Create the required context for each field related to the bundle types.
54
  foreach ($types as $key => $field_content_type) {
55
    list($entity_type, $field_name) = explode(':', $key, 2);
56
    $types[$key]['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
57
      'type' => array_keys($context_types[$key]['types']),
58
    ));
59
    unset($context_types[$key]['types']);
60
  }
61

    
62
  return $types;
63
}
64

    
65
/**
66
 * Returns an edit form for an extra field.
67
 */
68
function ctools_entity_field_extra_content_type_edit_form($form, &$form_state) {
69
  $conf = $form_state['conf'];
70
  $subtype = $form_state['subtype_name'];
71
  list($entity_type, $field_name) = explode(':', $subtype, 2);
72

    
73
  $info = entity_get_info($entity_type);
74
  $view_mode_options = array();
75
  foreach ($info['view modes'] as $mode => $option) {
76
    $view_mode_options[$mode] = $option['label'];
77
  }
78

    
79
  $form['view_mode'] = array(
80
    '#title' => t('View mode'),
81
    '#type' => 'select',
82
    '#description' => t('Select a view mode for this extra field.'),
83
    '#options' => $view_mode_options,
84
    '#default_value' => $conf['view_mode'],
85
  );
86

    
87
  return $form;
88
}
89

    
90
function ctools_entity_field_extra_content_type_edit_form_submit($form, &$form_state) {
91
  $form_state['conf']['view_mode'] = $form_state['values']['view_mode'];
92
}
93

    
94
/**
95
 * Render the extra field.
96
 */
97
function ctools_entity_field_extra_content_type_render($subtype, $conf, $panel_args, $context) {
98
  if (empty($context) || empty($context->data)) {
99
    return;
100
  }
101
  // Get a shortcut to the entity.
102
  $entity = clone $context->data;
103
  list($entity_type, $field_name) = explode(':', $subtype, 2);
104
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
105
  $langcode = $GLOBALS['language_content']->language;
106

    
107
  $function = $entity_type . '_view';
108
  if (in_array($entity_type, array('node', 'taxonomy_term', 'user')) && function_exists($function)) {
109
    // Call known ENTITY_view() to get the extra field.
110
    $entity->content = $function($entity, $conf['view_mode'], $langcode);
111
  }
112
  else {
113
    // Invoke the view-hook to get the extra field.
114
    $entity->content = array();
115

    
116
    module_invoke_all($entity_type . '_view', $entity, $conf['view_mode'], $langcode);
117
    module_invoke_all('entity_view', $entity, $entity_type, $conf['view_mode'], $langcode);
118
  }
119

    
120
  if (isset($entity->content[$field_name])) {
121
    // Build the content type block.
122
    $block = new stdClass();
123
    $block->module  = 'entity_field_extra';
124
    $block->content = $entity->content[$field_name];
125
    $block->delta   = $id;
126
    return $block;
127
  }
128
}
129

    
130
function ctools_entity_field_extra_content_type_admin_title($subtype, $conf, $context) {
131
  $info = ctools_entity_field_extra_content_type_content_type($subtype);
132
  return t('"@s" @field', array('@s' => $context->identifier, '@field' => $info['title']));
133
}