You are here

public function DomainSourcePathProcessor::processOutbound in Domain Access 8

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

domain_source/src/HttpKernel/DomainSourcePathProcessor.php, line 108

Class

DomainSourcePathProcessor
Processes the outbound path using path alias lookups.

Namespace

Drupal\domain_source\HttpKernel

Code

public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {

  // Load the active domain if not set.
  if (empty($options['active_domain'])) {
    $active_domain = $this
      ->getActiveDomain();
  }

  // Only act on valid internal paths and when a domain loads.
  if (empty($active_domain) || empty($path) || !empty($options['external'])) {
    return $path;
  }

  // Set the default source information.
  $source = NULL;
  $options['active_domain'] = $active_domain;

  // Get the current language.
  $langcode = NULL;
  if (!empty($options['language'])) {
    $langcode = $options['language']
      ->getId();
  }

  // Get the URL object for this request.
  $alias = $this->aliasManager
    ->getPathByAlias($path, $langcode);
  $url = Url::fromUserInput($alias, $options);

  // Get the route name to pass through to the alter hooks.
  if ($url
    ->isRouted()) {
    $options['route_name'] = $url
      ->getRouteName();
  }

  // Check the route, if available. Entities can be configured to
  // only rewrite specific routes.
  if ($url
    ->isRouted() && $this
    ->allowedRoute($url
    ->getRouteName())) {

    // Load the entity to check.
    if (!empty($options['entity'])) {
      $entity = $options['entity'];
    }
    else {
      $parameters = $url
        ->getRouteParameters();
      if (!empty($parameters)) {
        $entity = $this
          ->getEntity($parameters);
      }
    }
  }

  // One hook for entities.
  if (!empty($entity) && is_object($entity)) {

    // Ensure we send the right translation.
    if (!empty($langcode) && method_exists($entity, 'hasTranslation') && $entity
      ->hasTranslation($langcode) && ($translation = $entity
      ->getTranslation($langcode))) {
      $entity = $translation;
    }
    if (isset($options['domain_target_id'])) {
      $target_id = $options['domain_target_id'];
    }
    else {
      $target_id = domain_source_get($entity);
    }
    if (!empty($target_id)) {
      $source = $this
        ->domainStorage()
        ->load($target_id);
    }
    $options['entity'] = $entity;
    $options['entity_type'] = $entity
      ->getEntityTypeId();
    $this->moduleHandler
      ->alter('domain_source', $source, $path, $options);
  }
  else {
    if (isset($options['domain_target_id'])) {
      $target_id = $options['domain_target_id'];
      $source = $this
        ->domainStorage()
        ->load($target_id);
    }
    $this->moduleHandler
      ->alter('domain_source_path', $source, $path, $options);
  }

  // If a source domain is specified, rewrite the link.
  if (!empty($source)) {

    // Note that url rewrites add a leading /, which getPath() also adds.
    $options['base_url'] = trim($source
      ->getPath(), '/');
    $options['absolute'] = TRUE;
  }
  return $path;
}