views_handler_area_view.inc 2.8 KB

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