I recently upgraded a site from D6->D7, and for some reason, we started running into issues with unwanted URL alias changes. In our case, we will save a blog post and put the URL out as a link on social media, but some time later, the poster/editor will change the title, but not want to change the alias so as not to break the links that have already been put out. However, when the node is edited, the "Generate automatic URL alias" checkbox is checked by default, and unless they uncheck it, the alias is modified unintentionally. Yes, I am aware of the redirect module functionality that creates a redirect when the URL alias is changed, but that has a couple problems:

  1. It creates more moving parts, which could be an issue down the road when the site is upgraded and migrated to a new site.
  2. It can create loops, if for some reason the title is changed back to the original title (I've already had it happen).00

The cause of this behavior can be seen in the code that fills the variable that is used for the #default_value for the checkbox.

D6:

$pathauto_alias = pathauto_create_alias('node', 'return', $placeholders, "node/{$node->nid}", $node->nid, $node->type, $node->language);
$node->pathauto_perform_alias = isset($node->path) && $node->path == $pathauto_alias;

and D7:

module_load_include('inc', 'pathauto');
$uri = entity_uri($entity_type, $entity);
$pathauto_alias = pathauto_create_alias($entity_type, 'return', $uri['path'], array($entity_type => $entity), $bundle, $langcode);
...
$path = drupal_get_path_alias($uri['path'], $langcode);
$entity->path['pathauto'] = ($path != $uri['path'] && $path == $pathauto_alias);

where $uri['path'] is the base node URL (e.g. node/123).

So basically, the checkbox is checked by default if the current alias is the same as the one that would be generated from the current title, and not checked if the current alias is not the same as the generated alias.

My thought would be that to prevent unintentional generation of new aliases if the title is changed, the code should set the default as unchecked if there already is an alias set (regardless of whether or not it matches the one generated on the fly), so that if one wants to change the alias, they have to specifically check the box. Or, to handle more use cases, maybe an option to allow the user to specify whether or not to have the checkbox checked if an alias already exists for the entity.

Thoughts? I'll roll a patch either way (or do nothing if people think we should leave it the way it is), but I'd like to get some feedback first.

Thanks.

Comments

dtisom’s picture

I need this same functionality.

The process should be:

Create an Article with a Title and then

The address is generated and then

The Article Title is updated but

The original link automatically continues to work

Dave Reid’s picture

@dtisom: Go to admin/config/search/path/settings and under "Update action" select "Create a new alias. Leave the existing alias functioning." Or use the Redirect module to create redirects from the old alias to the new.

PratikshaD’s picture

I also have same issue can anyone suggest how can i change condition so that "Generate automatic URL alias" will be uncheck by default on node page

wonder95’s picture

Here is the code I use in hook_form_blog_node_form_alter().

  // Modify the behavior of the URL alias checkbox so that it is unchecked if an alias
  // already exists.
  if (isset($form_state['node']->nid)) {
    module_load_include('inc', 'pathauto');
    $uri = entity_uri('node', $form_state['node']);
    $path = drupal_get_path_alias($uri['path'], 'und');
    if ($path && substr($path, 0, 5) != 'node/') {
      $form['path']['pathauto']['#default_value'] = FALSE;
    }
  }

It works perfectly, and keeps unnecessary aliases and redirects from being created.

krrishnajee’s picture

Check this out:

<?php
function MYMODULE_node_presave($node){
  global $user;
  if($node->type=='content_type'){
    $node->path['pathauto'] = 1 ;
  }
}
?>
fubarhouse’s picture

Perhaps the better way to do this would be to add views bulk operation hooks/integration?
That way we could filter the desired results, update the node and add in the value.

I've been dealing with this problem on and off for some time, a node_presave or form alter hook works very well, but isn't really what we should be doing.