setDisplay example


  public function loadView() {
    if (empty($this->view)) {
      $metadata = $this->getMetaData();
      $view_id = $metadata['view_id'];
      $display_id = $metadata['display_id'];
      $view_entity = $this->entityTypeManager->getStorage('view')->load($view_id);
      $view = $this->viewExecutableFactory->get($view_entity);
      $view->setDisplay($display_id);
      $view->initDisplay();
      $this->view = $view;
    }
    return $this->view;
  }

  /** * {@inheritdoc} */
  public function getTitle() {
    // @todo Get the translated value from the config without instantiating the


  public function viewsData() {
    $data = parent::viewsData();
    $data['views_test_data']['age']['field']['id'] = 'file_size';

    return $data;
  }

  public function testFieldFileSize() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    $view->displayHandlers->get('default')->overrideOption('fields', [
      'age' => [
        'id' => 'age',
        'table' => 'views_test_data',
        'field' => 'age',
      ],
    ]);

    $this->executeView($view);

    

  public static $testViews = ['test_view'];

  /** * Tests the general rendering of styles. */
  public function testStyle() {
    $renderer = $this->container->get('renderer');

    // This run use the test row plugin and render with it.     $view = Views::getView('test_view');
    $view->setDisplay();
    $style = $view->display_handler->getOption('style');
    $style['type'] = 'test_style';
    $view->display_handler->setOption('style', $style);
    $row = $view->display_handler->getOption('row');
    $row['type'] = 'test_row';
    $view->display_handler->setOption('row', $row);
    $view->initDisplay();
    $view->initStyle();
    // Reinitialize the style as it supports row plugins now.     $view->style_plugin->init($view$view->display_handler);
    $this->assertInstanceOf(RowTest::class$view->rowPlugin);

    

  public static $testViews = ['test_view'];

  /** * Tests numeric ordering of the result set. */
  public function testNumericOrdering() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    // Change the ordering     $view->displayHandlers->get('default')->overrideOption('sorts', [
      'age' => [
        'order' => 'ASC',
        'id' => 'age',
        'table' => 'views_test_data',
        'field' => 'age',
        'relationship' => 'none',
      ],
    ]);

    
unset($display_duplicate['display_plugin']);
    unset($display_duplicate['new_id']);

    $displays[$new_display_id] = NestedArray::mergeDeep($displays[$new_display_id]$display_duplicate);
    $displays[$new_display_id]['id'] = $new_display_id;

    // First set the displays.     $this->set('display', $displays);

    // Ensure that we just copy display options, which are provided by the new     // display plugin.     $executable->setDisplay($new_display_id);

    $executable->display_handler->filterByDefinedOptions($displays[$new_display_id]['display_options']);
    // Update the display settings.     $this->set('display', $displays);

    return $new_display_id;
  }

  /** * {@inheritdoc} */
  
$this->installEntitySchema('user');
    $this->installConfig(['node', 'field']);

    ViewTestData::createTestViews(static::class['node_test_views']);
  }

  /** * Tests the nid argument. */
  public function testNidArgument() {
    $view = Views::getView('test_nid_argument');
    $view->setDisplay();

    $node1 = Node::create([
      'type' => 'default',
      'title' => $this->randomMachineName(),
    ]);
    $node1->save();
    $node2 = Node::create([
      'type' => 'default',
      'title' => $this->randomMachineName(),
    ]);
    $node2->save();

    
'delete any bar content',
    ]);
    // And switch to that user.     $this->container->get('account_switcher')->switchTo($this->testUser);
  }

  /** * Tests that dropbutton markup doesn't leak between rows. */
  public function testDropbuttonMarkupShouldNotLeakBetweenRows() {
    $view = Views::getView('test_dropbutton');
    $view->setDisplay();
    $view->preExecute([]);
    $view->execute();

    $renderer = $this->container->get('renderer');
    $dropbutton_output = [];

    // Render each row and field in turn - the dropbutton plugin relies on     // output being set in previous versions.     foreach ($view->result as $index => $row) {
      foreach (array_keys($view->field) as $field) {
        $output = $renderer->executeInRenderContext(new RenderContext()function D) use ($view$row$field) {
          
->save();
  }

  /** * Tests file extension views field handler extension_detect_tar option. */
  public function testFileExtensionTarOption() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    $view = Views::getView('file_extension_view');
    $view->setDisplay();
    $this->executeView($view);

    // Test without the tar option.     $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      $this->assertEquals('png', $view->field['extension']->advancedRender($view->result[0]));
      $this->assertEquals('tar', $view->field['extension']->advancedRender($view->result[1]));
      $this->assertEquals('gz', $view->field['extension']->advancedRender($view->result[2]));
      $this->assertEquals('', $view->field['extension']->advancedRender($view->result[3]));
    });

    // Test with the tar option.
