Drupal-folks,

First of all: Merry Christmas to all...
Fiddling with some Drupal 7 module development while the goose is in the oven.

I'm coding a conversion module that takes content from a custom CMS and creates nodes in Drupal 7. Things are working fine, but I'm wondering about one thing: When creating a new node programatically in Drupal 7, I can easily make a URL alias by adding the code

$node->path = array('alias' => $old_article_path);

This will nicely create an alias to the old URL when I save the node, and make sure that no links break in the new system.

But in the old CMS (like in Drupal) I could have several paths to the same article. How do I do this in Drupal 7? I have looked online for answers, but been unable to find them. I have also dissected the path and pathauto modules for Drupal 7, but to no avail.

Any ideas?

Martin

Comments

er.pushpinderrana’s picture

Hi,

But in the old CMS (like in Drupal) I could have several paths to the same article. How do I do this in Drupal 7? I have looked online for answers, but been unable to find them. I have also dissected the path and pathauto modules for Drupal 7, but to no avail.

This is very old post but the same problem I had also faced in past. To create multiple URL alias for one node, I have implemented hook_node_insert() and hook_node_update().

Sample Code :

function custom_node_insert($node) {
  $source = 'node/'.$node->nid;
  $type = $node->type; 
  switch($type) {
	case 'article':
	// Update alias for article content type on the basis of multiple programs selected. We are using programs multiselect dropdown as node reference and on the basis of this these programs we create same number of url alias. 
	  if(module_load_include('inc','pathauto','pathauto') !== FALSE) {
		if (function_exists('pathauto_cleanstring')) {
		  $currnt_page_title_alias = pathauto_cleanstring($node->title); // define in pathauto.inc file.
		} else {
		  $title =  strtolower($node->title);
		  $title =  str_replace('&','',strtolower($title));
		  $title =  str_replace(' ','-',strtolower($title));
		  $title =  str_replace('--','-',strtolower($title));
		  $currnt_page_title_alias = $title; 
		}
	  }
	  $selected_programs =  $node->field_programs_reference['und']; 
	  db_delete('url_alias')
		 ->condition('source', $source)
		->execute();
	  foreach($selected_programs as $selected) {
		$selected_programs_nid = $selected["value"];
		$newsAlias = 'article/'.$currnt_page_title_alias;
		// check if url exist
		$newsAlias =  get_custom_url_alias($newsAlias);		
		if(!empty($newsAlias)) {	
		  db_insert('url_alias')
			->fields(array(
			'source' => 'node/'.$node->nid,
			'alias' => $newsAlias,
			'language' => 'und',
		  ))
		  ->execute();
		}
      }		
	  $form['#redirect'] = $newsAlias;
	break;
  }	
}

// Function for check url exist for any node alias and return new url alias
function get_custom_url_alias($custom_alias){
  $i = 0;
  do {
	if($i==0){ 
	  $newAlias = $custom_alias;
	  $query = db_query("SELECT pid FROM url_alias WHERE alias =:alias", array(':alias'=>$newAlias));
	}else{
	  $newAlias =  $custom_alias."-".$i;
	  $query = db_query("SELECT pid FROM url_alias WHERE alias =:alias", array(':alias'=>$newAlias));
	}
	if ($query->rowCount() == 0) {
	  break;
	}
	$i++;
  } while ($i > 0);
  return $newAlias;
}

I have write the same code in hook_node_update() hook, it works fine as per my requirement. But recently I got one issue that come very seldom, sometimes when user save the node for article content type hook_node_insert() function did not get call and wrong URL alias get produced. This problem comes very rarely and after clear cache/cron run it again start works fine.

I am unable to find why this hook function dint called some time. If any body have faced this issue then please share with me because it creates wired situation sometime.

Pushpinder Rana #pushpinderdrupal
Acquia Certified Drupal Expert