3

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);
    }
4

4 Answers 4

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, '');
2
  • hook_insert() is for modules implementing a content type.
    – avpaderno
    Commented May 9, 2016 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. Commented Sep 19, 2017 at 14:55
6

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.

11
  • I tried your code in hook_node_insert but it didn't work!
    – M a m a D
    Commented Aug 26, 2015 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
    Commented Aug 26, 2015 at 7:27
  • No I just created a new node and checked the url via browser
    – M a m a D
    Commented Aug 26, 2015 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
    Commented Aug 26, 2015 at 7:54
  • 2
    Just to clarify: you can't implement node CRUD hooks in a theme @Drupalist
    – Clive
    Commented Aug 26, 2015 at 9:49
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.

1

I know this is an old post, but I just discovered a great way to do this for a Drupal 7 site:

  1. In hook_node_presave, set $node->path['pathauto'] equal to 0 if $node->original is not yet set (that is, if this is a node insert and not a node update). This prevents an automatic alias from being applied if one normally would.
  2. In hook_node_insert', create the new path usingpath_saveand make sure the source isnode/nid`.

Here is an example for a custom module called mymodule assuming you want this for all new mytype nodes with a 'my_fieldvalue not equal to1, and that you want to customize the alias asmycustomalias/my_field/nid`:

function mymodule_node_presave($node) {
  // Add any conditions that determine whether this node needs a custom alias.
  if (
    $node->type == 'mytype' &&
    !isset($node->original) &&
    isset($node->field_my_field['und'][0]['value']) &&
    $node->field_my_field['und'][0]['value'] != 1
  ) {
    $node->path['pathauto'] == 0;
  }
}

function mymodule_node_insert($node) {
  // Add the same conditions as you had in mymodule_node_presave, except you don't need to check for $node->original.
  if (
    $node->type == 'mytype' &&
    isset($node->field_my_field['und'][0]['value']) &&
    $node->field_my_field['und'][0]['value'] != 1
  ) {
    // Create a $new_path array that contains the source of your node and the custom alias you want.
    $new_path = [
      'source' => 'node/' . $node->nid,
      'alias' => 'mycustomalias/' . $node->field_my_field['und'][0]['value'] . '/' . $node->nid],
    ];
    // Save the path. By using the variable above rather than putting the array into the function call, you avoid PHP errors.
    path_save($new_path);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.