views_handler_area_view.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_area_view.
  5. */
  6. /**
  7. * Views area handlers. Insert a view inside of an area.
  8. *
  9. * @ingroup views_area_handlers
  10. */
  11. class views_handler_area_view extends views_handler_area {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function option_definition() {
  16. $options = parent::option_definition();
  17. $options['view_to_insert'] = array('default' => '');
  18. $options['inherit_arguments'] = array('default' => FALSE, 'bool' => TRUE);
  19. return $options;
  20. }
  21. /**
  22. * Default options form; provides the label widget all fields should have.
  23. */
  24. public function options_form(&$form, &$form_state) {
  25. parent::options_form($form, $form_state);
  26. $view_display = $this->view->name . ':' . $this->view->current_display;
  27. $options = array('' => t('-Select-'));
  28. $options += views_get_views_as_options(FALSE, 'all', $view_display, FALSE, TRUE);
  29. $form['view_to_insert'] = array(
  30. '#type' => 'select',
  31. '#title' => t('View to insert'),
  32. '#default_value' => $this->options['view_to_insert'],
  33. '#description' => t('The view to insert into this area.'),
  34. '#options' => $options,
  35. );
  36. $form['inherit_arguments'] = array(
  37. '#type' => 'checkbox',
  38. '#title' => t('Inherit contextual filters'),
  39. '#default_value' => $this->options['inherit_arguments'],
  40. '#description' => t('If checked, this view will receive the same contextual filters as its parent.'),
  41. );
  42. }
  43. /**
  44. * Render the area.
  45. */
  46. public function render($empty = FALSE) {
  47. if ($view = $this->loadView()) {
  48. if (!empty($this->options['inherit_arguments']) && !empty($this->view->args)) {
  49. return $view->preview(NULL, $this->view->args);
  50. }
  51. else {
  52. return $view->preview(NULL);
  53. }
  54. }
  55. return '';
  56. }
  57. /**
  58. * Loads the used view for rendering.
  59. *
  60. * @return \view|NULL
  61. * The loaded view or NULL, in case the view was not loadable / recursion
  62. * got detected / access got denied.
  63. */
  64. protected function loadView() {
  65. if (empty($this->options['view_to_insert'])) {
  66. return NULL;
  67. }
  68. list($view_name, $display_id) = explode(':', $this->options['view_to_insert']);
  69. $view = views_get_view($view_name);
  70. if (empty($view) || !$view->access($display_id)) {
  71. return NULL;
  72. }
  73. $view->set_display($display_id);
  74. // Avoid recursion.
  75. $view->parent_views += $this->view->parent_views;
  76. $view->parent_views[] = "$view_name:$display_id";
  77. // Check if the view is part of the parent views of this view.
  78. $search = "$view_name:$display_id";
  79. if (in_array($search, $this->view->parent_views)) {
  80. drupal_set_message(t("Recursion detected in view @view display @display.", array('@view' => $view_name, '@display' => $display_id)), 'error');
  81. return NULL;
  82. }
  83. return $view;
  84. }
  85. }