You are here

public function PathAliasXtProcessorAlias::processInbound in Extended Path Aliases 8

Processes the inbound path.

Implementations may make changes to the request object passed in but should avoid all other side effects. This method can be called to process requests other than the current request.

Parameters

string $path: The path to process, with a leading slash.

\Symfony\Component\HttpFoundation\Request $request: The HttpRequest object representing the request to process. Note, if this method is being called via the path_processor_manager service and is not part of routing, the current request object must be cloned before being passed in.

Return value

string The processed path.

Overrides PathProcessorAlias::processInbound

File

src/PathAliasXtProcessorAlias.php, line 62

Class

PathAliasXtProcessorAlias
Processes inbound and outbound path determining alias.

Namespace

Drupal\path_alias_xt

Code

public function processInbound($path, Request $request) {
  $path_to_process = ltrim($path, '/');

  // If Redirect module exists we must check for an active redirect.
  if ($this->moduleHandler
    ->moduleExists('redirect')) {
    $query = $this->entityTypeManager
      ->getStorage('redirect')
      ->getQuery();
    $query
      ->condition('redirect_source__path', $path_to_process);
    if ($query
      ->execute()) {
      return $path;
    }
  }
  $removed_elements = [];
  $path_elements = explode('/', $path_to_process);
  foreach ($path_elements as $element) {
    $candidate_alias = '/' . implode('/', $path_elements);
    $source = $this->aliasManager
      ->getPathByAlias($candidate_alias);
    if ($source != $candidate_alias) {

      // Change the order of the elements.
      krsort($removed_elements);
      $return_path = $source;
      if (!empty($removed_elements)) {
        $return_path .= '/' . implode('/', $removed_elements);
      }

      // Validate the path.
      // Injecting the service threw ServiceCircularReferenceException.
      if (\Drupal::service('path.validator')
        ->getUrlIfValidWithoutAccessCheck($return_path)) {
        return $return_path;
      }
    }

    // Remove the last element from the elements array to be able to add it
    // to the end of the found path.
    $removed_elements[] = array_pop($path_elements);
  }
  return $path;
}