overrideOption example

'entity_id' => $host->id(),
      'comment_type' => 'entity_test',
      'field_name' => $field_storage_comment->getName(),
      'status' => 1,
      'pid' => $comment2->id(),
    ]);
    $comment3->save();

    $view = Views::getView('test_comment');
    $view->setDisplay();

    $view->displayHandlers->get('default')->overrideOption('fields', [
      'thread' => [
        'table' => 'comment_field_data',
        'field' => 'thread',
        'id' => 'thread',
        'plugin_id' => 'comment_depth',
        'entity_type' => 'comment',
      ],
    ]);
    $view->save();

    $view->preview();

    

  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,
      ],
    ]);

    $this->executeView($view);

    

  }

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

    // Add a filter.     $view->displayHandlers->get('default')->overrideOption('filters', [
      'age' => [
        'operator' => '<',
        'value' => [
          'value' => '28',
          'min' => '',
          'max' => '',
        ],
        'group' => '0',
        'exposed' => FALSE,
        'expose' => [
          'operator' => FALSE,
          
return $data;
  }

  /** * Return a basic view with random ordering. */
  protected function getBasicRandomView() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    // Add a random ordering.     $view->displayHandlers->get('default')->overrideOption('sorts', [
      'random' => [
        'id' => 'random',
        'field' => 'random',
        'table' => 'views',
      ],
    ]);

    return $view;
  }

  /** * Tests random ordering of the result set. * * @see DatabaseSelectTestCase::testRandomOrder() */
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);

    // Test with the formatted option.     $this->assertEquals('', $view->field['age']->advancedRender($view->result[0]));
    
