0

Here is the code I'm using to change the name field of my nodes if they contain certain phrases.

$query = new EntityFieldQuery();
$query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'my_product')
    ->fieldCondition('field_my_name', 'value', 'Old Phrase%', 'LIKE');

foreach ($result['node'] as $key => $value) {
    $node = node_load($key);
    $node->field_my_name[LANGUAGE_NONE][0]['value'] = str_replace('Old Phrase', 'New Phrase', $node->field_my_name[LANGUAGE_NONE][0]['value']);
    node_save($node);
}

It successfully changes the name; however, I'd like to automatically create a new URL and also a redirect entry from the old URL to the newly generated one.

I have pathauto installed and my URL Alias is configured to use the my_name field, but the code above does not change the Alias.

1

Pathauto should automatically create a new alias with the updates (unless you have it turned off on the node). pathauto.module lines 519-522:

// Skip processing if the user has disabled pathauto for the node.
if (isset($node->path['pathauto']) && empty($node->path['pathauto'])) {
  return;
}

The Global Redirect module can then provide 301 redirects to the new URL alias.

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.