0

I want to append the date and time to node titles. For example, If the title is Test, I want to change it to Test_DDMMYY_hhmmss.

How can I achieve this?

3 Answers 3

3

This will do for you

function modulename_node_presave($node) {
      if($node->type == 'node_type_machine_name') {
        $new_date = date("DMY_hms", time());
        $node->title = $node->title."_".$new_date;
      }
}
2
  • Where am i need to add this?
    – Sam1604
    Feb 10, 2015 at 6:52
  • if you had custom module you can add it there Feb 10, 2015 at 7:01
3

Check out the Automatic Nodetitles module to do it without code. In combination with the Token module you can build complex patterns.

1
  • Is there is any option to append the entered title with date?
    – Sam1604
    Feb 10, 2015 at 8:46
2

I think you want to use hook_node_presave(). This should get you started.

function mymodule_node_presave($node){
  if ($node->type == "my_content_type") {
    $node->title = $node->title . "_" . date('dmy_his');
  }
}

Though I am only inferring that you want date and time of when the node was saved.

8
  • I am new for Drupal. May i know where am i need to add this Function?
    – Sam1604
    Feb 10, 2015 at 6:59
  • You probably want to put it in a module. In the modules directory, you must create a folder for your module. In this example I would have a my_test folder, with files named my_test.module and my_test.info. This function would go in the my_test.module file.
    – TerryCB
    Feb 10, 2015 at 7:03
  • i just copied the function in my_test.module file but its not working
    – Sam1604
    Feb 10, 2015 at 7:13
  • in your info declare these name = Test module package = Custom Modules description = Module for LO Admin related code core = 7.x files[] = my_test.module Feb 10, 2015 at 7:18
  • @Sam1604, I believe there's a bare minimum you need in the .info file: name = My Test Module description = Test module core = 7.x Then of course you must enable the module.
    – TerryCB
    Feb 10, 2015 at 7:18

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.