Regenerating Path Aliases Programatically in Drupal 7

It should look something like this:

/**
* Update path aliases for taxonomy terms and nodes.
*/
function gateway_controller_update_7057() {
  module_load_include('inc', 'pathauto');
  module_load_include('inc', 'pathauto.pathauto');

  // Delete the existing node aliases.
  db_delete('url_alias')
    ->condition('source', 'node/%', 'LIKE')
    ->execute();

  // Regenerate the node aliases.
  $nids = db_query("SELECT nid FROM {node}")->fetchCol();
  pathauto_node_update_alias_multiple($nids, 'bulkupdate');

  // Delete the topics taxonomy aliases.
  db_delete('url_alias')
    ->condition('source', 'taxonomy/term/%', 'LIKE')
    ->execute();

  // Regenerate the taxonomy aliases.
  $tids = db_query("SELECT tid FROM {taxonomy_term_data} where vid = 8")->fetchCol();
  pathauto_taxonomy_term_update_alias_multiple($tids, 'bulkupdate');
}

Published by

Sam

Drupal developer from Perth, Western Australia. I love programming, gaming, the Internet, growing vegetables and hiking.

Leave a Reply

Your email address will not be published. Required fields are marked *