Hello.

I have two content types called "intro" and "infos" (just for my tests) and I want to programmatically create an "infos" page each time an "intro" page is created by a user, in a module. URLs, thanks to pathauto, are like this : example.com/content/firstpage (for the "intro") and should be example.com/content/firstpage/test (for the "infos").

Actually, I managed to create this second content but I can't have the good URL. I just managed to have this kind of URL :

  • example.com/content/test/ (normal behaviour)
  • example.com/node/XX/test/ (XX for a number, but with the extended path alias module, it changed like I wish but show the "intro" page, not the "infos" page for this URL)
  • example.com//test/ (with nothing between the //)

My code is actually this one :

	function MYMODULE_node_insert($node) {
		if($node->type=="intro") {
			// Below, the case where I have example.com/node/XX/test but only show an "intro" page instead of an "infos" page
			// $aliaspath = drupal_get_path_alias(arg(0).'/'.arg(1));
			
			// Variable to have the good "intro" node
			$typePath = 'node/' . $node->nid;
			
			// Query to get the URL alias from database, but something is missing here I guess
			$query = db_select('url_alias', 'alias')
				->fields('alias')
				->condition('source', $typePath);
			$aliaspath = $query->execute()->fetchObject();
			
			// I even tried a foreach here but couldn't have any "intro" URL alias in $aliaspath
			
			// Below, the "infos" page is created
			$node_new = new StdClass();
			$node_new->type = 'infos';
			$node_new->created = time();
			$node_new->status = 1; // Published
			$node_new->title = "Test";
			$node_new->uid = 1;
			$node_new->language = 'en';
			$node_new->path['alias'] = $aliaspath . '/test';
			$node_new->body = "";

			node_save($node_new);
			// drupal_set_message('Automated '.$node_new->type.'Created');
			
		}
	}

Can you help me with my query to have the node alias of this "intro" type please ? Thank you.

Comments

sadashiv’s picture

Use path_save() to set alias for more details refer http://api.drupal.org/api/drupal/includes!path.inc/function/path_save/7

This function basically needs an array of source and alias like

$path['source'] = 'node/13';
$path['alias'] = 'content/firstpage/test';
path_save($path);

This will update the path alias of node 13 to 'content/firstpage/test'

Hth,
Sadashiv.

-Odd-’s picture

Thank you for the tip, it will help me for my next code.

I didn't try it yet, but I'm not sure if it can do what I need.

I think I explained badly what I really want : I will add a lot of "intro" page and there will be different "infos" page for that. So, for the URL content/<strong>firstpage</strong>/test;, the "firstpage" will be one time (just for example) content/my-example/test, for another one content/another-example/test, etc.

It depends on the "intro" page, so I could say the "firstpage" will dynamically change depending of the node created and that's why I need to check what the URL alias is for the $node (the "intro" page which is just created) so I can have subpages with the good URL.

I'm not sure your code will help me to do that (but I can make a mistake, I didn't test it yet because I'm not on the good computer to make my tests).

sadashiv’s picture

I just gave an example of how to use the function. The complete code would be something like

function MYMODULE_attendance_node_insert($node) {
    if($node->type=="intro") {
        $aliaspath = drupal_get_path_alias('node/'.$node->nid). '/test';
        // "infos" page is created
        $node_new = new StdClass();
        $node_new->type = 'infos';
        $node_new->created = time();
        $node_new->status = 1; // Published
        $node_new->title = $node->title;
        $node_new->uid = 1; //you may also set same user using $node->uid
        $node_new->body = $node->body;

        node_save($node_new);
        $path['source'] = 'node/'.$node_new->nid;
        $path['alias'] = $aliaspath;
        path_save($path);
        // drupal_set_message('Automated '.$node_new->type.'Created');

    }
}

This code works like if you create a new content of type intro with url alias as content/anything then it adds alias to infos as content/anything/test dynamically

Hth,
Sadashiv.

-Odd-’s picture

Thank you for your help, even if I already tested it. I always had a little problem with this solution : I have the good URL thanks to the Extended Alias module but I want to see "Test" as the title of this new "Infos" page and with no body (that's why I added a $node_new->body = " ";) because I'll add a views on it after I find a solution for this.

In this case, I have the title and the body of the "Intro" page, even if I add an empty body and a "Test" as a title, instead of $node_new->title = $node->title; and $node_new->body = $node->body;. It's as if Drupal just read example.com/node/XX (to show the "Intro" page) but set the URL example.com/node/XX/test, that's strange.

Another suggestion to this part please ? It's the only thing I don't manage to do. Thank you.

Edit : MYMODULE_attendance_node_insert($node) didn't work, I replaced it by MYMODULE_node_insert($node)

Edit 2 : after some tests, I understood the bad redirection is from the Extended Path Aliases module. But if I don't use it, I have URLs like example.com/node/XX/test and not example.com/introNodeAlias/test as I wish. I will add an issue to its queue. Maybe it will resolve my problem.