You are here

public function UrlPath::validateArgument in Views URL Path Arguments 8

Performs validation for a given argument.

Overrides ArgumentValidatorPluginBase::validateArgument

File

src/Plugin/views/argument_validator/UrlPath.php, line 117

Class

UrlPath
Convert an entity id to its url path.

Namespace

Drupal\views_url_path_arguments\Plugin\views\argument_validator

Code

public function validateArgument($argument) {

  // Is it already the entity id?
  if (ctype_digit($argument)) {
    $this->argument->argument = $argument;
    return TRUE;
  }
  $alias = '/';
  if ($this->options['provide_static_segments']) {
    $alias .= $this->options['segments'] . '/';
  }
  $alias .= $argument;
  $langcode = $this->languageManager
    ->getCurrentLanguage(LanguageInterface::TYPE_URL)
    ->getId();
  $canonicalPath = '';
  if (\Drupal::hasService('path_alias.repository')) {
    if ($alias = \Drupal::service('path_alias.repository')
      ->lookupByAlias($alias, $langcode)) {
      $canonicalPath = $alias['path'];
    }
  }
  else {
    $canonicalPath = \Drupal::service('path.alias_storage')
      ->lookupPathSource($alias, $langcode);
  }
  $entity_id = substr($canonicalPath, strrpos($canonicalPath, '/') + 1);
  if (ctype_digit($entity_id)) {
    $this->argument->argument = $entity_id;
    return TRUE;
  }
  else {
    return FALSE;
  }
}