You are here

public function DisplayPluginBase::validate in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/display/DisplayPluginBase.php \Drupal\views\Plugin\views\display\DisplayPluginBase::validate()

Validate that the plugin is correct and can be saved.

Return value

An array of error strings to tell the user what is wrong with this plugin.

Overrides PluginBase::validate

3 calls to DisplayPluginBase::validate()
DisplayTest::validate in core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php
Validate that the plugin is correct and can be saved.
EntityReference::validate in core/modules/views/src/Plugin/views/display/EntityReference.php
Validate that the plugin is correct and can be saved.
PathPluginBase::validate in core/modules/views/src/Plugin/views/display/PathPluginBase.php
Validate that the plugin is correct and can be saved.
3 methods override DisplayPluginBase::validate()
DisplayTest::validate in core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php
Validate that the plugin is correct and can be saved.
EntityReference::validate in core/modules/views/src/Plugin/views/display/EntityReference.php
Validate that the plugin is correct and can be saved.
PathPluginBase::validate in core/modules/views/src/Plugin/views/display/PathPluginBase.php
Validate that the plugin is correct and can be saved.

File

core/modules/views/src/Plugin/views/display/DisplayPluginBase.php, line 2483

Class

DisplayPluginBase
Base class for views display plugins.

Namespace

Drupal\views\Plugin\views\display

Code

public function validate() {
  $errors = [];

  // Make sure displays that use fields HAVE fields.
  if ($this
    ->usesFields()) {
    $fields = FALSE;
    foreach ($this
      ->getHandlers('field') as $field) {
      if (empty($field->options['exclude'])) {
        $fields = TRUE;
      }
    }
    if (!$fields) {
      $errors[] = $this
        ->t('Display "@display" uses fields but there are none defined for it or all are excluded.', [
        '@display' => $this->display['display_title'],
      ]);
    }
  }

  // Validate the more link.
  if ($this
    ->isMoreEnabled() && $this
    ->getOption('link_display') !== 'custom_url') {
    $routed_display = $this
      ->getRoutedDisplay();
    if (!$routed_display || !$routed_display
      ->isEnabled()) {
      $errors[] = $this
        ->t('Display "@display" uses a "more" link but there are no displays it can link to. You need to specify a custom URL.', [
        '@display' => $this->display['display_title'],
      ]);
    }
  }
  if ($this
    ->hasPath() && !$this
    ->getOption('path')) {
    $errors[] = $this
      ->t('Display "@display" uses a path but the path is undefined.', [
      '@display' => $this->display['display_title'],
    ]);
  }

  // Validate style plugin.
  $style = $this
    ->getPlugin('style');
  if (empty($style)) {
    $errors[] = $this
      ->t('Display "@display" has an invalid style plugin.', [
      '@display' => $this->display['display_title'],
    ]);
  }
  else {
    $result = $style
      ->validate();
    if (!empty($result) && is_array($result)) {
      $errors = array_merge($errors, $result);
    }
  }

  // Validate query plugin.
  $query = $this
    ->getPlugin('query');
  $result = $query
    ->validate();
  if (!empty($result) && is_array($result)) {
    $errors = array_merge($errors, $result);
  }

  // Check for missing relationships.
  $relationships = array_keys($this
    ->getHandlers('relationship'));
  foreach (ViewExecutable::getHandlerTypes() as $type => $handler_type_info) {
    foreach ($this
      ->getHandlers($type) as $handler_id => $handler) {
      if (!empty($handler->options['relationship']) && $handler->options['relationship'] != 'none' && !in_array($handler->options['relationship'], $relationships)) {
        $errors[] = $this
          ->t('The %handler_type %handler uses a relationship that has been removed.', [
          '%handler_type' => $handler_type_info['lstitle'],
          '%handler' => $handler
            ->adminLabel(),
        ]);
      }
    }
  }

  // Validate handlers.
  foreach (ViewExecutable::getHandlerTypes() as $type => $info) {
    foreach ($this
      ->getHandlers($type) as $handler) {
      $result = $handler
        ->validate();
      if (!empty($result) && is_array($result)) {
        $errors = array_merge($errors, $result);
      }
    }
  }

  // Validate extenders.
  foreach ($this->extenders as $extender) {
    $result = $extender
      ->validate();
    if (!empty($result) && is_array($result)) {
      $errors = array_merge($errors, $result);
    }
  }
  return $errors;
}