entity_views_handler_field_entity.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Contains the entity_views_handler_field_entity class.
  5. */
  6. /**
  7. * A handler to provide proper displays for entities retrieved via data selection.
  8. *
  9. * This handler may only be used in conjunction with data selection based Views
  10. * tables or other base tables using a query plugin that supports data
  11. * selection.
  12. *
  13. * @see entity_views_field_definition()
  14. * @ingroup views_field_handlers
  15. */
  16. class entity_views_handler_field_entity extends views_handler_field {
  17. /**
  18. * Stores the entity type of the result entities.
  19. */
  20. public $entity_type;
  21. /**
  22. * Stores the result entities' metadata wrappers.
  23. */
  24. public $wrappers = array();
  25. /**
  26. * The entity type of the entity displayed by this field.
  27. */
  28. public $field_entity_type;
  29. /**
  30. * Stores the current value when rendering list fields.
  31. */
  32. public $current_value;
  33. /**
  34. * Initialize the entity type with the field's entity type.
  35. */
  36. public function init(&$view, &$options) {
  37. parent::init($view, $options);
  38. $this->field_entity_type = entity_property_extract_innermost_type($this->definition['type']);
  39. }
  40. /**
  41. * Overridden to add the field for the entity ID (if necessary).
  42. */
  43. public function query() {
  44. EntityFieldHandlerHelper::query($this);
  45. }
  46. /**
  47. * Adds a click-sort to the query.
  48. */
  49. public function click_sort($order) {
  50. EntityFieldHandlerHelper::click_sort($this, $order);
  51. }
  52. /**
  53. * Load the entities for all rows that are about to be displayed.
  54. */
  55. public function pre_render(&$values) {
  56. EntityFieldHandlerHelper::pre_render($this, $values);
  57. }
  58. /**
  59. * Overridden to use a metadata wrapper.
  60. */
  61. public function get_value($values, $field = NULL) {
  62. return EntityFieldHandlerHelper::get_value($this, $values, $field);
  63. }
  64. public function option_definition() {
  65. $options = parent::option_definition();
  66. $options += EntityFieldHandlerHelper::option_definition($this);
  67. $options['display'] = array('default' => 'label');
  68. $options['link_to_entity']['default'] = TRUE;
  69. $options['view_mode'] = array('default' => 'default');
  70. $options['bypass_access'] = array('default' => FALSE);
  71. return $options;
  72. }
  73. public function options_form(&$form, &$form_state) {
  74. parent::options_form($form, $form_state);
  75. EntityFieldHandlerHelper::options_form($this, $form, $form_state);
  76. // We want a different form field at a different place.
  77. unset($form['link_to_entity']);
  78. $options = array(
  79. 'label' => t('Show entity label'),
  80. 'id' => t('Show entity ID'),
  81. 'view' => t('Show complete entity'),
  82. );
  83. $form['display'] = array(
  84. '#type' => 'select',
  85. '#title' => t('Display'),
  86. '#description' => t('Decide how this field will be displayed.'),
  87. '#options' => $options,
  88. '#default_value' => $this->options['display'],
  89. );
  90. $form['link_to_entity'] = array(
  91. '#type' => 'checkbox',
  92. '#title' => t('Link to entity'),
  93. '#description' => t('Link this field to the entity.'),
  94. '#default_value' => $this->options['link_to_entity'],
  95. '#dependency' => array('edit-options-display' => array('label', 'id')),
  96. );
  97. // Stolen from entity_views_plugin_row_entity_view.
  98. $entity_info = entity_get_info($this->field_entity_type);
  99. $options = array();
  100. if (!empty($entity_info['view modes'])) {
  101. foreach ($entity_info['view modes'] as $mode => $settings) {
  102. $options[$mode] = $settings['label'];
  103. }
  104. }
  105. if (count($options) > 1) {
  106. $form['view_mode'] = array(
  107. '#type' => 'select',
  108. '#options' => $options,
  109. '#title' => t('View mode'),
  110. '#default_value' => $this->options['view_mode'],
  111. '#dependency' => array('edit-options-display' => array('view')),
  112. );
  113. }
  114. else {
  115. $form['view_mode'] = array(
  116. '#type' => 'value',
  117. '#value' => $options ? key($options) : 'default',
  118. );
  119. }
  120. $form['bypass_access'] = array(
  121. '#type' => 'checkbox',
  122. '#title' => t('Bypass access checks'),
  123. '#description' => t('If enabled, access permissions for rendering the entity are not checked.'),
  124. '#default_value' => !empty($this->options['bypass_access']),
  125. );
  126. }
  127. public function render($values) {
  128. return EntityFieldHandlerHelper::render($this, $values);
  129. }
  130. /**
  131. * Render a value as a link to the entity if applicable.
  132. *
  133. * @param $value
  134. * The value to render.
  135. * @param $values
  136. * The values for the current row retrieved from the Views query, as an
  137. * object.
  138. */
  139. public function render_entity_link($entity, $values) {
  140. $type = $this->field_entity_type;
  141. if (!is_object($entity) && isset($entity) && $entity !== FALSE) {
  142. $entity = entity_load_single($type, $entity);
  143. }
  144. if (!$entity) {
  145. return '';
  146. }
  147. $render = $this->render_single_value($entity, $values);
  148. if (!$this->options['link_to_entity'] || $this->options['display'] == 'view') {
  149. return $render;
  150. }
  151. if (is_object($entity) && ($url = entity_uri($type, $entity))) {
  152. return l($render, $url['path'], array('html' => TRUE) + $url['options']);
  153. }
  154. return $render;
  155. }
  156. /**
  157. * Render a single field value.
  158. */
  159. public function render_single_value($entity, $values) {
  160. $type = $this->field_entity_type;
  161. if (!is_object($entity) && isset($entity) && $entity !== FALSE) {
  162. $entity = entity_load_single($type, $entity);
  163. }
  164. // Make sure the entity exists and access is either given or bypassed.
  165. if (!$entity || !(!empty($this->options['bypass_access']) || entity_access('view', $type, $entity))) {
  166. return '';
  167. }
  168. if ($this->options['display'] === 'view') {
  169. $entity_view = entity_view($type, array($entity), $this->options['view_mode']);
  170. return render($entity_view);
  171. }
  172. if ($this->options['display'] == 'label') {
  173. $value = entity_label($type, $entity);
  174. }
  175. // Either $options[display] == 'id', or we have no label.
  176. if (empty($value)) {
  177. $value = entity_id($type, $entity);
  178. }
  179. $value = $this->sanitize_value($value);
  180. return $value;
  181. }
  182. }