1

In hook_node_insert() i have used drupal_goto(), to go to the newly created node with alias name of it, but id doesn't work.

I have already created url alias with patterns for that content type.

$alias = drupal_get_path_alias('node/'.$node->nid);
drupal_goto($alias);

If i give drupal_goto() with nid it works.

drupal_goto('node/'.$node->nid);

What is the issue with respect to alias name not working or how to get alias of newly created node and get it redirected to view page of it with alias name as url.

Any ideas or suggestions? Thanks in advance.

[Updated code]

This is the code which i am trying

function hook_node_insert($node)
{
    if($node->type == 'product')
    {

        $_SESSION['product_node_nid'] = $node->nid;
        $banner = $_SESSION['banner'];
        $theme = $_SESSION['theme'];
        $params = array();
        $params['node'] = $node;
        $site_email = variable_get('site_mail', '');                
        if(($banner || $theme) || ($banner && $theme)) {
            drupal_goto('payment-approval');
        }
        else {
            //$alias = drupal_get_path_alias('node/'.$node->nid);
            module_load_include('inc', 'pathauto'); // include the inc file for Pathauto module
            pathauto_create_alias('node', 'insert', 'node/'.$node->nid, array('node' => $node), $node->type, $language->language); // Language is optional
            $alias = drupal_get_path_alias('node/'.$node->nid);
            //$alias = drupal_get_path_alias('node/'.$node->nid); print_r($alias); exit();
            drupal_goto($alias);
        }
    }
}
  • What are the circumstances that would require this, exactly? The default action after submitting the node add form is already to redirect to the new node. Use drupal_goto() with care in hooks; it can interfere with cron runs if it ever gets executed by cron. – Les Lim Nov 16 '13 at 6:44
  • As LesLim said, there is default functionality of node module to redirect to node/[nid] when new node be save. What you want to achieve with your code, its not clear. Please update the complete scenario, so you will find the correct solution. – Jayendra Kainthola Nov 18 '13 at 11:58
  • [Updated code] is the one which i am trying. – Ram Nov 18 '13 at 12:07
2

Third variable for module_load_include() function is missing in your code.

You should use code module_load_include('inc', 'pathauto', 'pathauto'); instead of module_load_include('inc', 'pathauto');

There is another method for your problem that you can use hook_form_alter instead of hook_node_insert hook in your module.

hook_node_insert will execute if you will insert node using any other method like importing node using feeds module.

Update Code:

function custom_feature_node_insert($node) {

  $options = array('language' => pathauto_entity_language('node', $node));

  //$alias = drupal_get_path_alias('node/'.$node->nid);
  module_load_include('inc', 'pathauto', 'pathauto'); // include the inc file for Pathauto module

  pathauto_create_alias('node', 'insert', 'node/'.$node->nid, array('node' => $node), $node->type, $options['language']); // Language is optional
  $alias = drupal_get_path_alias('node/'.$node->nid);

  drupal_goto($alias);

}
  • tried this one module_load_include('inc', 'pathauto', 'pathauto'); instead of module_load_include('inc', 'pathauto'); still it is redirecting to node/nid not alias name – Ram Nov 18 '13 at 12:30
  • @Ram, Have you checked $alias in your function before drupal_goto() function. – Jayendra Kainthola Nov 18 '13 at 12:38
  • this is wat print_r($alias) is printing node/212 – Ram Nov 18 '13 at 12:48
  • @Ram, it mean pathauto_create_alias() function is not working in your condition. – Jayendra Kainthola Nov 18 '13 at 12:49
  • pathauto_create_alias() first parameter module i can give module only know ??? i gave node is tat correct rite ? then wats the issue? – Ram Nov 18 '13 at 12:50
2

I checked the drupal_goto and found out that you must provide the path for parameter. You can't use alias for drupal_goto. So i think you can solve your problem like that:

  1. While you create node. You will also create the alias for that node. The reference for create the alias for node here. In your code just normally redirect like that

    drupal_goto('node/'.$node->nid);
    
  2. After that you will install Global redirect module. This module will have responsibility to redirect your node/nid to your node's alias

[Updated]

Can you try to generate the alias by using the Pathauto. So can you put these code in hook_insert()

function yourmodule_node_insert($node){
  module_load_include('inc', 'pathauto');
  pathauto_cleanstring($node->title);
  $node->path = array('pathauto' => TRUE );
  drupal_goto('node/'.$node->nid);
}
  • Tried installing Global redirect module, gave like this drupal_goto('node/'.$node->nid);still redirects to node/nid only – Ram Nov 16 '13 at 6:22
  • in hook_node_insert will node alias will get created or is it after tat only is the question.Because its only redirecting with node/nid. – Ram Nov 16 '13 at 6:31
  • module_load_include('inc', 'pathauto'); pathauto_create_alias('node', 'insert', 'node/'.$node->nid, array('node' => $node), $node->type, $language->language); $alias = drupal_get_path_alias('node/'.$node->nid); drupal_goto($alias); Still it goes to node/nid only. – Ram Nov 16 '13 at 9:40
  • Can you check the alias is created or not $alias = drupal_get_path_alias('node/'.$node->nid); print_r($alias); exit(); – Stone Vo Nov 16 '13 at 16:48
  • Its printing node/207 this only. Not alias name – Ram Nov 18 '13 at 4:14
1

drupal_get_path_alias() expects a full internal Drupal path, like this:

drupal_get_path_alias('node/23');

However, you're only giving it an NID, which is being evaluated like this:

drupal_get_path_alias(23);
  • See my updated question. i tried like that, it still goes like this only node/nid – Ram Nov 16 '13 at 6:13
  • Hmm, okay. I'm guessing you're using Pathauto, which also uses hook_node_insert() to generate the URL path for the node. Your hook is probably running before Pathauto does its work. – Les Lim Nov 16 '13 at 6:38
  • You can adjust your module's weight to ensure that it runs its hooks after Pathauto, or you can implement hook_module_implements_alter() to change the execution order for a particular hook: drupal.stackexchange.com/a/9720/23553 – Les Lim Nov 16 '13 at 6:47

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.