You are here

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

Return the default argument.

This needs to be overridden by every default argument handler to properly do what is needed.

Overrides ArgumentDefaultPluginBase::getArgument

File

src/Plugin/views/argument_default/UrlPath.php, line 119

Class

UrlPath
Convert an entity id to its url path.

Namespace

Drupal\views_url_path_arguments\Plugin\views\argument_default

Code

public function getArgument() {
  $parameter = $this->routeMatch
    ->getRawParameters()
    ->all();
  $lastSegment = array_pop($parameter);

  // Is it already the entity id?
  if (ctype_digit($lastSegment)) {
    return $lastSegment;
  }
  $alias = '/';
  if ($this->options['provide_static_segments']) {
    $alias .= $this->options['segments'] . '/';
  }
  $alias .= $lastSegment;
  $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);
  }
  return substr($canonicalPath, strrpos($canonicalPath, '/') + 1);
}