From 7de4c1c81078398d704e909625b4eaf07a935522 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Mon, 29 Jan 2024 20:54:44 -0800 Subject: [PATCH] Changed naming from canonical to internal path. --- .../quant_search/src/Controller/Search.php | 1 + quant.module | 1 + src/Seed.php | 42 ++++++++++--------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/modules/quant_search/src/Controller/Search.php b/modules/quant_search/src/Controller/Search.php index 640d602e..40db7587 100644 --- a/modules/quant_search/src/Controller/Search.php +++ b/modules/quant_search/src/Controller/Search.php @@ -261,6 +261,7 @@ public static function generateSearchRecord($entity, $langcode = NULL) { } // @todo Update node-only logic. + // The "canonical" URL is the alias, if it exists, or the internal path. $record['url'] = Url::fromRoute('entity.node.canonical', ['node' => $entity->id()], $options)->toString(); // Add search meta for node entities. diff --git a/quant.module b/quant.module index b1c0fec7..d83eaf99 100644 --- a/quant.module +++ b/quant.module @@ -191,6 +191,7 @@ function quant_node_access(NodeInterface $node, $op, AccountInterface $account) return AccessResult::neutral(); } + // The "canonical" URL is the alias, if it exists, or the internal path. $options = ['absolute' => FALSE]; $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options)->toString(); diff --git a/src/Seed.php b/src/Seed.php index d209c227..18f0deab 100644 --- a/src/Seed.php +++ b/src/Seed.php @@ -218,6 +218,7 @@ public static function seedTaxonomyTerm($entity, $langcode = NULL) { $options['language'] = $language; } + // The "canonical" URL is the alias, if it exists, or the internal path. $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid], $options)->toString(); $response = self::markupFromRoute($url); @@ -251,8 +252,8 @@ public static function seedTaxonomyTerm($entity, $langcode = NULL) { \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); } - // Handle canonical redirects. - self::handleCanonicalRedirects($entity, $langcode, $url); + // Handle internal path redirects. + self::handleInternalPathRedirects($entity, $langcode, $url); } /** @@ -276,6 +277,7 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { $options['language'] = $language; } + // The "canonical" URL is the alias, if it exists, or the internal path. $url = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options)->toString(); // Special case for home-page, rewrite URL as /. @@ -333,8 +335,8 @@ public static function seedNode(EntityInterface $entity, $langcode = NULL) { \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); } - // Handle canonical redirects. - self::handleCanonicalRedirects($entity, $langcode, $url); + // Handle internal path redirects. + self::handleInternalPathRedirects($entity, $langcode, $url); } /** @@ -354,6 +356,7 @@ public static function unpublishNode(EntityInterface $entity) { $options['language'] = $language; } + // The "canonical" URL is the alias, if it exists, or the internal path. $url = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options)->toString(); $site_config = \Drupal::config('system.site'); @@ -362,8 +365,8 @@ public static function unpublishNode(EntityInterface $entity) { \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', '/', [], NULL), QuantEvent::UNPUBLISH); } - // Handle canonical redirects. - self::handleCanonicalRedirects($entity, $langcode, $url); + // Handle internal path redirects. + self::handleInternalPathRedirects($entity, $langcode, $url); \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); } @@ -385,10 +388,11 @@ public static function unpublishTaxonomyTerm(EntityInterface $entity) { $options['language'] = $language; } + // The "canonical" URL is the alias, if it exists, or the internal path. $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid], $options)->toString(); - // Handle canonical redirects. - self::handleCanonicalRedirects($entity, $langcode, $url); + // Handle internal path redirects. + self::handleInternalPathRedirects($entity, $langcode, $url); \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $url, [], NULL), QuantEvent::UNPUBLISH); } @@ -404,7 +408,7 @@ public static function unpublishPathAlias($pathAlias) { } /** - * Handle canonical redirects. + * Handle internal path redirects. * * Example redirects: * - en node 123, no alias: /node/123 to /en/node/123. @@ -417,20 +421,21 @@ public static function unpublishPathAlias($pathAlias) { * * @todo Create simpler logic for when multilingual isn't used? */ - public static function handleCanonicalRedirects($entity, $langcode, $url) { + public static function handleInternalPathRedirects($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]); + \Drupal::logger('quant_seed')->error('Quant: handleInternalPathRedirects called with wrong type [@type]', ['@type' => $type]); return; } $id = $entity->id(); $published = $entity->isPublished(); - $canonical = ($type == 'node') ? "/node/{$id}" : "/taxonomy/term/{$id}"; + $internalPath = ($type == 'node') ? "/node/{$id}" : "/taxonomy/term/{$id}"; $usesPrefixes = Utility::usesLanguagePathPrefixes(); - // If there is default language content, then the canonical redirect can + // If there is default language content, then the internal path redirect can // use the default URL. Otherwise, it should use the current language. + // Note, the canonical URL is the alias, if it exists, or the internal path. $defaultLanguage = \Drupal::languageManager()->getDefaultLanguage(); $defaultUrl = Url::fromRoute('entity.' . $type . '.canonical', [$type => $id], ['language' => $defaultLanguage])->toString(); $defaultTranslation = $entity->hasTranslation($defaultLanguage->getId()) ? $entity->getTranslation($defaultLanguage->getId()) : NULL; @@ -442,22 +447,21 @@ public static function handleCanonicalRedirects($entity, $langcode, $url) { } // Only create redirects if the content has an alias. - if ($canonical != $url) { - $type = 'entity.' . $type . '.canonical'; - \Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent($canonical, $defaultUrl, 301), QuantRedirectEvent::UPDATE); + if ($internalPath != $url) { + \Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent($internalPath, $defaultUrl, 301), QuantRedirectEvent::UPDATE); 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}{$internalPath}", $languageUrl, 301), QuantRedirectEvent::UPDATE); } } // Unpublish redirects. if (!$defaultPublished) { - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $canonical, [], NULL), QuantEvent::UNPUBLISH); + \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', $internalPath, [], NULL), QuantEvent::UNPUBLISH); } if (!$published && $usesPrefixes) { // Handle redirects with path prefix too. - \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', "/{$langcode}{$canonical}", [], NULL), QuantEvent::UNPUBLISH); + \Drupal::service('event_dispatcher')->dispatch(new QuantEvent('', "/{$langcode}{$internalPath}", [], NULL), QuantEvent::UNPUBLISH); } }