Community

Add some additional text to $form['title'] when saving

Hi All

Been a while but could do with a hand please,

I ve created a custom node-edit.tpl for a content type in my theme and I have now figured out how to move things where I want them on the screen and to hide stuff I dont want (Very cool to be able to do this and design my own add/edit pages now I know how), all working great so moving onto the next piece of learning and thats where I could do with your help please.

I would like to know how to add some additional text to the user input of a title (or any other field for that matter using code). Im using drupal 6 for this project.

so if my user inputs 1234 how would i get it to save item1234 into the database instead of just 1234

Also just wanted to say how cool drupal is, I wrote the website www.policecrimecommissioner.co.uk for a client as everything I read told me drupal could handle high volumes of traffic and would be a good choice, that site has had nearly 1m hits in 2 months and it has worked great throughout so thanks guys. I am now a total convert, everything I do will now be using drupal as the platform as nothing else i ve tried even comes close now i ve figured out most of what i regularly need to do, fairly steep learning curve to start with but well worth the effort.

Gibbo

Comments

You could try using Auto Node

You could try using Auto Node Titles

Many Thanks for the reply but

Many Thanks for the reply but im trying to learn how to do this with code as might not just be the title field i want to do this with and im trying to get my head around some more of the coding/theming side of drupal, any ideas please

thanks

gibbo

You can code a custom module

You can code a custom module as
Drupal 6:

<?php
MYMODULE_nodeapi
(&$node, $op, $a3 = NULL, $a4 = NULL){
    if(
$op == 'presave'){
        if(
$node->type == 'MY_CONTENT_TYPE_NAME'){
             
$node->title = 'test'.$node->title;
        }
    }
}
?>

This is implementation of hook_nodeapi refer http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_nodeapi/6

Drupal 7:

<?php
function MYMODULE_node_presave($node){
    if(
$node->type == 'MY_CONTENT_TYPE_NAME'){
       
$node->title = 'test'.$node->title;
    }
}
?>

This is implementation of hook_node_presave refer http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_presave/7

You should try drupal 7 it has many new features than drupal 6.

Hth,
Sadashiv.

Many Thanks, will give it a

Many Thanks, will give it a go

CCK fields

Made a module to do this and it worked great, I have two more questions if i may

1. How would i get the current value of my title field and then add the input text before what is already in the title field (i.e. if i had my title and i want to add the text this is how would i get this is my title into the database please)

2. I have a cck text field called field_update, how do I save to that field as .field_update doesnt seem to work?

Update to 2 above for others: you need to add [0]['value'] at the end so becomes

<?php
MYMODULE_nodeapi
(&$node, $op, $a3 = NULL, $a4 = NULL){
    if(
$op == 'presave'){
        if(
$node->type == 'MY_CONTENT_TYPE_NAME'){
             
$node->.field_update[0]['value'] = 'test'.$node->.field_update[0]['value'];
        }
    }
}
?>

This works for a multiline text field so may be different for other types of field so would be good to know where to find that information if that is the case

Many thanks for all your help

Paul

1) You may change to code

1) You may change to code to

<?php
MYMODULE_nodeapi
(&$node, $op, $a3 = NULL, $a4 = NULL){
    if(
$op == 'presave'){
        if(
$node->type == 'MY_CONTENT_TYPE_NAME' && substr($node->title,0,4) !='test' ){
             
$node->title = 'test'.$node->title;
        }
    }
}
?>

to avoid apending test again.

2) I think it is $node->field_FIELD_NAME[0]['value'] to update.

Hth,
Sadashiv.

This

This $node->.field_update[0]['value'] has a period before field that should not be there.

nobody click here