initDisplay example

// Test the default value of use_more_always.     $view = View::create()->getExecutable();
    $this->assertTrue($view->getDisplay()->getOption('use_more_always'), 'Always display the more link by default.');
  }

  /** * Tests the templates with empty rows. */
  public function testEmptyRow() {
    $view = Views::getView('test_page_display');
    $view->initDisplay();
    $view->newDisplay('page', 'Page', 'empty_row');
    $view->save();

    $styles = [
      'default' => '//div[@class="views-row"]',
      // Olivero does not use the 'views-col' class.       'grid' => '//div[contains(@class, "views-col") or contains(@class, "views-view-grid__item-inner")]',
      'html_list' => '//div[@class="item-list"]//li',
    ];

    $themes = ['olivero', 'stable9', 'stark', 'claro'];

    
/** * Tests result caching with filters. * * @see views_plugin_cache_time */
  public function testTimeResultCachingWithFilter() {
    // Check that we can find the test filter plugin.     $plugin = $this->container->get('plugin.manager.views.filter')->createInstance('test_filter');
    $this->assertInstanceOf(FilterPlugin::class$plugin);

    $view = Views::getView('test_filter');
    $view->initDisplay();
    $view->display_handler->overrideOption('cache', [
      'type' => 'time',
      'options' => [
        'results_lifespan' => '3600',
        'output_lifespan' => '3600',
      ],
    ]);

    // Change the filtering.     $view->displayHandlers->get('default')->overrideOption('filters', [
      'test_filter' => [
        
/** * Tests that we can successfully change a view page display path. * * @param string $path * Path that will be set as the view page display path. * * @internal */
  public function assertPagePath(string $path): void {
    $view = Views::getView('test_page_display_path');
    $view->initDisplay('page_1');
    $view->displayHandlers->get('page_1')->overrideOption('path', $path);
    $view->save();
    $this->container->get('router.builder')->rebuild();
    // Check if we successfully changed the path.     $this->drupalGet($path);
    $this->assertSession()->statusCodeEquals(200);
    // Check if we don't get any error on the view edit page.     $this->drupalGet('admin/structure/views/view/test_page_display_path');
    $this->assertSession()->statusCodeEquals(200);
  }

}

    }
  }

  /** * Tests the node row plugin. */
  public function testRowPlugin() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');
    $view = Views::getView('test_node_row_plugin');
    $view->initDisplay();
    $view->setDisplay('page_1');
    $view->initStyle();
    $view->rowPlugin->options['view_mode'] = 'full';

    // Test with view_mode full.     $output = $view->preview();
    $output = $renderer->renderRoot($output);
    foreach ($this->nodes as $node) {
      $this->assertStringNotContainsString($node->body->summary, $output, 'Make sure the teaser appears in the output of the view.');
      $this->assertStringContainsString($node->body->value, $output, 'Make sure the full text appears in the output of the view.');
    }

    
/** * Tests that all Default views work as expected. */
  public function testDefaultViews() {
    // Get all default views.     $controller = $this->container->get('entity_type.manager')->getStorage('view');
    $views = $controller->loadMultiple();

    foreach ($views as $name => $view_storage) {
      $view = $view_storage->getExecutable();
      $view->initDisplay();
      foreach ($view->storage->get('display') as $display_id => $display) {
        $view->setDisplay($display_id);

        // Add any args if needed.         if (array_key_exists($name$this->viewArgMap)) {
          $view->preExecute($this->viewArgMap[$name]);
        }

        $view->execute();

        $tokens = ['@name' => $name, '@display_id' => $display_id];
        
 'Apply');

    // Duplicate as a different display type.     $this->submitForm([], 'Duplicate as Block');
    $this->assertSession()->linkByHrefExists($path_prefix . '/block_1', 0, 'Make sure after duplicating the new display appears in the UI');
    $this->assertSession()->addressEquals($path_prefix . '/block_1');
    $this->assertSession()->pageTextContains('Block settings');
    $this->assertSession()->pageTextNotContains('Page settings');

    $this->submitForm([], 'Save');
    $view = Views::getView($view['id']);
    $view->initDisplay();

    $page_2 = $view->displayHandlers->get('page_2');
    $this->assertNotEmpty($page_2, 'The new page display got saved.');
    $this->assertEquals('Page', $page_2->display['display_title']);
    $this->assertEquals($path$page_2->display['display_options']['path']);
    $block_1 = $view->displayHandlers->get('block_1');
    $this->assertNotEmpty($block_1, 'The new block display got saved.');
    $this->assertEquals('block', $block_1->display['display_plugin']);
    $this->assertEquals('Block', $block_1->display['display_title'], 'The new display title got generated as expected.');
    $this->assertFalse(isset($block_1->display['display_options']['path']));
    $this->assertEquals($random_title$block_1->getOption('title'), 'The overridden title option from the display got copied into the duplicate');
    

    $this->submitForm($edit, 'Apply');
    $this->drupalGet($row_options_url);
    // Make sure the custom settings form field has the expected value stored.     $this->assertSession()->fieldValueEquals('row_options[test_option]', $random_name);

    $this->drupalGet($view_edit_url);
    $this->submitForm([], 'Save');
    $this->assertSession()->linkExists('Test row plugin', 0, 'Make sure the test row plugin is shown in the UI');

    $view = Views::getView($view_name);
    $view->initDisplay();
    $row = $view->display_handler->getOption('row');
    $this->assertEquals('test_row', $row['type'], 'Make sure that the test_row got saved as used row plugin.');
    $this->assertEquals($random_name$row['options']['test_option'], 'Make sure that the custom settings field got saved as expected.');

    $this->drupalGet($row_plugin_url);
    $this->submitForm(['row[type]' => 'fields'], 'Apply');
    $this->drupalGet($row_plugin_url);
    $this->assertSession()->statusCodeEquals(200);
    // Make sure that 'fields' was saved as the row plugin.     $this->assertSession()->fieldValueEquals('row[type]', 'fields');

    


  /** * Tests query of the row plugin. */
  public function testFilterQuery() {
    // Check that we can find the test filter plugin.     $plugin = $this->container->get('plugin.manager.views.filter')->createInstance('test_filter');
    $this->assertInstanceOf(FilterPlugin::class$plugin);

    $view = Views::getView('test_filter');
    $view->initDisplay();

    // Change the filtering.     $view->displayHandlers->get('default')->overrideOption('filters', [
      'test_filter' => [
        'id' => 'test_filter',
        'table' => 'views_test_data',
        'field' => 'name',
        'operator' => '=',
        'value' => 'John',
        'group' => 0,
      ],
    ]);
