3 namespace Drupal\views\Plugin\views\area;
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Views;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Views area handlers. Insert a view inside of an area.
13 * @ingroup views_area_handlers
17 class View extends AreaPluginBase {
20 * Stores whether the embedded view is actually empty.
29 * @var \Drupal\Core\Entity\EntityStorageInterface
31 protected $viewStorage;
34 * Constructs a View object.
36 * @param array $configuration
37 * A configuration array containing information about the plugin instance.
38 * @param string $plugin_id
39 * The plugin_id for the plugin instance.
40 * @param mixed $plugin_definition
41 * The plugin implementation definition.
42 * @param \Drupal\Core\Entity\EntityStorageInterface $view_storage
45 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $view_storage) {
46 parent::__construct($configuration, $plugin_id, $plugin_definition);
48 $this->viewStorage = $view_storage;
54 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
59 $container->get('entity.manager')->getStorage('view')
66 protected function defineOptions() {
67 $options = parent::defineOptions();
69 $options['view_to_insert'] = ['default' => ''];
70 $options['inherit_arguments'] = ['default' => FALSE];
77 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
78 parent::buildOptionsForm($form, $form_state);
80 $view_display = $this->view->storage->id() . ':' . $this->view->current_display;
82 $options = ['' => $this->t('-Select-')];
83 $options += Views::getViewsAsOptions(FALSE, 'all', $view_display, FALSE, TRUE);
84 $form['view_to_insert'] = [
86 '#title' => $this->t('View to insert'),
87 '#default_value' => $this->options['view_to_insert'],
88 '#description' => $this->t('The view to insert into this area.'),
89 '#options' => $options,
92 $form['inherit_arguments'] = [
93 '#type' => 'checkbox',
94 '#title' => $this->t('Inherit contextual filters'),
95 '#default_value' => $this->options['inherit_arguments'],
96 '#description' => $this->t('If checked, this view will receive the same contextual filters as its parent.'),
103 public function render($empty = FALSE) {
104 if (!empty($this->options['view_to_insert'])) {
105 list($view_name, $display_id) = explode(':', $this->options['view_to_insert']);
107 $view = $this->viewStorage->load($view_name)->getExecutable();
109 if (empty($view) || !$view->access($display_id)) {
112 $view->setDisplay($display_id);
115 $view->parent_views += $this->view->parent_views;
116 $view->parent_views[] = "$view_name:$display_id";
118 // Check if the view is part of the parent views of this view
119 $search = "$view_name:$display_id";
120 if (in_array($search, $this->view->parent_views)) {
121 drupal_set_message(t("Recursion detected in view @view display @display.", ['@view' => $view_name, '@display' => $display_id]), 'error');
124 if (!empty($this->options['inherit_arguments']) && !empty($this->view->args)) {
125 $output = $view->preview($display_id, $this->view->args);
128 $output = $view->preview($display_id);
130 $this->isEmpty = $view->display_handler->outputIsEmpty();
140 public function isEmpty() {
141 if (isset($this->isEmpty)) {
142 return $this->isEmpty;
145 return parent::isEmpty();
152 public function calculateDependencies() {
153 $dependencies = parent::calculateDependencies();
155 list($view_id) = explode(':', $this->options['view_to_insert'], 2);
156 // Don't call the current view, as it would result into an infinite recursion.
157 if ($view_id && $this->view->storage->id() != $view_id) {
158 $view = $this->viewStorage->load($view_id);
159 $dependencies[$view->getConfigDependencyKey()][] = $view->getConfigDependencyName();
162 return $dependencies;