You are here

class DisplayTest in Drupal 10

Same name in this branch
  1. 10 core/modules/views_ui/tests/src/Functional/DisplayTest.php \Drupal\Tests\views_ui\Functional\DisplayTest
  2. 10 core/modules/views_ui/tests/src/FunctionalJavascript/DisplayTest.php \Drupal\Tests\views_ui\FunctionalJavascript\DisplayTest
  3. 10 core/modules/views/tests/src/Functional/Plugin/DisplayTest.php \Drupal\Tests\views\Functional\Plugin\DisplayTest
  4. 10 core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php \Drupal\views_test_data\Plugin\views\display\DisplayTest
Same name and namespace in other branches
  1. 8 core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php \Drupal\views_test_data\Plugin\views\display\DisplayTest
  2. 9 core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php \Drupal\views_test_data\Plugin\views\display\DisplayTest

Defines a Display test plugin.

Plugin annotation


@ViewsDisplay(
  id = "display_test",
  title = @Translation("Display test"),
  help = @Translation("Defines a display test plugin."),
  theme = "views_view",
  register_theme = FALSE,
  contextual_links_locations = {"view"}
)

Hierarchy

  • class \Drupal\views_test_data\Plugin\views\display\DisplayTest extends \Drupal\views\Plugin\views\display\DisplayPluginBase

Expanded class hierarchy of DisplayTest

2 files declare their use of DisplayTest
DisplayTest.php in core/modules/views/tests/src/Functional/Plugin/DisplayTest.php
ViewExecutableTest.php in core/modules/views/tests/src/Kernel/ViewExecutableTest.php
1 string reference to 'DisplayTest'
DisplayTest::calculateDependencies in core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php

File

core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php, line 21

Namespace

Drupal\views_test_data\Plugin\views\display
View source
class DisplayTest extends DisplayPluginBase {

  /**
   * Whether the display allows attachments.
   *
   * @var bool
   */
  protected $usesAttachments = TRUE;

  /**
   * {@inheritdoc}
   */
  public function getType() {
    return 'test';
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['test_option'] = [
      'default' => '',
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function optionsSummary(&$categories, &$options) {
    parent::optionsSummary($categories, $options);
    $categories['display_test'] = [
      'title' => $this
        ->t('Display test settings'),
      'column' => 'second',
      'build' => [
        '#weight' => -100,
      ],
    ];
    $test_option = $this
      ->getOption('test_option') ?: $this
      ->t('Empty');
    $options['test_option'] = [
      'category' => 'display_test',
      'title' => $this
        ->t('Test option'),
      'value' => views_ui_truncate($test_option, 24),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    switch ($form_state
      ->get('section')) {
      case 'test_option':
        $form['#title'] .= $this
          ->t('Test option');
        $form['test_option'] = [
          '#title' => $this
            ->t('Test option'),
          '#type' => 'textfield',
          '#description' => $this
            ->t('This is a textfield for test_option.'),
          '#default_value' => $this
            ->getOption('test_option'),
        ];
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    parent::validateOptionsForm($form, $form_state);
    \Drupal::logger('views')
      ->notice($form_state
      ->getValue('test_option'));
    switch ($form_state
      ->get('section')) {
      case 'test_option':
        if (!trim($form_state
          ->getValue('test_option'))) {
          $form_state
            ->setError($form['test_option'], $this
            ->t('You cannot have an empty option.'));
        }
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitOptionsForm(&$form, FormStateInterface $form_state) {
    parent::submitOptionsForm($form, $form_state);
    switch ($form_state
      ->get('section')) {
      case 'test_option':
        $this
          ->setOption('test_option', $form_state
          ->getValue('test_option'));
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function execute() {
    $this->view
      ->build();
    $render = $this->view
      ->render();

    // Render the test option as the title before the view output.
    $render['#prefix'] = '<h1>' . Xss::filterAdmin($this->options['test_option']) . '</h1>';
    return $render;
  }

  /**
   * {@inheritdoc}
   */
  public function preview() {
    return $this
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return parent::calculateDependencies() + [
      'content' => [
        'DisplayTest',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function validate() {
    $errors = parent::validate();
    foreach ($this->view->displayHandlers as $display_handler) {
      $errors[] = 'error';
    }
    return $errors;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DisplayTest::$usesAttachments protected property Whether the display allows attachments.
DisplayTest::buildOptionsForm public function
DisplayTest::calculateDependencies public function
DisplayTest::defineOptions protected function
DisplayTest::execute public function
DisplayTest::getType public function
DisplayTest::optionsSummary public function
DisplayTest::preview public function
DisplayTest::submitOptionsForm public function
DisplayTest::validate public function
DisplayTest::validateOptionsForm public function