You are here

public function DynamicPathProcessor::processInbound in View Mode Page 3.2.x

Same name and namespace in other branches
  1. 8.3 src/PathProcessor/DynamicPathProcessor.php \Drupal\view_mode_page\PathProcessor\DynamicPathProcessor::processInbound()
  2. 4.0.x src/PathProcessor/DynamicPathProcessor.php \Drupal\view_mode_page\PathProcessor\DynamicPathProcessor::processInbound()

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 InboundPathProcessorInterface::processInbound

File

src/PathProcessor/DynamicPathProcessor.php, line 78

Class

DynamicPathProcessor
Class DynamicPathProcessor.

Namespace

Drupal\view_mode_page\PathProcessor

Code

public function processInbound($path, Request $request) {

  // DR - REMINDER: Empty cache after altering this code!
  $patterns = $this->patternRepository
    ->findAll();

  /** @var \Drupal\view_mode_page\ViewmodepagePatternInterface $pattern */
  foreach ($patterns as $pattern) {
    if (preg_match($pattern
      ->getPatternRegex(), $path, $matchesArray)) {
      $entityAlias = $matchesArray[1];
      $entityUri = $this->aliasManager
        ->getPathByAlias('/' . $entityAlias);
      $url = Url::fromUri('internal:' . $entityUri);
      if ($url
        ->isRouted()) {
        $routeParams = $url
          ->getRouteParameters();
        if ($entityType = key($routeParams)) {
          $entityId = $routeParams[$entityType];
        }
      }
      if (!empty($entityType) && !empty($entityId) && $entityType == $pattern
        ->getAliasType()
        ->getDerivativeId()) {
        $entity = $this->entityTypeManager
          ->getStorage($entityType)
          ->load($entityId);
        if ($entity instanceof EntityInterface) {

          // Check for a translation of the entity and load that instead if
          // one is found.
          $language_interface = $this->languageManager
            ->getCurrentLanguage();
          if ($entity instanceof TranslatableInterface && $entity
            ->hasTranslation($language_interface
            ->getId())) {
            $entity = $entity
              ->getTranslation($language_interface
              ->getId());
          }
          if ($pattern
            ->applies($entity)) {
            $newPath = '/view_mode_page/' . $pattern
              ->getViewMode() . '/' . $entityType . '/' . $entityId;
            return $newPath;
          }
        }
      }
    }
  }
  return $path;
}