You are here

function Node::validate_argument in Views (for Drupal 7) 8.3

Overrides ArgumentValidatorPluginBase::validate_argument

File

lib/Views/node/Plugin/views/argument_validator/Node.php, line 83
Definition of Views\node\Plugin\views\argument_validator\Node.

Class

Node
Validate whether an argument is an acceptable node.

Namespace

Views\node\Plugin\views\argument_validator

Code

function validate_argument($argument) {
  $types = $this->options['types'];
  switch ($this->options['nid_type']) {
    case 'nid':
      if (!is_numeric($argument)) {
        return FALSE;
      }
      $node = node_load($argument);
      if (!$node) {
        return FALSE;
      }
      if (!empty($this->options['access'])) {
        if (!node_access($this->options['access_op'], $node)) {
          return FALSE;
        }
      }

      // Save the title() handlers some work.
      $this->argument->validated_title = check_plain($node
        ->label());
      if (empty($types)) {
        return TRUE;
      }
      return isset($types[$node->type]);
    case 'nids':
      $nids = new stdClass();
      $nids->value = array(
        $argument,
      );
      $nids = $this
        ->breakPhrase($argument, $nids);
      if ($nids->value == array(
        -1,
      )) {
        return FALSE;
      }
      $test = drupal_map_assoc($nids->value);
      $titles = array();
      $nodes = node_load_multiple($nids->value);
      foreach ($nodes as $node) {
        if ($types && empty($types[$node->type])) {
          return FALSE;
        }
        if (!empty($this->options['access'])) {
          if (!node_access($this->options['access_op'], $node)) {
            return FALSE;
          }
        }
        $titles[] = check_plain($node
          ->label());
        unset($test[$node->nid]);
      }
      $this->argument->validated_title = implode($nids->operator == 'or' ? ' + ' : ', ', $titles);

      // If this is not empty, we did not find a nid.
      return empty($test);
  }
}