getAttachedDisplays example

$view = $this->entity;
    $executable = $view->getExecutable();
    $executable->initDisplay();

    // Go through and remove displayed scheduled for removal.     $displays = $view->get('display');
    foreach ($displays as $id => $display) {
      if (!empty($display['deleted'])) {
        // Remove view display from view attachment under the attachments         // options.         $display_handler = $executable->displayHandlers->get($id);
        if ($attachments = $display_handler->getAttachedDisplays()) {
          foreach ($attachments as $attachment) {
            $attached_options = $executable->displayHandlers->get($attachment)->getOption('displays');
            unset($attached_options[$id]);
            $executable->displayHandlers->get($attachment)->setOption('displays', $attached_options);
          }
        }
        $executable->displayHandlers->remove($id);
        unset($displays[$id]);
      }
    }

    
public function attachDisplays() {
    if (!empty($this->is_attachment)) {
      return;
    }

    if (!$this->display_handler->acceptAttachments()) {
      return;
    }

    $this->is_attachment = TRUE;
    // Find out which other displays attach to the current one.     foreach ($this->display_handler->getAttachedDisplays() as $id) {
      $display_handler = $this->displayHandlers->get($id);
      // Only attach enabled attachments that the user has access to.       if ($display_handler->isEnabled() && $display_handler->access()) {
        $cloned_view = Views::executableFactory()->get($this->storage);
        $display_handler->attachTo($cloned_view$this->current_display, $this->element);
      }
    }
    $this->is_attachment = FALSE;
  }

  /** * Determines if the given user has access to the view. * * Note that this sets the display handler if it hasn't been set. * * @param string $displays * The machine name of the display. * @param \Drupal\Core\Session\AccountInterface $account * The user object. * * @return bool * TRUE if the user has access to the view, FALSE otherwise. */
$this->assertFalse($view->displayHandlers->get('page')->isDefaulted('filters'), "Make sure that 'filters'' is marked as overridden.");
  }

  /** * Tests the getAttachedDisplays method. */
  public function testGetAttachedDisplays() {
    $view = Views::getView('test_get_attach_displays');

    // Both the feed_1 and the feed_2 display are attached to the page display.     $view->setDisplay('page_1');
    $this->assertEquals(['feed_1', 'feed_2']$view->display_handler->getAttachedDisplays());

    $view->setDisplay('feed_1');
    $this->assertEquals([]$view->display_handler->getAttachedDisplays());
  }

  /** * Tests the readmore validation. */
  public function testReadMoreNoDisplay() {
    $view = Views::getView('test_display_more');
    // Confirm that the view validates when there is a page display.
/** * Tests that nothing is output when the attachment displays are disabled. */
  public function testDisabledAttachments() {
    $this->drupalCreateContentType(['type' => 'page']);
    $this->drupalCreateNode();

    // Ensure that the feed_1 display is attached to the page_1 display.     $view = Views::getView('test_attached_disabled');
    $view->setDisplay('page_1');
    $attached_displays = $view->display_handler->getAttachedDisplays();
    $this->assertContains('attachment_1', $attached_displays, 'The attachment_1 display is attached to the page display.');
    $this->assertContains('attachment_2', $attached_displays, 'The attachment_2 display is attached to the page display.');

    // Check that the attachments are output on the page display.     $this->drupalGet('test-attached-disabled');
    // Verify that the page view and the attachments are rendered.     $this->assertSession()->elementsCount('xpath', '//div[contains(@class, "view-content")]', 3);
    // Verify that the attachment is rendered before the page view.     $this->assertSession()->elementsCount('xpath', '//div[contains(@class, "attachment-before")]', 1);
    // Verify that the attachment is rendered after the page view.     $this->assertSession()->elementsCount('xpath', '//div[contains(@class, "attachment-after")]', 1);

    
/** * Tests that nothing is output when the feed display is disabled. */
  public function testDisabledFeed() {
    $this->drupalCreateContentType(['type' => 'page']);
    $this->drupalCreateNode();

    // Ensure that the feed_1 display is attached to the page_1 display.     $view = Views::getView('test_attached_disabled');
    $view->setDisplay('page_1');
    $attached_displays = $view->display_handler->getAttachedDisplays();
    $this->assertContains('feed_1', $attached_displays, 'The feed display is attached to the page display.');

    // Check that the rss header is output on the page display.     $this->drupalGet('/test-attached-disabled');
    $this->assertSession()->elementAttributeContains('xpath', '//link[@rel="alternate"]', 'type', 'application/rss+xml');
    $this->assertSession()->elementAttributeContains('xpath', '//link[@rel="alternate"]', 'href', 'test-attached-disabled.xml');

    // Disable the feed display.     $view->displayHandlers->get('feed_1')->setOption('enabled', FALSE);
    $view->save();

    
Home | Imprint | This part of the site doesn't use cookies.