Skip to content

Commit

Permalink
Changed naming from canonical to internal path.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Jan 30, 2024
1 parent 1cf87aa commit 7de4c1c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions modules/quant_search/src/Controller/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions quant.module
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
42 changes: 23 additions & 19 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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 /.
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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');
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 7de4c1c

Please sign in to comment.