Skip to content

Commit

Permalink
Cleaned up logic to handle multilingual canonicals.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Jan 9, 2024
1 parent 00b9a64 commit 641735f
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,6 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) {
\Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH);
}

// @fixme If unpublished, need to unpublish redirect.
// @fixme what about isDefaultRevision()?
//if ("/node/{$nid}" != $url && $published && $entity->isDefaultRevision())

// Handle canonical redirects.
self::handleCanonicalRedirects($entity, $langcode, $url);
}
Expand Down Expand Up @@ -408,7 +404,7 @@ public static function unpublishPathAlias($pathAlias) {
*
* @todo Create simpler logic for when multilingual isn't used?
*/
function handleCanonicalRedirects($entity, $langcode, $url) {
public static function handleCanonicalRedirects($entity, $langcode, $url) {
$type = $entity->getEntityTypeId();
if (!in_array($type, ['node', 'taxonomy_term'])) {
\Drupal::logger('quant_seed')->error('Quant: handleCanonicalRedirects called with wrong type [@type]', ['@type' => $type]);
Expand All @@ -418,13 +414,13 @@ function handleCanonicalRedirects($entity, $langcode, $url) {
$id = $entity->id();
$published = $entity->isPublished();
$canonical = ($type == 'node') ? "/node/{$id}" : "/taxonomy/term/{$id}";
$usePrefixes = Utility::usesLanguagePathPrefixes();
$usesPrefixes = Utility::usesLanguagePathPrefixes();

// If there is default language content, then the canonical redirect can
// use the default URL. Otherwise, it should use the current language.
$defaultLanguage = \Drupal::languageManager()->getDefaultLanguage();
$defaultUrl = Url::fromRoute('entity.' . $type . '.canonical', [$type => $id], ['language' => $defaultLanguage])->toString();
$defaultTranslation = $entity->getTranslation($defaultLanguage->getId());
$defaultTranslation = $entity->hasTranslation($defaultLanguage->getId()) ? $entity->getTranslation($defaultLanguage->getId()) : NULL;
$defaultPublished = $defaultTranslation ? $defaultTranslation->isPublished() : $published;
$language = \Drupal::languageManager()->getLanguage($langcode);
$languageUrl = Url::fromRoute('entity.' . $type . '.canonical', [$type => $id], ['language' => $language])->toString();
Expand All @@ -434,19 +430,19 @@ function handleCanonicalRedirects($entity, $langcode, $url) {

// Only create redirects if the content has an alias.
if ($canonical != $url) {
$type = 'entity.' $type . '.canonical';
$type = 'entity.' . $type . '.canonical';
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent($canonical, $defaultUrl, 301), QuantRedirectEvent::UPDATE);
if ($usePrefixes) {
if ($usesPrefixes) {
// Handle redirects with path prefix too.
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/{$langcode}($canonical}", $languageUrl, 301), QuantRedirectEvent::UPDATE);
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/{$langcode}{$canonical}", $languageUrl, 301), QuantRedirectEvent::UPDATE);
}
}

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

0 comments on commit 641735f

Please sign in to comment.