$this->assertEquals('views_block__test_view_block_block_1', $views_block->getMachineNameSuggestion());
  }

  /** * Tests that ViewsBlock::build() produces the right output with title tokens. * * @see \Drupal\views\Plugin\Block::build() */
  public function testBuildWithTitleToken() {
    $view = Views::getView('test_view_block');
    $view->setDisplay();

    $sorts = [
      'name' => [
        'id' => 'name',
        'field' => 'name',
        'table' => 'views_test_data',
        'plugin_id' => 'standard',
        'order' => 'asc',
      ],
    ];
    // Set the title to the 'name' field in the first row and add a sort order

  protected function _testOffset() {
    $view = Views::getView('test_filter_datetime');
    $field = static::$fieldName . '_value';

    // Test simple operations.     $view->initHandlers();

    $view->filter[$field]->operator = '>';
    $view->filter[$field]->value['type'] = 'offset';
    $view->filter[$field]->value['value'] = '+1 hour';
    $view->setDisplay('default');
    $this->executeView($view);
    $expected_result = [
      ['nid' => $this->nodes[3]->id()],
    ];
    $this->assertIdenticalResultset($view$expected_result$this->map);
    $view->destroy();

    // Test offset for between operator.     $view->initHandlers();
    $view->filter[$field]->operator = 'between';
    $view->filter[$field]->value['type'] = 'offset';
    


  /** * Tests the selection handler. */
  public function testSelectionHandler() {
    // Tests the selection handler.     $this->assertResults($this->selectionHandler->getReferenceableEntities());

    // Add a relationship to the view.     $view = Views::getView('test_entity_reference');
    $view->setDisplay();
    $view->displayHandlers->get('default')->setOption('relationships', [
      'test_relationship' => [
        'id' => 'uid',
        'table' => 'node_field_data',
        'field' => 'uid',
      ],
    ]);

    // Add a filter depending on the relationship to the test view.     $view->displayHandlers->get('default')->setOption('filters', [
      'uid' => [
        
    ksort($this->commentsCreated, SORT_NUMERIC);
  }

  /** * Tests the block defined by the comments_recent view. */
  public function testBlockDisplay() {
    $user = $this->drupalCreateUser(['access comments']);
    $this->drupalLogin($user);

    $view = Views::getView('comments_recent');
    $view->setDisplay('block_1');
    $this->executeView($view);

    $map = [
      'subject' => 'subject',
      'cid' => 'cid',
      'comment_field_data_created' => 'created',
    ];
    $expected_result = [];
    foreach (array_values($this->commentsCreated) as $key => $comment) {
      $expected_result[$key]['subject'] = $comment->getSubject();
      $expected_result[$key]['cid'] = $comment->id();
      

  public function __construct(array $configuration$plugin_id$plugin_definition, ViewExecutableFactory $executable_factory, EntityStorageInterface $storage, AccountInterface $user) {
    $this->pluginId = $plugin_id;
    $delta = $this->getDerivativeId();
    [$name$this->displayID] = explode('-', $delta, 2);
    // Load the view.     $view = $storage->load($name);
    $this->view = $executable_factory->get($view);
    $this->displaySet = $this->view->setDisplay($this->displayID);
    $this->user = $user;

    parent::__construct($configuration$plugin_id$plugin_definition);
  }

  /** * {@inheritdoc} */
  public static function create(ContainerInterface $container, array $configuration$plugin_id$plugin_definition) {
    return new static(
      $configuration$plugin_id$plugin_definition,
      
/** * Executes a view. * * @param \Drupal\views\ViewExecutable $view * The view object. * @param array $args * (optional) An array of the view arguments to use for the view. */
  protected function executeView(ViewExecutable $view$args = []) {
    // A view does not really work outside of a request scope, due to many     // dependencies like the current user.     $view->setDisplay();
    $view->preExecute($args);
    $view->execute();
  }

  /** * Returns the schema definition. * * @internal */
  protected function schemaDefinition() {
    return ViewTestData::schemaDefinition();
  }
return $data;
  }

  public function viewsData() {
    $data = parent::viewsData();
    $data['views_test_data']['age']['field']['id'] = 'boolean';
    return $data;
  }

  public function testFieldBoolean() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    $view->displayHandlers->get('default')->overrideOption('fields', [
      'age' => [
        'id' => 'age',
        'table' => 'views_test_data',
        'field' => 'age',
        'relationship' => 'none',
      ],
    ]);

    $this->executeView($view);

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