You are here

class View in Drupal 10

Same name in this branch
  1. 10 core/modules/views/src/Element/View.php \Drupal\views\Element\View
  2. 10 core/modules/views/src/Entity/View.php \Drupal\views\Entity\View
  3. 10 core/modules/views/src/Plugin/views/area/View.php \Drupal\views\Plugin\views\area\View
Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/area/View.php \Drupal\views\Plugin\views\area\View
  2. 9 core/modules/views/src/Plugin/views/area/View.php \Drupal\views\Plugin\views\area\View

Views area handlers. Insert a view inside of an area.

Plugin annotation

@ViewsArea("view");

Hierarchy

  • class \Drupal\views\Plugin\views\area\AreaPluginBase extends \Drupal\views\Plugin\views\HandlerBase
    • class \Drupal\views\Plugin\views\area\View

Expanded class hierarchy of View

Related topics

1 file declares its use of View
ViewTest.php in core/modules/views/tests/src/Unit/Plugin/area/ViewTest.php
29 string references to 'View'
BookAdminEditForm::bookAdminTableTree in core/modules/book/src/Form/BookAdminEditForm.php
Helps build the main table in the book administration page form.
BookAdminEditForm::submitForm in core/modules/book/src/Form/BookAdminEditForm.php
Form submission handler.
BookTest::testAdminBookNodeListing in core/modules/book/tests/src/Functional/BookTest.php
Tests the administrative listing of all book pages in a book.
CommentForm::save in core/modules/comment/src/CommentForm.php
Form submission handler for the 'save' action.
contact.links.task.yml in core/modules/contact/contact.links.task.yml
core/modules/contact/contact.links.task.yml

... See full list

File

core/modules/views/src/Plugin/views/area/View.php, line 17

Namespace

Drupal\views\Plugin\views\area
View source
class View extends AreaPluginBase {

  /**
   * Stores whether the embedded view is actually empty.
   *
   * @var bool
   */
  protected $isEmpty;

  /**
   * The view storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $viewStorage;

  /**
   * Constructs a View object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $view_storage
   *   The view storage.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $view_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->viewStorage = $view_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager')
      ->getStorage('view'));
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['view_to_insert'] = [
      'default' => '',
    ];
    $options['inherit_arguments'] = [
      'default' => FALSE,
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $view_display = $this->view->storage
      ->id() . ':' . $this->view->current_display;
    $options = [
      '' => $this
        ->t('-Select-'),
    ];
    $options += Views::getViewsAsOptions(FALSE, 'all', $view_display, FALSE, TRUE);
    $form['view_to_insert'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('View to insert'),
      '#default_value' => $this->options['view_to_insert'],
      '#description' => $this
        ->t('The view to insert into this area.'),
      '#options' => $options,
    ];
    $form['inherit_arguments'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Inherit contextual filters'),
      '#default_value' => $this->options['inherit_arguments'],
      '#description' => $this
        ->t('If checked, this view will receive the same contextual filters as its parent.'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function render($empty = FALSE) {
    if (!empty($this->options['view_to_insert'])) {
      [
        $view_name,
        $display_id,
      ] = explode(':', $this->options['view_to_insert']);
      $view = $this->viewStorage
        ->load($view_name)
        ->getExecutable();
      if (empty($view) || !$view
        ->access($display_id)) {
        return [];
      }
      $view
        ->setDisplay($display_id);

      // Avoid recursion
      $view->parent_views += $this->view->parent_views;
      $view->parent_views[] = "{$view_name}:{$display_id}";

      // Check if the view is part of the parent views of this view
      $search = "{$view_name}:{$display_id}";
      if (in_array($search, $this->view->parent_views)) {
        \Drupal::messenger()
          ->addError(t("Recursion detected in view @view display @display.", [
          '@view' => $view_name,
          '@display' => $display_id,
        ]));
      }
      else {
        if (!empty($this->options['inherit_arguments']) && !empty($this->view->args)) {
          $output = $view
            ->preview($display_id, $this->view->args);
        }
        else {
          $output = $view
            ->preview($display_id);
        }
        $this->isEmpty = $view->display_handler
          ->outputIsEmpty();
        return $output;
      }
    }
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    if (isset($this->isEmpty)) {
      return $this->isEmpty;
    }
    else {
      return parent::isEmpty();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    [
      $view_id,
    ] = explode(':', $this->options['view_to_insert'], 2);

    // Don't call the current view, as it would result into an infinite recursion.
    if ($view_id && $this->view->storage
      ->id() != $view_id) {
      $view = $this->viewStorage
        ->load($view_id);
      $dependencies[$view
        ->getConfigDependencyKey()][] = $view
        ->getConfigDependencyName();
    }
    return $dependencies;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AreaPluginBase::$areaType public property The type of this area handler, i.e. 'header', 'footer', or 'empty'.
AreaPluginBase::adminSummary public function
AreaPluginBase::init public function Overrides Drupal\views\Plugin\views\HandlerBase::init(). 1
AreaPluginBase::preRender public function Performs any operations needed before full rendering. 1
AreaPluginBase::usesGroupBy public function
View::$isEmpty protected property Stores whether the embedded view is actually empty.
View::$viewStorage protected property The view storage.
View::buildOptionsForm public function Overrides AreaPluginBase::buildOptionsForm
View::calculateDependencies public function
View::create public static function
View::defineOptions protected function Overrides AreaPluginBase::defineOptions
View::isEmpty public function Does that area have nothing to show. Overrides AreaPluginBase::isEmpty
View::render public function Render the area. Overrides AreaPluginBase::render
View::__construct public function Constructs a View object.