diff --git a/src/Seed.php b/src/Seed.php index 63641253..28d83570 100644 --- a/src/Seed.php +++ b/src/Seed.php @@ -283,11 +283,11 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { $language = \Drupal::languageManager()->getLanguage($langcode); $options['language'] = $language; } + $defaultLangcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); $url = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options)->toString(); // If this is the front/home page, rewrite URL as /. - // @todo Handle translated front/home page. $site_config = \Drupal::config('system.site'); $front = $site_config->get('page.front'); @@ -296,7 +296,24 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { // Trigger redirect event from alias to /. \Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent($url, "/", 301), QuantRedirectEvent::UPDATE); } + $url = "/"; + + // Handle default language prefix. + if ($langcode && $langcode == $defaultLangcode) { + // Tack on the prefix if it's set. + $negotiation = \Drupal::config('language.negotiation')->get('url'); + $url .= $negotiation['prefixes'][$langcode] ?? ''; + if ($url != "/") { + \Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/", $url, 301), QuantRedirectEvent::UPDATE); + \Drupal::logger('quant_seed')->notice("Adding home page redirect: / => @url", ['@url' => $url]); + } + } + // Handle translated front/home page. + elseif ($prefix = Utility::getPathPrefix($langcode)) { + $url = $prefix; + \Drupal::logger('quant_seed')->notice("Adding translated home page: @url", ['@url' => $url]); + } } $response = self::markupFromRoute($url, ['quant-revision' => $rid]); @@ -332,7 +349,7 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { if ((strpos($value, '/node/') === 0) && $entity->get('nid')->value == substr($value, 6)) { // Only set for the default language. // @todo Handle translated status pages. - if (empty($langcode) || $langcode == \Drupal::languageManager()->getDefaultLanguage()->getId()) { + if (empty($langcode) || $langcode == $defaultLangcode) { $url = $key; \Drupal::logger('quant')->notice("Setting status page: @key => @value", [ diff --git a/src/Utility.php b/src/Utility.php index 3993f8d9..7ca900f7 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -56,21 +56,42 @@ public static function getUrl(string $url = NULL, string $langcode = NULL) : str $url = '/' . $url; } + // Handle multilingual paths. + $prefix = self::getPathPrefix($langcode); + + // Only add the language prefix if it's not there. + if (!str_starts_with($url, $prefix)) { + $url = $prefix . $url; + } + + return $url; + } + + /** + * Get path prefix based on site settings. + * + * @param string $langcode + * The language code. + * + * @return string + * The path prefix based on multilingual settings. Defaults to '/'. + */ + public static function getPathPrefix(string $langcode = NULL) : string { + + // Always start with a slash. + $prefix = '/'; + // Handle multilingual paths. if (self::usesLanguagePathPrefixes()) { // Use the current language if none is provided. if (!$langcode) { $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); } + // @todo Handle when prefix is different than the langcode. $prefix = '/' . $langcode; - - // Only add the language prefix if it's not there. - if (!str_starts_with($url, $prefix)) { - $url = $prefix . $url; - } } - return $url; + return $prefix; } /**