/** * Tests base join functionality. * * This duplicates parts of * \Drupal\Tests\views\Kernel\Plugin\JoinTest::testBasePlugin() to ensure that * no functionality provided by the base join plugin is broken. */
  public function testBase() {
    // Setup a simple join and test the result sql.     $view = Views::getView('test_view');
    $view->initDisplay();
    $view->initQuery();

    // First define a simple join without an extra condition.     // Set the various options on the join object.     $configuration = [
      'left_table' => 'views_test_data',
      'left_field' => 'uid',
      'table' => 'users_field_data',
      'field' => 'uid',
      'adjusted' => TRUE,
    ];
    
$factory = $this->container->get('views.executable');
    $this->assertInstanceOf(ViewExecutableFactory::class$factory);
    $view = View::load('test_executable_displays');
    $this->assertInstanceOf(ViewExecutable::class$factory->get($view));
  }

  /** * Tests the initDisplay() and initHandlers() methods. */
  public function testInitMethods() {
    $view = Views::getView('test_destroy');
    $view->initDisplay();

    $this->assertInstanceOf(DefaultDisplay::class$view->display_handler);
    $this->assertInstanceOf(DefaultDisplay::class$view->displayHandlers->get('default'));

    $view->destroy();
    $view->initHandlers();

    // Check for all handler types.     $handler_types = array_keys(ViewExecutable::getHandlerTypes());
    foreach ($handler_types as $type) {
      // The views_test integration doesn't have relationships.
/** * {@inheritdoc} */
  public function calculateDependencies() {
    parent::calculateDependencies();

    // Ensure that the view is dependant on the module that implements the view.     $this->addDependency('module', $this->module);

    $executable = $this->getExecutable();
    $executable->initDisplay();
    $executable->initStyle();

    foreach ($executable->displayHandlers as $display) {
      // Calculate the dependencies each display has.       $this->calculatePluginDependencies($display);
    }

    return $this;
  }

  /** * {@inheritdoc} */
// Save the attachments and test the value on the view.     $this->drupalGet($attachment_display_url);
    $this->submitForm(['displays[page_1]' => 1], 'Apply');
    // Options summary should be escaped.     $this->assertSession()->assertEscaped('<em>Page</em>');
    $this->assertSession()->responseNotContains('<em>Page</em>');
    $this->assertSession()->elementAttributeContains('xpath', '//a[@id = "views-attachment-1-displays"]', 'title', 'Page');
    $this->submitForm([], 'Save');

    $view = Views::getView('test_attachment_ui');
    $view->initDisplay();
    $this->assertEquals(['page_1']array_keys(array_filter($view->displayHandlers->get('attachment_1')->getOption('displays'))), 'The attached displays got saved as expected');

    $this->drupalGet($attachment_display_url);
    $this->submitForm([
      'displays[default]' => 1,
      'displays[page_1]' => 1,
    ], 'Apply');
    $this->assertSession()->elementAttributeContains('xpath', '//a[@id = "views-attachment-1-displays"]', 'title', 'Multiple displays');
    $this->submitForm([], 'Save');

    $view = Views::getView('test_attachment_ui');
    
/** * {@inheritdoc} */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $links = [];
    $views = Views::getApplicableViews('uses_menu_links');

    foreach ($views as $data) {
      [$view_id$display_id] = $data;
      /** @var \Drupal\views\ViewExecutable $executable */
      $executable = $this->viewStorage->load($view_id)->getExecutable();
      $executable->initDisplay();
      $display = $executable->displayHandlers->get($display_id);

      if (($display instanceof DisplayMenuInterface) && ($result = $display->getMenuLinks())) {
        foreach ($result as $link_id => $link) {
          $links[$link_id] = $link + $base_plugin_definition;
        }
      }
    }

    return $links;
  }

}

  public function testCollectRoutes() {
    [$view] = $this->setupViewExecutableAccessPlugin();

    $display = [];
    $display['display_plugin'] = 'page';
    $display['id'] = 'page_1';
    $display['display_options'] = [
      'path' => 'test_route',
    ];
    $this->pathPlugin->initDisplay($view$display);

    $collection = new RouteCollection();
    $result = $this->pathPlugin->collectRoutes($collection);
    $this->assertEquals(['test_id.page_1' => 'view.test_id.page_1']$result);

    $route = $collection->get('view.test_id.page_1');
    $this->assertInstanceOf(Route::class$route);
    $this->assertEquals('test_id', $route->getDefault('view_id'));
    $this->assertEquals('page_1', $route->getDefault('display_id'));
    $this->assertFalse($route->getOption('returns_response'));
    $this->assertEquals('Drupal\views\Routing\ViewPageController::getTitle', $route->getDefault('_title_callback'));
  }
$form_state->setErrorByName('', $error);
      }
    }
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    $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');
            
Home | Imprint | This part of the site doesn't use cookies.