Skip to content

Commit

Permalink
Added logic for translated home/front page.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Jan 30, 2024
1 parent 5f24210 commit 5235135
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
21 changes: 19 additions & 2 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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]);
Expand Down Expand Up @@ -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",
[
Expand Down
33 changes: 27 additions & 6 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 5235135

Please sign in to comment.