entity_views_plugin_row_entity_view.inc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * Row style plugin for displaying the results as entities.
  5. */
  6. /**
  7. * Plugin class for displaying Views results with entity_view.
  8. */
  9. class entity_views_plugin_row_entity_view extends views_plugin_row {
  10. protected $entity_type, $entities;
  11. public function init(&$view, &$display, $options = NULL) {
  12. parent::init($view, $display, $options);
  13. // Initialize the entity-type used.
  14. $table_data = views_fetch_data($this->view->base_table);
  15. $this->entity_type = $table_data['table']['entity type'];
  16. // Set base table and field information as used by views_plugin_row to
  17. // select the entity id if used with default query class.
  18. $info = entity_get_info($this->entity_type);
  19. if (!empty($info['base table']) && $info['base table'] == $this->view->base_table) {
  20. $this->base_table = $info['base table'];
  21. $this->base_field = $info['entity keys']['id'];
  22. }
  23. }
  24. public function option_definition() {
  25. $options = parent::option_definition();
  26. $options['view_mode'] = array('default' => 'full');
  27. return $options;
  28. }
  29. public function options_form(&$form, &$form_state) {
  30. parent::options_form($form, $form_state);
  31. $entity_info = entity_get_info($this->entity_type);
  32. $options = array();
  33. if (!empty($entity_info['view modes'])) {
  34. foreach ($entity_info['view modes'] as $mode => $settings) {
  35. $options[$mode] = $settings['label'];
  36. }
  37. }
  38. if (count($options) > 1) {
  39. $form['view_mode'] = array(
  40. '#type' => 'select',
  41. '#options' => $options,
  42. '#title' => t('View mode'),
  43. '#default_value' => $this->options['view_mode'],
  44. );
  45. }
  46. else {
  47. $form['view_mode_info'] = array(
  48. '#type' => 'item',
  49. '#title' => t('View mode'),
  50. '#description' => t('Only one view mode is available for this entity type.'),
  51. '#markup' => $options ? current($options) : t('Default'),
  52. );
  53. $form['view_mode'] = array(
  54. '#type' => 'value',
  55. '#value' => $options ? key($options) : 'default',
  56. );
  57. }
  58. return $form;
  59. }
  60. public function pre_render($values) {
  61. if (!empty($values)) {
  62. list($this->entity_type, $this->entities) = $this->view->query->get_result_entities($values, !empty($this->relationship) ? $this->relationship : NULL, isset($this->field_alias) ? $this->field_alias : NULL);
  63. }
  64. // Render the entities.
  65. if ($this->entities) {
  66. $render = entity_view($this->entity_type, $this->entities, $this->options['view_mode']);
  67. // Remove the first level of the render array.
  68. $this->rendered_content = reset($render);
  69. }
  70. }
  71. /**
  72. * Overridden to return the entity object.
  73. */
  74. function get_value($values, $field = NULL) {
  75. return isset($this->entities[$this->view->row_index]) ? $this->entities[$this->view->row_index] : FALSE;
  76. }
  77. public function render($values) {
  78. if ($entity = $this->get_value($values)) {
  79. // Add the view object as views_plugin_row_node_view::render() would.
  80. // Otherwise the views theme suggestions won't work properly.
  81. $entity->view = $this->view;
  82. $render = $this->rendered_content[entity_id($this->entity_type, $entity)];
  83. return drupal_render($render);
  84. }
  85. }
  86. }