Projet

Général

Profil

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

root / drupal7 / sites / all / modules / entity / ctools / content_types / entity_view.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * Content type plugin to expose rendered entities, view mode configuration
6
 * still available.
7
 */
8

    
9
$plugin = array(
10
  'title' => t('Rendered entity'),
11
  'defaults' => array('view_mode' => 'full'),
12
  'content type' => 'entity_entity_view_content_type_info',
13
);
14

    
15
/**
16
 * Get the entity content type info.
17
 */
18
function entity_entity_view_content_type_info($entity_type) {
19
  $types = entity_entity_view_content_type_content_types();
20
  if (isset($types[$entity_type])) {
21
    return $types[$entity_type];
22
  }
23
}
24

    
25
/**
26
 * Implements hook_PLUGIN_content_type_content_types().
27
 *
28
 * Rendered entity use entity types machine name as subtype name.
29
 */
30
function entity_entity_view_content_type_content_types() {
31
  $types = array();
32
  $entities = entity_get_info();
33

    
34
  foreach ($entities as $entity_type => $info) {
35
    if (entity_type_supports($entity_type, 'view')) {
36
      $types[$entity_type] = array(
37
        'title' => t('Rendered @entity_type', array('@entity_type' => $info['label'])),
38
        'category' => t('Entity'),
39
        'required context' => new ctools_context_required(t('Entity'), $entity_type),
40
      );
41
    }
42
  }
43

    
44
  return $types;
45
}
46

    
47
/**
48
 * Returns an edit form for a entity.
49
 *
50
 * Rendered entity use entity types machine name as subtype name.
51
 *
52
 * @see entity_entity_view_get_content_types()
53
 */
54
function entity_entity_view_content_type_edit_form($form, &$form_state) {
55
  $conf = $form_state['conf'];
56
  $entity_type = $form_state['subtype_name'];
57
  $entity_info = entity_get_info($entity_type);
58

    
59
  $options = array();
60
  if (!empty($entity_info['view modes'])) {
61
    foreach ($entity_info['view modes'] as $mode => $settings) {
62
      $options[$mode] = $settings['label'];
63
    }
64
  }
65

    
66
  if (count($options) > 1) {
67
    $form['view_mode'] = array(
68
      '#type' => 'select',
69
      '#options' => $options,
70
      '#title' => t('View mode'),
71
      '#default_value' => $conf['view_mode'],
72
    );
73
  }
74
  else {
75
    $form['view_mode_info'] = array(
76
      '#type' => 'item',
77
      '#title' => t('View mode'),
78
      '#description' => t('Only one view mode is available for this entity type.'),
79
      '#markup' => $options ? current($options) : t('Default'),
80
    );
81

    
82
    $form['view_mode'] = array(
83
      '#type' => 'value',
84
      '#value' => $options ? key($options) : 'default',
85
    );
86
  }
87

    
88
  return $form;
89
}
90

    
91
/**
92
 * Save selected view mode.
93
 */
94
function entity_entity_view_content_type_edit_form_submit(&$form, &$form_state) {
95
  if (isset($form_state['values']['view_mode'])) {
96
    $form_state['conf']['view_mode'] = $form_state['values']['view_mode'];
97
  }
98
}
99

    
100
/**
101
 * Implements hook_PLUGIN_content_type_render().
102
 *
103
 * Ctools requires us to return a block.
104
 *
105
 * @see ctools_content_render()
106
 */
107
function entity_entity_view_content_type_render($entity_type, $conf, $panel_args, $context) {
108
  if ($context->empty) {
109
    return;
110
  }
111
  $block = new stdClass();
112
  $block->module = 'entity';
113
  $block->delta = $entity_type . '-' . str_replace('-', '_', $conf['view_mode']);
114

    
115
  $entity_id = $context->argument;
116
  $entity = entity_load_single($entity_type, $entity_id);
117
  $block->content = entity_view($entity_type, array($entity_id => $entity), $conf['view_mode']);
118
  return $block;
119
}
120

    
121
/**
122
 * Implements hook_PLUGIN_content_type_admin_title().
123
 *
124
 * Returns the administrative title for a type.
125
 */
126
function entity_entity_view_content_type_admin_title($entity_type, $conf, $contexts) {
127
  $entity_info = entity_get_info($entity_type);
128
  $view_mode = $conf['view_mode'];
129
  if (isset($entity_info['view modes'][$view_mode])) {
130
    $view_mode = $entity_info['view modes'][$view_mode]['label'];
131
  }
132
  return t('Rendered @entity_type using view mode "@view_mode"', array('@entity_type' => $entity_info['label'], '@view_mode' => $view_mode));
133
}