Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 */
6

    
7
$plugin = array(
8
  'title' => t('Entity extra field'),
9
  'defaults' => array('view_mode' => NULL),
10
  'content type' => 'ctools_entity_field_extra_content_type_content_type',
11
);
12

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

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

    
37
  $types = array();
38
  $context_types = array();
39
  $entities = entity_get_info();
40

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

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

    
66
  return $types;
67
}
68

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

    
77
  $info = entity_get_info($entity_type);
78
  $view_mode_options = array();
79
  foreach ($info['view modes'] as $mode => $option) {
80
    $view_mode_options[$mode] = $option['label'];
81
  }
82

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

    
91
  return $form;
92
}
93

    
94
function ctools_entity_field_extra_content_type_edit_form_submit($form, &$form_state) {
95
  $form_state['conf']['view_mode'] = $form_state['values']['view_mode'];
96
}
97

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

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

    
120
    module_invoke_all($entity_type . '_view', $entity, $conf['view_mode'], $langcode);
121
    module_invoke_all('entity_view', $entity, $entity_type, $conf['view_mode'], $langcode);
122
  }
123

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

    
134
function ctools_entity_field_extra_content_type_admin_title($subtype, $conf, $context) {
135
  $info = ctools_entity_field_extra_content_type_content_type($subtype);
136
  return t('"@s" @field', array('@s' => $context->identifier, '@field' => $info['title']));
137
}