Projet

Général

Profil

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

root / drupal7 / sites / all / modules / entity / views / handlers / entity_views_handler_area_entity.inc @ 082b75eb

1
<?php
2
/**
3
 * @file
4
 * Renders a full entity in a views area.
5
 */
6

    
7
class entity_views_handler_area_entity extends views_handler_area {
8
  public function option_definition() {
9
    $options = parent::option_definition();
10
    $options['entity_type'] = array('default' => 'node');
11
    $options['entity_id'] = array('default' => '');
12
    $options['view_mode'] = array('default' => 'full');
13
    $options['bypass_access'] = array('default' => FALSE);
14
    return $options;
15
  }
16

    
17
  function options_form(&$form, &$form_state) {
18
    parent::options_form($form, $form_state);
19

    
20
    $entity_type_options = array();
21
    foreach (entity_get_info() as $entity_type => $entity_info) {
22
      $entity_type_options[$entity_type] = $entity_info['label'];
23
    }
24

    
25
    $entity_type = $this->options['entity_type'];
26

    
27
    $form['entity_type'] = array(
28
      '#type' => 'select',
29
      '#title' => t('Entity type'),
30
      '#options' => $entity_type_options,
31
      '#description' => t('Choose the entity type you want to display in the area.'),
32
      '#default_value' => $entity_type,
33
      '#ajax' => array(
34
        'path' => views_ui_build_form_url($form_state),
35
      ),
36
      '#submit' => array('views_ui_config_item_form_submit_temporary'),
37
      '#executes_submit_callback' => TRUE,
38
    );
39

    
40
    $form['entity_id'] = array(
41
      '#type' => 'textfield',
42
      '#title' => t('Entity id'),
43
      '#description' => t('Choose the entity you want to display in the area. To render an entity given by a contextual filter use "%1" for the first argument, "%2" for the second, etc.'),
44
      '#default_value' => $this->options['entity_id'],
45
    );
46

    
47
    if ($entity_type) {
48
      $entity_info = entity_get_info($entity_type);
49
      $options = array();
50
      if (!empty($entity_info['view modes'])) {
51
        foreach ($entity_info['view modes'] as $mode => $settings) {
52
          $options[$mode] = $settings['label'];
53
        }
54
      }
55

    
56
      if (count($options) > 1) {
57
        $form['view_mode'] = array(
58
          '#type' => 'select',
59
          '#options' => $options,
60
          '#title' => t('View mode'),
61
          '#default_value' => $this->options['view_mode'],
62
        );
63
      }
64
      else {
65
        $form['view_mode_info'] = array(
66
          '#type' => 'item',
67
          '#title' => t('View mode'),
68
          '#description' => t('Only one view mode is available for this entity type.'),
69
          '#markup' => $options ? current($options) : t('Default'),
70
        );
71
        $form['view_mode'] = array(
72
          '#type' => 'value',
73
          '#value' => $options ? key($options) : 'default',
74
        );
75
      }
76
    }
77
    $form['bypass_access'] = array(
78
      '#type' => 'checkbox',
79
      '#title' => t('Bypass access checks'),
80
      '#description' => t('If enabled, access permissions for rendering the entity are not checked.'),
81
      '#default_value' => !empty($this->options['bypass_access']),
82
    );
83
    return $form;
84
  }
85

    
86
  public function admin_summary() {
87
    $label = parent::admin_summary();
88
    if (!empty($this->options['entity_id'])) {
89
      return t('@label @entity_type:@entity_id', array(
90
        '@label' => $label,
91
        '@entity_type' => $this->options['entity_type'],
92
        '@entity_id' => $this->options['entity_id'],
93
      ));
94
    }
95
  }
96

    
97
  public function render($empty = FALSE) {
98
    if (!$empty || !empty($this->options['empty'])) {
99
      return $this->render_entity($this->options['entity_type'], $this->options['entity_id'], $this->options['view_mode']);
100
    }
101
    return '';
102
  }
103

    
104
  /**
105
   * Render an entity using the view mode.
106
   */
107
  public function render_entity($entity_type, $entity_id, $view_mode) {
108
    $tokens = $this->get_render_tokens();
109
    // Replace argument tokens in entity id.
110
    $entity_id = strtr($entity_id, $tokens);
111
    if (!empty($entity_type) && !empty($entity_id) && !empty($view_mode)) {
112
      $entity = entity_load_single($entity_type, $entity_id);
113
      if (!empty($this->options['bypass_access']) || entity_access('view', $entity_type, $entity)) {
114
        $render = entity_view($entity_type, array($entity), $view_mode);
115
        $render_entity = reset($render);
116
        return drupal_render($render_entity);
117
      }
118
    }
119
    else {
120
      return '';
121
    }
122
  }
123

    
124
  /**
125
   * Get the 'render' tokens to use for advanced rendering.
126
   *
127
   * This runs through all of the fields and arguments that
128
   * are available and gets their values. This will then be
129
   * used in one giant str_replace().
130
   */
131
  function get_render_tokens() {
132
    $tokens = array();
133
    if (!empty($this->view->build_info['substitutions'])) {
134
      $tokens = $this->view->build_info['substitutions'];
135
    }
136
    $count = 0;
137
    foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
138
      $token = '%' . ++$count;
139
      if (!isset($tokens[$token])) {
140
        $tokens[$token] = '';
141
      }
142
      // Use strip tags as there should never be HTML in the path.
143
      // However, we need to preserve special characters like " that
144
      // were removed by check_plain().
145
      $tokens['%' . $count] = $handler->argument;
146
    }
147

    
148
    return $tokens;
149
  }
150
}