You are here

public function DynamicPathProcessor::processOutbound in View Mode Page 8.3

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

Processes the outbound path.

Parameters

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

array $options: (optional) An associative array of additional options, with the following elements:

  • 'query': An array of query key/value-pairs (without any URL-encoding) to append to the URL.
  • 'fragment': A fragment identifier (named anchor) to append to the URL. Do not include the leading '#' character.
  • 'absolute': Defaults to FALSE. Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.
  • 'language': An optional language object used to look up the alias for the URL. If $options['language'] is omitted, it defaults to the current language for the language type LanguageInterface::TYPE_URL.
  • 'https': Whether this URL should point to a secure location. If not defined, the current scheme is used, so the user stays on HTTP or HTTPS respectively. TRUE enforces HTTPS and FALSE enforces HTTP.
  • 'base_url': Only used internally by a path processor, for example, to modify the base URL when a language dependent URL requires so.
  • 'prefix': Only used internally, to modify the path when a language dependent URL requires so.
  • 'route': The route object for the given path. It will be set by \Drupal\Core\Routing\UrlGenerator::generateFromRoute().

\Symfony\Component\HttpFoundation\Request $request: The HttpRequest object representing the current request.

\Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata: (optional) Object to collect path processors' bubbleable metadata.

Return value

string The processed path.

Overrides OutboundPathProcessorInterface::processOutbound

File

src/PathProcessor/DynamicPathProcessor.php, line 122

Class

DynamicPathProcessor
Class DynamicPathProcessor.

Namespace

Drupal\view_mode_page\PathProcessor

Code

public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
  $prefix = '/view_mode_page/';
  if (substr($path, 0, strlen($prefix)) === $prefix) {
    $path_minus_prefix = substr($path, strlen($prefix));
    $path_minus_prefix_parts = explode('/', $path_minus_prefix);
    if (count($path_minus_prefix_parts) === 3) {
      list($view_mode, $entityType, $entityId) = $path_minus_prefix_parts;
      $entity = $this->entityTypeManager
        ->getStorage($entityType)
        ->load($entityId);
      if ($entity) {

        // Check if the target language is set.
        if (isset($options['language'])) {
          $target_language = $options['language'];
        }
        else {
          $target_language = $this->languageManager
            ->getCurrentLanguage();
        }

        // Check for a translation of the entity and load that instead if
        // one is found.
        if ($target_language instanceof LanguageInterface && $entity instanceof TranslatableInterface && $entity
          ->hasTranslation($target_language
          ->getId())) {
          $entity = $entity
            ->getTranslation($target_language
            ->getId());
        }

        /** @var \Drupal\view_mode_page\ViewmodepagePatternInterface[] $patterns */
        $patterns = $this->entityTypeManager
          ->getStorage('view_mode_page_pattern')
          ->loadByProperties([
          'view_mode' => $view_mode,
        ]);
        foreach ($patterns as $pattern) {
          if ($pattern
            ->applies($entity)) {
            $url = $entity
              ->toUrl();
            $url_alias = $this->aliasManager
              ->getAliasByPath("/" . $url
              ->getInternalPath(), $target_language
              ->getId());
            $path = str_replace('%', $url_alias, $pattern
              ->getPattern());
            break;
          }
        }
      }
    }
  }
  return $path;
}