Skip to content

Commit

Permalink
Merge pull request #232 from quantcdn/feature/path-prefixes
Browse files Browse the repository at this point in the history
Updated prefix logic to not use langcode directly.
  • Loading branch information
kepol authored Jul 22, 2024
2 parents f01f991 + b75290a commit 0a1aadb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ public static function getRedirectLocationsFromRedirect($redirect) {

// Get language and prefix configuration.
$langcode = $redirect->language()->getId();
$siteDefaultLangcode = \Drupal::service('language.default')->get()->getId();
$defaultLangcode = \Drupal::service('language.default')->get()->getId();
$pathPrefixes = \Drupal::config('language.negotiation')->get('url.prefixes');
$defaultPrefix = $pathPrefixes[$defaultLangcode] ?? '';

// Multilingual redirects can be configured for a specific language or
// for all languages. If the redirect is configured for all languages,
Expand All @@ -150,8 +151,8 @@ public static function getRedirectLocationsFromRedirect($redirect) {
// Check if a node or term for this path exists.
$node = NULL;
$term = NULL;
$aliasWithoutLangcode = preg_replace('/^\/(' . $siteDefaultLangcode . ')\//', '/', $destination);
$path = \Drupal::service('path_alias.manager')->getPathByAlias($aliasWithoutLangcode);
$aliasWithoutPrefix = preg_replace('/^\/(' . $defaultPrefix . ')\//', '/', $destination);
$path = \Drupal::service('path_alias.manager')->getPathByAlias($aliasWithoutPrefix);
if (preg_match('/node\/(\d+)/', $path, $matches)) {
$node = Node::load($matches[1]);
}
Expand Down Expand Up @@ -183,7 +184,7 @@ public static function getRedirectLocationsFromRedirect($redirect) {
}
// @todo Test use case where page is not a node or term.
else {
$updatedDestination = preg_replace('/^\/(' . $siteDefaultLangcode . ')\//', $pathPrefix . '/', $destination);
$updatedDestination = preg_replace('/^\/(' . $defaultPrefix . ')\//', $pathPrefix . '/', $destination);
}
$redirects[] = [
'source' => $updatedSource,
Expand Down Expand Up @@ -443,7 +444,7 @@ public static function handleInternalPathRedirects($entity, $langcode, $url) {
$id = $entity->id();
$published = $entity->isPublished();
$internalPath = ($type == 'node') ? "/node/{$id}" : "/taxonomy/term/{$id}";
$usesPrefixes = Utility::usesLanguagePathPrefixes();
$prefix = Utility::getPathPrefix($langcode);

// If there is default language content, then the internal path redirect can
// use the default URL. Otherwise, it should use the current language.
Expand All @@ -461,19 +462,19 @@ public static function handleInternalPathRedirects($entity, $langcode, $url) {
// Only create redirects if the content has an alias.
if ($internalPath != $url) {
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent($internalPath, $defaultUrl, 301), QuantRedirectEvent::UPDATE);
if ($usesPrefixes) {
if ($prefix) {
// Handle redirects with path prefix too.
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/{$langcode}{$internalPath}", $languageUrl, 301), QuantRedirectEvent::UPDATE);
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/{$prefix}{$internalPath}", $languageUrl, 301), QuantRedirectEvent::UPDATE);
}
}

// Unpublish redirects.
if (!$defaultPublished) {
\Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $internalPath, [], NULL), QuantEvent::UNPUBLISH);
}
if (!$published && $usesPrefixes) {
if (!$published && $prefix) {
// Handle redirects with path prefix too.
\Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', "/{$langcode}{$internalPath}", [], NULL), QuantEvent::UNPUBLISH);
\Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', "/{$prefix}{$internalPath}", [], NULL), QuantEvent::UNPUBLISH);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public static function getPathPrefix(string $langcode = NULL) : string {
if (!$langcode) {
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
}
// @todo Handle when prefix is different than the langcode.
$prefix = '/' . $langcode;
$prefixes = \Drupal::config('language.negotiation')->get('url.prefixes');
$prefix .= $prefixes[$langcode] ?? '';
}

return $prefix;
Expand Down

0 comments on commit 0a1aadb

Please sign in to comment.