1

I am using the Redirect module. I have a feature, where I have an update hook, that updates (and fixes) the path alias of the nodes on the site (after I made a change to the pathauto pattern). I need to create a redirect, as updating the path alias does not do this for you when you use pathauto_node_update_alias(). How do I go about this. Below is the code I have:

function uw_cfg_redirect_update_7010(&$sandbox) {
  $query = "SELECT nid FROM {node} where status = 1";

  // If this is the first pass through this update function then set some variables.
  if (!isset($sandbox['total'])) {
    $result = db_query($query);
    $sandbox['total'] = $result->rowCount();
    $sandbox['current'] = 0;
  }

  // How many nodes should be processed per pass. The higher this number is, the faster your update will
  // complete, but the more likely your server will run out of memory or timeout.
  $nodes_per_pass = 50;

  // Get the nodes to process during this pass.
  $result = db_query_range($query, $sandbox['current'], $nodes_per_pass);
  while ($row = $result->fetchAssoc()) {
    $node = node_load($row['nid']);
    pathauto_node_update_alias($node);

    // TODO: [need to add redirect code here]

    // Increment "current" by 1.
    $sandbox['current']++;
  }

  // Set the value for finished. If current == total then finished will be 1, signifying we are done.
  // If not then this function will be trigger again.  See Drupal Batch API for more details.
  $sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']);

  if ($sandbox['#finished'] === 1) {
    drupal_set_message(t('We processed @nodes nodes. DONE!!!', array('@nodes' => $sandbox['total'])));
    return t('Finish rewrite the path of all the nodes.');
  }

  return $sandbox;
}

Note: This is not a problem if you use Global Redirect module. This is not a problem when you resave a node, as redirect gets added.

0

I'd have a look at redirect_path_update and hook_path_update and maybe consider using node_save() instead of writing the paths directly.

If you're going to write them directly with pathauto_node_update_alias(), you may as well just write paths aliases and redirects directly to the database instead of wasting time node_load()ing all the nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.