$this->helperButtonHasLabel('edit-submit-test-exposed-form-buttons', 'Apply');
  }

  /** * Tests the exposed form with a non-standard identifier. */
  public function testExposedIdentifier() {
    // Alter the identifier of the filter to a random string.     $view = Views::getView('test_exposed_form_buttons');
    $view->setDisplay();
    $identifier = 'new_identifier';
    $view->displayHandlers->get('default')->overrideOption('filters', [
      'type' => [
        'exposed' => TRUE,
        'field' => 'type',
        'id' => 'type',
        'table' => 'node_field_data',
        'plugin_id' => 'in_operator',
        'entity_type' => 'node',
        'entity_field' => 'type',
        'expose' => [
          'identifier' => $identifier,
          'label' => 'Content: Type',
          
$view->display_handler->setOption('style', $style);
    $view->initDisplay();
    $view->initStyle();
    $view->style_plugin->setUsesRowPlugin(FALSE);

    $output = $view->preview();
    $output = $renderer->renderRoot($output);

    $this->assertStringContainsString('<h1></h1>', $output, 'An empty value for test_option found in output.');

    // Change this option and check the title of out output.     $view->display_handler->overrideOption('test_option', 'Test option title');
    $view->save();

    $output = $view->preview();
    $output = $renderer->renderRoot($output);

    // Test we have our custom <h1> tag in the output of the view.     $this->assertStringContainsString('<h1>Test option title</h1>', $output, 'The test_option value found in display output title.');

    // Test that the display category/summary is in the UI.     $this->drupalGet('admin/structure/views/view/test_view/edit/display_test_1');
    $this->assertSession()->pageTextContains('Display test settings');
    
/** * Tests numeric ordering of the result set. */
  public function testDateOrdering() {
    foreach (['second', 'minute', 'hour', 'day', 'month', 'year'] as $granularity) {
      foreach ([FALSE, TRUE] as $reverse) {
        $view = Views::getView('test_view');
        $view->setDisplay();

        // Change the fields.         $view->displayHandlers->get('default')->overrideOption('fields', [
          'name' => [
            'id' => 'name',
            'table' => 'views_test_data',
            'field' => 'name',
            'relationship' => 'none',
          ],
          'created' => [
            'id' => 'created',
            'table' => 'views_test_data',
            'field' => 'created',
            'relationship' => 'none',
          ],
$this->enableViewsTestModule();
  }

  /** * Tests the output caching on an actual page. */
  public function testCacheOutputOnPage() {
    $view = Views::getView('test_display');
    $view->storage->setStatus(TRUE);
    $view->setDisplay('page_1');
    $view->display_handler->overrideOption('cache', [
      'type' => 'time',
      'options' => [
        'results_lifespan' => '3600',
        'output_lifespan' => '3600',
      ],
    ]);
    $view->save();
    $this->container->get('router.builder')->rebuildIfNeeded();

    /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
    $render_cache = \Drupal::service('render_cache');
    
/** * Tests the Numeric handler with different settings. * * @dataProvider providerTestFieldNumeric */
  public function testFieldNumeric($field_settings$values$expected_values) {
    $view = Views::getView('test_view');
    $view->setDisplay();

    if (!empty($field_settings)) {
      $view->displayHandlers->get('default')->overrideOption('fields', ['age' => $field_settings]);
    }
    $this->executeView($view);

    foreach ($values as $key => $value) {
      $view->result[0]->views_test_data_age = $value;
      $this->assertSame($expected_values[$key]$view->field['age']->advancedRender($view->result[0]));
    }
  }

  /** * Data provider for testFieldNumeric. * * @return array * The data set containing field settings, values to set and expected * values. */
// Ensure the RSS version is 2.0.     $rss_array = $this->getSession()->getDriver()->find('rss');
    $this->assertEquals('2.0', reset($rss_array)->getAttribute('version'));

    // Check that the "Exception value" is disabled by default.     $this->drupalGet('taxonomy/term/all/feed');
    $this->assertSession()->statusCodeEquals(404);
    // Set the exception value to 'all'.     $view = Views::getView('taxonomy_term');
    $arguments = $view->getDisplay()->getOption('arguments');
    $arguments['tid']['exception']['value'] = 'all';
    $view->getDisplay()->overrideOption('arguments', $arguments);
    $view->storage->save();
    // Check the article is shown in the feed.     $raw_xml = '<title>' . $node->label() . '</title>';
    $this->drupalGet('taxonomy/term/all/feed');
    $this->assertSession()->responseContains($raw_xml);
    // Unpublish the article and check that it is not shown in the feed.     $node->setUnpublished()->save();
    $this->drupalGet('taxonomy/term/all/feed');
    $this->assertSession()->responseNotContains($raw_xml);
  }

}
/** * Tests ViewsBlock::build() with a title override. * * @see \Drupal\views\Plugin\Block::build() */
  public function testBuildWithTitleOverride() {
    $view = Views::getView('test_view_block');
    $view->setDisplay();

    // Add a fixed argument that sets a title and save the view.     $view->displayHandlers->get('default')->overrideOption('arguments', [
      'name' => [
        'default_action' => 'default',
        'title_enable' => TRUE,
        'title' => 'Overridden title',
        'default_argument_type' => 'fixed',
        'default_argument_options' => [
          'argument' => 'fixed',
        ],
        'validate' => [
          'type' => 'none',
          'fail' => 'not found',
        ],
$this->assertCount(0, $handlers);

    $output = $view->preview();
    $output = \Drupal::service('renderer')->renderRoot($output);
    // The area output should not be present since access was denied.     $this->assertStringNotContainsString('a custom string', $output);
    $view->destroy();

    // Test with access granted for the area handler.     $view = Views::getView('test_example_area_access');
    $view->initDisplay();
    $view->display_handler->overrideOption('empty', [
      'test_example' => [
        'field' => 'test_example',
        'id' => 'test_example',
        'table' => 'views',
        'plugin_id' => 'test_example',
        'string' => 'a custom string',
        'custom_access' => TRUE,
      ],
    ]);
    $view->initHandlers();
    $handlers = $view->display_handler->getHandlers('empty');

    
$this->assertFalse($render['#embed']);
    $this->setRawContent($renderer->renderRoot($render));

    $xpath = $this->xpath('//div[@class="views-element-container"]');
    $this->assertNotEmpty($xpath, 'The view container has been found in the rendered output.');

    // There should be 5 rows in the results.     $xpath = $this->xpath('//div[@class="views-row"]');
    $this->assertCount(5, $xpath);

    // Add an argument and save the view.     $view->displayHandlers->get('default')->overrideOption('arguments', [
      'age' => [
        'default_action' => 'ignore',
        'title' => '',
        'default_argument_type' => 'fixed',
        'validate' => [
          'type' => 'none',
          'fail' => 'not found',
        ],
        'break_phrase' => FALSE,
        'not' => FALSE,
        'id' => 'age',
        

  public static $testViews = ['test_view'];

  /** * Tests the language argument. */
  public function testArgument() {
    $view = Views::getView('test_view');
    foreach (['en' => 'John', 'xx-lolspeak' => 'George'] as $langcode => $name) {
      $view->setDisplay();
      $view->displayHandlers->get('default')->overrideOption('arguments', [
        'langcode' => [
          'id' => 'langcode',
          'table' => 'views_test_data',
          'field' => 'langcode',
        ],
      ]);
      $this->executeView($view[$langcode]);

      $expected = [
        ['name' => $name],
      ];
      
Home | Imprint | This part of the site doesn't use cookies.