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
- Create path alias programatically when node is created
- How To Set Node Path/Alias Programatically
- 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);
}
hook_nodeapi()
isn't a valid function in 7.x. Please read about the 7.x equivalent(s) at this link: drupal.org/update/modules/6/7#remove_op. Also please note the limitations ofhook_insert()
as noted by @kiamlaluno here: drupal.stackexchange.com/questions/13170/….7