You are here

public function DomainPathAliasManager::getPathByAlias in Domain Path 8

Given the alias, return the path it represents.

Parameters

string $alias: An alias.

string $langcode: An optional language code to look up the path in.

Return value

string The path represented by alias, or the alias if no path was found.

Throws

\InvalidArgumentException Thrown when the path does not start with a slash.

Overrides AliasManager::getPathByAlias

File

src/DomainPathAliasManager.php, line 33

Class

DomainPathAliasManager

Namespace

Drupal\domain_path

Code

public function getPathByAlias($alias, $langcode = NULL) {
  $config = \Drupal::config('domain_path.settings');
  $this->method = $config
    ->get('language_method') ? $config
    ->get('language_method') : LanguageInterface::TYPE_CONTENT;
  $active = \Drupal::service('domain.negotiator')
    ->getActiveDomain();
  if ($active === NULL) {
    $active = \Drupal::service('domain.negotiator')
      ->getActiveDomain(TRUE);
  }
  if ($active) {
    $properties = [
      'alias' => $alias,
      'domain_id' => \Drupal::service('domain.negotiator')
        ->getActiveDomain()
        ->id(),
    ];

    //https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php#L36

    //didn't pass the $langcode.
    $langcode = $langcode ?: $this->languageManager
      ->getCurrentLanguage($this->method)
      ->getId();
    if ($langcode != NULL) {
      $properties['language'] = $langcode;
    }
    else {

      //TODO: zxx -> Not applicable
      $properties['language'] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    }
    $domain_paths = \Drupal::entityTypeManager()
      ->getStorage('domain_path')
      ->loadByProperties($properties);
    $this->domainPath = reset($domain_paths);
    if ($this->domainPath) {
      return $this->domainPath
        ->getSource();
    }
  }
  return parent::getPathByAlias($alias, $langcode);
}