You are here

public function LanguagePathSubscriber::collectPaths in Tome 8

Reacts to a collect paths event to add multilingual homepage paths.

Parameters

\Drupal\tome_static\Event\CollectPathsEvent $event: The collect paths event.

File

modules/tome_static/src/EventSubscriber/LanguagePathSubscriber.php, line 55

Class

LanguagePathSubscriber
Adds language negotiation URL prefixes to the list of paths to export.

Namespace

Drupal\tome_static\EventSubscriber

Code

public function collectPaths(CollectPathsEvent $event) {
  $config = $this->configFactory
    ->get('language.negotiation')
    ->get('url');
  if (is_array($config) && isset($config['source'])) {
    if ($config['source'] === LanguageNegotiationUrl::CONFIG_PATH_PREFIX) {
      foreach ($this->languageManager
        ->getLanguages() as $language) {
        $langcode = $language
          ->getId();
        if (!empty($config['prefixes'][$langcode])) {
          $prefix = $this
            ->joinPaths('/', $config['prefixes'][$langcode]);
          $event
            ->addPath($prefix, [
            'language_processed' => 'language_processed',
          ]);
          foreach ($event
            ->getPaths(TRUE) as $path => $metadata) {
            if (!isset($metadata['language_processed']) && (!isset($metadata['language_prefix']) || $metadata['language_prefix'] === $langcode)) {
              if (!isset($metadata['original_path'])) {
                $metadata['original_path'] = $path;
              }
              $metadata['language_processed'] = 'language_processed';
              if (isset($metadata['language_prefix'])) {
                $event
                  ->replacePath($path, $this
                  ->joinPaths($prefix, $path), $metadata);
              }
              else {
                $event
                  ->addPath($this
                  ->joinPaths($prefix, $path), $metadata);
              }
            }
          }
        }
      }
    }
    elseif ($config['source'] === LanguageNegotiationUrl::CONFIG_DOMAIN) {
      $paths = $event
        ->getPaths(TRUE);
      foreach ($paths as $path => $metadata) {
        if (isset($metadata['language_processed']) && isset($metadata['langcode'])) {
          if ($metadata['langcode'] !== $this->languageManager
            ->getCurrentLanguage()
            ->getId()) {
            unset($paths[$path]);
          }
        }
      }
      $event
        ->replacePaths($paths);
    }
  }
}