0

I have enabled the path auto and workbench modules in my drupal installation. I have to set the url alias once only. Once a node is published and url alias is generated, the url alias should not be generated again if end user changes the title of the node. Also I have to generate the url alias only when node goes to published state.

I am trying something like this:

function hook_entity_presave($entity, $type) {
  //Do not create url alias of node unless its not published and if you create a Draft of a published node, the URL of the published node should not go away.
  if($entity->workbench_moderation['current']->state != 'published'  && !isset($entity->workbench_moderation['published']))
  {
    $entity->path['alias'] = '';
    $entity->path['old_alias'] = '';
    $entity->path['pathauto'] = 0;
  }
  else {
    $entity->path['pathauto'] = 1;
  }
  // if node is published, do not change the url alias if end user changes the title of the node. 
  if(isset($entity->workbench_moderation['published'])) {
    $entity->path['alias'] = $entity->path['original']['alias'];
  } 
}

and I am trying to accomplish this with hook

function hook_node_presave($node) {
  $node->path['alias'] = $node->path['original']['alias'];
  $node->path['pathauto'] = 0;
}
  • ok...did you have a question? :) – Clive Sep 14 '13 at 13:17
  • The url alias is changing if I change the title of the node after publish. How do I make it to not change the url alias once its added on first publishing of node? – Umar Sep 14 '13 at 17:41
0

You can set that in the URL aliases configuration.

In http://example.com/admin/config/search/path/settings

In the section 'update action', check one of the two:

Do nothing. Leave the old alias intact.
Create a new alias. Leave the existing alias functioning. 

The default is:

Create a new alias. Delete the old alias. 
  • thanks for your help. That will not work for my issue. I am updating the above code changed the logic and hopping that will work – Umar Sep 16 '13 at 6:32
0

I have changed the logic in my custom module and hopping that will work.

/**
 * Implements hook_entity_presave().
 */
function my_module_entity_presave($entity, $type) {
  // the url alias should not be created as long as the node goes in published state 
  if($entity->workbench_moderation['current']->state != 'published' && !isset($entity->workbench_moderation['published'])) {
    $entity->path['pathauto'] = 0;
  }
  else {
    $entity->path['pathauto'] = 1;
  }
  // if node is passed through a published state then node url should not be changed in any title edits.
  if(isset($entity->workbench_moderation['published']) && isset($entity->path['alias']) && !empty($entity->path['alias'])) {
    $entity->path['pathauto'] = 0;
  }
}
  • This works for me Yeppy!!!! – Umar Sep 16 '13 at 15:47

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