1

I need to set a special path alias for nodes created by users in the template.php file of my theme. the pathauto module is enabled. I already tried the solutions that are given in the following links

  1. Create path alias programatically when node is created
  2. How To Set Node Path/Alias Programatically
  3. Programmatically set path alias

But none of them worked. Here is a few codes I tried but I failed.

function THEME_nodeapi(&$node, $op, $a3=NULL, $a4=NULL){
    if($node->type == "image"){
        switch ($op){
            case "insert":
                $default_node_path = 'node/'.$node->nid;
                $alternate_url = "SOME SPECIAL PATH";
                path_set_alias($default_node_path, $alternate_url, 0, '');
                break;
        }
    }
}

or this one

function THEME_insert($node){
    $path = "SOME SPECIAL PATH";
    $node->path = array('alias' => $path, 'pathauto' => FALSE);
}

function THEME_node_insert($node){
    $path = "SOME SPECIAL PATH";
    $node->path = array('alias' => $path, 'pathauto' => FALSE);
}

or

function THEME_nodeapi(&$node, $op, $a3=NULL, $a4=NULL){
    if($node->type == "image"){
        switch ($op){
            case "insert":
                $node->path = t("SOME SPECIAL PATH");
                break;
        }
    }
}

How can I do set path alias programmatically ?


UPDATE

I tried this code that recommended by @Ajit S and it didn't work again

<?php
    function THEME_node_insert($node){
        $alias = "arar/".$node->nid;
        $lang_code = "en";
        $path = array(
          'source' => "node/{$node->nid}",
          'alias' => $alias,  // Any alias that you want to set.
          'language' => $lang_code,  // Optional, if you are working on a multilingual site.
        );
        path_save($path);
    }
2

Use hook_insert

/**
 * Implements hook_insert().
 */
function mymodule_insert($node) {
    // Set the URL alias
    if (empty($node->path['alias'])) {
        $node->path['alias'] = 'slug/' . $node->nid;
    }
}

if url has no alias then we can create it

if( urlAlias == '' )
path_set_alias($default_node_path, $alternate_url, 0, '');
  • hook_insert() is for modules implementing a content type. – kiamlaluno May 9 '16 at 13:04
  • hook_insert() is for any module that wants to chip in when content is being saved. The example given for this is when adding a new content type that needs handling in the save process, but that is not the only use for the hook. – DeveloperWeeks Sep 19 '17 at 14:55
6

None of the hook_nodeapi implementations will work, that hook was removed in Drupal 7.

Your other attempts are failing because you're trying to implement the hooks in a theme - themes can implement theme hooks, hook_theme, and alter hooks only.

Move your code to a module, and you'll have better luck.

4

Path alias could be set using the path module in the core. Just call path_save the following from a function (or hook) of your choice. In your case I'd recommend you to implement the following code in hook_node_insert, because it is called after a node has been saved, and the node will have a valid nid.

// Assuming you have a $node object already.
$path = array(
  'source' => "node/{$node->nid}",
  'alias' => $alias,  // Any alias that you want to set.
  'language' => $lang_code,  // Optional, if you are working on a multilingual site.
);
path_save($path);

If you have global redirect set, the node page will be redirected to the alias.

  • I tried your code in hook_node_insert but it didn't work! – No one Aug 26 '15 at 7:16
  • Did you edit the node and check if the alias was set? Also, it would be good if you could verify if the entry was made in the url_alias table in the database. – AjitS Aug 26 '15 at 7:27
  • No I just created a new node and checked the url via browser – No one Aug 26 '15 at 7:36
  • Can you check by editing the node to make sure if the alias is set. Also, install globalredirect and try again. – AjitS Aug 26 '15 at 7:54
  • 2
    Just to clarify: you can't implement node CRUD hooks in a theme @Drupalist – Clive Aug 26 '15 at 9:49

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.