initStyle example

// 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);

    $random_text = $this->randomMachineName();
    $view->rowPlugin->setOutput($random_text);

    $output = $view->preview();
    $output = $renderer->renderRoot($output);
    $this->assertStringContainsString($random_text(string) $output);

    
$view->setDisplay('display_test_1');

    $this->assertInstanceOf(DisplayTestPlugin::class$view->display_handler);

    // Check the test option.     $this->assertSame('', $view->display_handler->getOption('test_option'));

    $style = $view->display_handler->getOption('style');
    $style['type'] = 'test_style';
    $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();

    

    $option_build['#attributes']['class'][] = Html::cleanCssIdentifier($display_id . '-' . $id);
    return $option_build;
  }

  /** * Add information about a section to a display. */
  public function getFormBucket(ViewUI $view$type$display) {
    $executable = $view->getExecutable();
    $executable->setDisplay($display['id']);
    $executable->initStyle();

    $types = $executable->getHandlerTypes();

    $build = [
      '#theme_wrappers' => ['views_ui_display_tab_bucket'],
    ];

    $build['#overridden'] = FALSE;
    $build['#defaulted'] = FALSE;

    $build['#name'] = $type;
    
return $display;
  }

  /** * Gets the current style plugin. * * @return \Drupal\views\Plugin\views\style\StylePluginBase * The current style plugin. */
  public function getStyle() {
    if (!isset($this->style_plugin)) {
      $this->initStyle();
    }

    return $this->style_plugin;
  }

  /** * Finds and initializes the style plugin. * * Note that arguments may have changed which style plugin we use, so * check the view object first, then ask the display handler. * * @return bool * TRUE if the style plugin was or could be initialized, FALSE otherwise. */
