4 * Renders a full entity in a views area.
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);
17 function options_form(&$form, &$form_state) {
18 parent::options_form($form, $form_state);
20 $entity_type_options = array();
21 foreach (entity_get_info() as $entity_type => $entity_info) {
22 $entity_type_options[$entity_type] = $entity_info['label'];
25 $entity_type = $this->options['entity_type'];
27 $form['entity_type'] = array(
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,
34 'path' => views_ui_build_form_url($form_state),
36 '#submit' => array('views_ui_config_item_form_submit_temporary'),
37 '#executes_submit_callback' => TRUE,
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'],
48 $entity_info = entity_get_info($entity_type);
50 if (!empty($entity_info['view modes'])) {
51 foreach ($entity_info['view modes'] as $mode => $settings) {
52 $options[$mode] = $settings['label'];
56 if (count($options) > 1) {
57 $form['view_mode'] = array(
59 '#options' => $options,
60 '#title' => t('View mode'),
61 '#default_value' => $this->options['view_mode'],
65 $form['view_mode_info'] = array(
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'),
71 $form['view_mode'] = array(
73 '#value' => $options ? key($options) : 'default',
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']),
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(
91 '@entity_type' => $this->options['entity_type'],
92 '@entity_id' => $this->options['entity_id'],
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']);
105 * Render an entity using the view mode.
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);