Skip to content

Commit

Permalink
Modify the imported redirects to point at the node instead of the ali…
Browse files Browse the repository at this point in the history
…as (#168)
  • Loading branch information
pookmish committed Jan 20, 2022
1 parent 7865507 commit 8a7ec23
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions stanford_profile_helper.module
Expand Up @@ -45,6 +45,26 @@ function stanford_profile_helper_form_taxonomy_overview_terms_alter(&$form, Form
}
}

/**
* Implements hook_ENTITY_TYPE_presave().
*/
function stanford_profile_helper_redirect_presave(EntityInterface $redirect) {
$destination = $redirect->get('redirect_redirect')->getString();

// If a redirect is added to go to the aliased path of a node (often from
// importing redirect), change the destination to target the node instead.
// This works if the destination is `/about` or `/node/9`.
if (preg_match('/^internal:(\/.*)/', $destination, $matches)) {
// Find the internal path from the alias.
$path = \Drupal::service('path_alias.manager')->getPathByAlias($matches[1]);

// Grab the node id from the internal path and use that as the destination.
if (preg_match('/node\/(\d+)/', $path, $matches)) {
$redirect->set('redirect_redirect', 'entity:node/' . $matches[1]);
}
}
}

/**
* Implements hook_search_api_index_items_alter().
*/
Expand Down

0 comments on commit 8a7ec23

Please sign in to comment.