entity_views_handler_area_entity.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @file
  4. * Renders a full entity in a views area.
  5. */
  6. class entity_views_handler_area_entity extends views_handler_area {
  7. public function option_definition() {
  8. $options = parent::option_definition();
  9. $options['entity_type'] = array('default' => 'node');
  10. $options['entity_id'] = array('default' => '');
  11. $options['view_mode'] = array('default' => 'full');
  12. $options['bypass_access'] = array('default' => FALSE);
  13. return $options;
  14. }
  15. function options_form(&$form, &$form_state) {
  16. parent::options_form($form, $form_state);
  17. $entity_type_options = array();
  18. foreach (entity_get_info() as $entity_type => $entity_info) {
  19. $entity_type_options[$entity_type] = $entity_info['label'];
  20. }
  21. $entity_type = $this->options['entity_type'];
  22. $form['entity_type'] = array(
  23. '#type' => 'select',
  24. '#title' => t('Entity type'),
  25. '#options' => $entity_type_options,
  26. '#description' => t('Choose the entity type you want to display in the area.'),
  27. '#default_value' => $entity_type,
  28. '#ajax' => array(
  29. 'path' => views_ui_build_form_url($form_state),
  30. ),
  31. '#submit' => array('views_ui_config_item_form_submit_temporary'),
  32. '#executes_submit_callback' => TRUE,
  33. );
  34. $form['entity_id'] = array(
  35. '#type' => 'textfield',
  36. '#title' => t('Entity id'),
  37. '#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.'),
  38. '#default_value' => $this->options['entity_id'],
  39. );
  40. if ($entity_type) {
  41. $entity_info = entity_get_info($entity_type);
  42. $options = array();
  43. if (!empty($entity_info['view modes'])) {
  44. foreach ($entity_info['view modes'] as $mode => $settings) {
  45. $options[$mode] = $settings['label'];
  46. }
  47. }
  48. if (count($options) > 1) {
  49. $form['view_mode'] = array(
  50. '#type' => 'select',
  51. '#options' => $options,
  52. '#title' => t('View mode'),
  53. '#default_value' => $this->options['view_mode'],
  54. );
  55. }
  56. else {
  57. $form['view_mode_info'] = array(
  58. '#type' => 'item',
  59. '#title' => t('View mode'),
  60. '#description' => t('Only one view mode is available for this entity type.'),
  61. '#markup' => $options ? current($options) : t('Default'),
  62. );
  63. $form['view_mode'] = array(
  64. '#type' => 'value',
  65. '#value' => $options ? key($options) : 'default',
  66. );
  67. }
  68. }
  69. $form['bypass_access'] = array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Bypass access checks'),
  72. '#description' => t('If enabled, access permissions for rendering the entity are not checked.'),
  73. '#default_value' => !empty($this->options['bypass_access']),
  74. );
  75. return $form;
  76. }
  77. public function admin_summary() {
  78. $label = parent::admin_summary();
  79. if (!empty($this->options['entity_id'])) {
  80. return t('@label @entity_type:@entity_id', array(
  81. '@label' => $label,
  82. '@entity_type' => $this->options['entity_type'],
  83. '@entity_id' => $this->options['entity_id'],
  84. ));
  85. }
  86. }
  87. public function render($empty = FALSE) {
  88. if (!$empty || !empty($this->options['empty'])) {
  89. return $this->render_entity($this->options['entity_type'], $this->options['entity_id'], $this->options['view_mode']);
  90. }
  91. return '';
  92. }
  93. /**
  94. * Render an entity using the view mode.
  95. */
  96. public function render_entity($entity_type, $entity_id, $view_mode) {
  97. $tokens = $this->get_render_tokens();
  98. // Replace argument tokens in entity id.
  99. $entity_id = strtr($entity_id, $tokens);
  100. if (!empty($entity_type) && !empty($entity_id) && !empty($view_mode)) {
  101. $entity = entity_load_single($entity_type, $entity_id);
  102. if (!empty($this->options['bypass_access']) || entity_access('view', $entity_type, $entity)) {
  103. $render = entity_view($entity_type, array($entity), $view_mode);
  104. $render_entity = reset($render);
  105. return drupal_render($render_entity);
  106. }
  107. }
  108. else {
  109. return '';
  110. }
  111. }
  112. /**
  113. * Get the 'render' tokens to use for advanced rendering.
  114. *
  115. * This runs through all of the fields and arguments that
  116. * are available and gets their values. This will then be
  117. * used in one giant str_replace().
  118. */
  119. function get_render_tokens() {
  120. $tokens = array();
  121. if (!empty($this->view->build_info['substitutions'])) {
  122. $tokens = $this->view->build_info['substitutions'];
  123. }
  124. $count = 0;
  125. foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
  126. $token = '%' . ++$count;
  127. if (!isset($tokens[$token])) {
  128. $tokens[$token] = '';
  129. }
  130. // Use strip tags as there should never be HTML in the path.
  131. // However, we need to preserve special characters like " that
  132. // were removed by check_plain().
  133. $tokens['%' . $count] = $handler->argument;
  134. }
  135. return $tokens;
  136. }
  137. }