Commit | Line | Data |
---|---|---|
7f254ad8 AE |
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.'), | |
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 | if (!empty($entity_type) && !empty($entity_id) && !empty($view_mode)) { | |
109 | $entity = entity_load_single($entity_type, $entity_id); | |
110 | if (!empty($this->options['bypass_access']) || entity_access('view', $entity_type, $entity)) { | |
111 | $render = entity_view($entity_type, array($entity), $view_mode); | |
112 | $render_entity = reset($render); | |
113 | return drupal_render($render_entity); | |
114 | } | |
115 | } | |
116 | else { | |
117 | return ''; | |
118 | } | |
119 | } | |
120 | } |