public $query;

  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['fields'] = ['default' => []];

    return $options;
  }

  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form$form_state);
    $this->view->initStyle();

    // Allow to choose all fields as possible     if ($this->view->style_plugin->usesFields()) {
      $options = [];
      foreach ($this->view->display_handler->getHandlers('field') as $name => $field) {
        // Only allow clickSortable fields. Fields without clickSorting will         // probably break in the Combine filter.         if ($field->clickSortable()) {
          $options[$name] = $field->adminLabel(TRUE);
        }
      }
      
    $row = $view->display_handler->getOption('row');
    $row['options'] = [
      'inline' => [
        'age' => 'age',
        'id' => 'id',
        'name' => 'name',
      ],
      'separator' => '<br />',
    ];
    $view->display_handler->setOption('row', $row);
    $view->initDisplay();
    $view->initStyle();
    $output = $view->preview();
    $output = $renderer->renderRoot($output);
    $this->assertStringContainsString('<div class="views-row"><span class="views-field views-field-age"><span class="field-content">25</span></span><br /><span class="views-field views-field-id"><span class="field-content">1</span></span><br /><span class="views-field views-field-name"><span class="field-content">John</span></span></div>', (string) $output);
    $view->destroy();

    // Check that unsafe separators are stripped.     $view->setDisplay();
    $row = $view->display_handler->getOption('row');
    $row['options'] = [
      'inline' => [
        'age' => 'age',
        

  public function testMessages() {

    // Remove the watchdog entries added by the potential batch process.     $this->container->get('database')->truncate('watchdog')->execute();

    $entries = $this->createLogEntries();

    $view = Views::getView('test_dblog');
    $this->executeView($view);
    $view->initStyle();

    foreach ($entries as $index => $entry) {
      if (!isset($entry['variables'])) {
        continue;
      }
      $message_vars = $entry['variables'];
      unset($message_vars['link']);
      $this->assertEquals(new FormattableMarkup($entry['message']$message_vars)$view->style_plugin->getField($index, 'message'));
      $link_field = $view->style_plugin->getField($index, 'link');
      // The 3rd entry contains some unsafe markup that needs to get filtered.       if ($index == 2) {
        
/** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');
    $view = Views::getView('test_node_path_plugin');

    // The configured deprecated node path plugin should be converted to the     // entity link plugin.     $field = $view->getHandler('page_1', 'field', 'path');
    $this->assertEquals('entity_link', $field['plugin_id']);

    $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->assertStringContainsString('This is <strong>not escaped</strong> and this is ' . $node->toLink('the link')->toString()$output, 'Make sure path field rewriting is not escaped.');
    }
  }

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

    foreach ($themes as $theme) {
      \Drupal::service('theme_installer')->install([$theme]);
      \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme($theme));
      foreach ($styles as $type => $xpath) {
        $view = Views::getView('test_page_display');
        $view->storage->invalidateCaches();
        $view->initDisplay();
        $view->setDisplay('empty_row');
        $view->displayHandlers->get('empty_row')->default_display->options['style']['type'] = $type;
        $view->initStyle();
        $this->executeView($view);
        $output = $view->preview();
        $output = \Drupal::service('renderer')->renderRoot($output);
        $this->setRawContent($output);
        $this->assertCount(5, $this->xpath("{$xpath}[not(text()) and not(node())]"), "Empty rows in theme '$theme', type '$type'.");
      }
    }
  }

}

  public static $testViews = ['test_table'];

  /** * Tests the table style. */
  public function testTable() {
    $view = Views::getView('test_table');

    $view->setDisplay('default');
    $view->initStyle();
    $view->initHandlers();
    $view->initQuery();
    $style_plugin = $view->style_plugin;

    // Test the buildSort() method.     $request = new Request();
    $view->setRequest($request);

    $style_plugin->options['override'] = FALSE;

    $style_plugin->options['default'] = '';
    

  public function testResponsiveGrid(array $options, array $expected): void {
    // Create and preview a View with the provided options.     $view = Views::getView('test_grid_responsive');
    $view->setDisplay('default');
    $view->initStyle();
    $view->initHandlers();
    $view->initQuery();
    $view->style_plugin->options = $options + $view->style_plugin->options;
    $this->executeView($view);
    $output = $view->preview();
    $output = \Drupal::service('renderer')->renderRoot($output);
    $this->setRawContent($output);

    // Confirm that the alignment class is added.     $result = $this->xpath('//div[contains(@class, "views-view-responsive-grid") and contains(@class, :alignment)]', [':alignment' => 'views-view-responsive-grid--' . $expected['alignment']]);
    $this->assertGreaterThan(0, count($result), "Alignment CSS variable value is detected and correct.");

    
/** * {@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} */


  /** * 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.');
    }

    // Test with teasers.
$this->assertGreaterThan(0, count($view->$type)new FormattableMarkup('Make sure a %type instance got instantiated.', ['%type' => $type]));
    }

    // initHandlers() should create display handlers automatically as well.     $this->assertInstanceOf(DefaultDisplay::class$view->display_handler);
    $this->assertInstanceOf(DefaultDisplay::class$view->displayHandlers->get('default'));

    $view_hash = spl_object_hash($view);
    $display_hash = spl_object_hash($view->display_handler);

    // Test the initStyle() method.     $view->initStyle();
    $this->assertInstanceOf(DefaultStyle::class$view->style_plugin);
    // Test the plugin has been invited and view have references to the view and     // display handler.     $this->assertEquals($view_hashspl_object_hash($view->style_plugin->view));
    $this->assertEquals($display_hashspl_object_hash($view->style_plugin->displayHandler));

    // Test the initQuery method().     $view->initQuery();
    $this->assertInstanceOf(Sql::class$view->query);
    $this->assertEquals($view_hashspl_object_hash($view->query->view));
    $this->assertEquals($display_hashspl_object_hash($view->query->displayHandler));

    

  public static $testViews = ['test_field_permission'];

  /** * Tests the permission field handler output. */
  public function testFieldPermission() {
    $this->setupPermissionTestData();

    $view = Views::getView('test_field_permission');
    $this->executeView($view);
    $view->initStyle();
    $view->render();
    $style_plugin = $view->style_plugin;

    $expected_permissions = [];
    $expected_permissions[$this->users[0]->id()] = [];
    $expected_permissions[$this->users[1]->id()] = [];
    $expected_permissions[$this->users[2]->id()][] = 'Administer roles and permissions';
    // View user profiles comes first, because we sort by the permission     // machine name.     $expected_permissions[$this->users[3]->id()][] = 'View user information';
    $expected_permissions[$this->users[3]->id()][] = 'Administer roles and permissions';
    
Home | Imprint | This part of the site doesn't use cookies.