This is a question about Drupal 6.6. I created a new content type, and would like to change the "save" button to say something different. How difficult would this be?

Would it be possible to change it just for a specific content type?

Andrew

Comments

greylogic’s picture

I guess, you may achieve this by
1. implement a hook_form and change the title of the submit button.
2. take a look at hook_nodeapi.

The code should be very short in both these cases. For doing it just for one content type, you need to check the form id and parameters or node type respectively.

--------------------------------------------------------------
My attempt with Drupal - Jaanlo.com

atreus’s picture

Ok, from a bit of googling it doesn't look....too hard. Would I have to create my own module to implement a hook?

Alan D.’s picture

Have a look at the examples on http://api.drupal.org/ these are a great resource.

You could always hack another module, but when you forget about it and upgrade that module, your changes are lost.


Alan Davison
www.caignwebs.com.au

Alan Davison
adshill’s picture

Hi Atreus,

Did you ever come up with the code for this? I'm really in need of it! If you could post it - would be fantastic... and save me a long time trying to work it out myself with my limited PHP knowledge!

Thanks,

Adam

cincy_kid’s picture

I have also been working on this for a couple of days now and have decided to go the module route.

1) Create a .module and .info file (you can learn how to do this here: http://drupal.org/node/206753)

2) From my research it looks like the hook_form_FORM_ID_alter (http://api.drupal.org/api/function/hook_form_FORM_ID_alter/6) would be better to use than the typical hook_form_alter. That way you can just affect 1 form instead of globally.

The code part is what I am having problems with. Being a PHP noob also, I am struggling with syntax.

Here is what I have (doesn't work currently)

function hook_form_FORM_ID_alter(&$form, &$form_state) {
  // Modification for the form with the given form ID goes here. For example, if
  // FORM_ID is "user_register" this code would run only on the user
  // registration form.

  // Change Text on Save Button
  // $form['my_new_form_node_form'] = array(
  //  '#title' => t("Save and add new"),
  // );
  
  	$form['submit']['#value'] = t("Save and add new")
}

As you can see I tried both the commented and uncommented code above but neither work. So unfortunately this is not your answer but hopefully it points you in the right direction and maybe someone with more experience than I can chime in and help us both with what I am missing.

cincy_kid’s picture

So I changed it to this:

  /**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
  function my_hook_module_form_alter(&$form, $form_state, $form_id) {
    // Normally a switch is used because you may want to alter more than
    // one form and it is easy to add a new case for each form.
    switch ($form_id) {
      // This is our form ID.
      case 'my_new_form_node_form':
        // Our kickin' mods go here.
		$form['submit'] = array(
  		'#type' => 'submit',
  		'#value' => t('Save and add new'),
		);
     break;
  }
}

Now, it actually adds ANOTHER submit button (at the very top of the form instead of the bottom next to the existing submit button) with "Save and add new" as the text instead of just replacing the current submit button text. So it appears my code is adding new instead of modifying the existing one. I am getting closer!

I tried viewing source of the page and getting the id of the current submit button and doing something like:

$form['edit-submit']['#value'] = t('Save and add new');

but that didn't work either...

Anyone want to tell me what I am doing wrong?

TIA

Alan D.’s picture

Try <?php $form['buttons']['submit']['#value'] = t('Save and add new'); ?>


Alan Davison
www.caignwebs.com.au

Alan Davison
cincy_kid’s picture

Thanks Alan!

I still dont understand how it knows to change the text on that specific button without supplying some sort of ID, but heck it works and I am happy about that!

I mean, if there was a Save and Edit and cancel button, i am guessing they have different IDs?

Thanks again ~

Alan D.’s picture

If you have a look at the native node form:

<?php
/**
 * Generate the node add/edit form array.
 */
function node_form(&$form_state, $node) {

  // ### CUT ####

  // Add the buttons.
  $form['buttons'] = array();
  $form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#access' => !variable_get('node_preview', 0) || (!form_get_errors() && isset($form_state['node_preview'])),
    '#value' => t('Save'),
    '#weight' => 5,
    '#submit' => array('node_form_submit'),
  );
  $form['buttons']['preview'] = array(
    '#type' => 'submit',
    '#value' => t('Preview'),
    '#weight' => 10,
    '#submit' => array('node_form_build_preview'),
  );
  if (!empty($node->nid) && node_access('delete', $node)) {
    $form['buttons']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#weight' => 15,
      '#submit' => array('node_form_delete_submit'),
    );
  }
  $form['#validate'][] = 'node_form_validate';
  $form['#theme'] = array($node->type .'_node_form', 'node_form');
  return $form;
}

?>

You can see how they are created. All ids get "edit-" added and underscores are converted to dashes during the theming stage after form alter.

:)


Alan Davison
www.caignwebs.com.au

Alan Davison
cincy_kid’s picture

for the explanation....

:)

wims’s picture

Read this interesting tread.

I woult like to change the preview into a Logout button.
Strubbling around the idee came up that the action after pressing a buttons seems to be in the form array (button array) to.

So was thinking the next could work, but didn't:

// cange the button caption
$form['buttons']['preview']['#value'] = t('LogOut');
// change the button action
$form['buttons']['preview']['#submit']['0'] = t('user_logout()');

Is this possible or am i thinking in a rong direction.

Thanks in advance
W!m

Open University of the Netherlands (www.ou.nl)

Alan D.’s picture

You could try:

$form['buttons']['preview']['#value'] = t('LogOut');
$form['buttons']['preview']['#submit'] = array('my_logout');

and define the submit callback

function my_logout() {
drupal_goto('logout');
}

The problem with "$form['buttons']['preview']['#submit']['0'] = t('user_logout()');" is:

a) you should not use the brackets or t function (just "$form['buttons']['preview']['#submit']['0'] = 'user_logout';")
b) there is the potential for multiple submit handlers, so rather than [0] and overriding the first, fully overwrite, "$form['buttons']['preview']['#submit'] = array('user_logout');"
c) Lastly, the function 'user_logout' is not included in a *.module and needs to be included. So rather than including the file and calling it directly, I simply used drupal_goto to let drupal do this for me, by calling the menu callback

Sorry for more info than required, but each point may help you in the future.


Alan Davison
www.caignwebs.com.au

Alan Davison
amariotti’s picture

I'm sitting here trying to the same thing as these other guys and I'm getting nothing. Here's my function from my submit_button.module:

  /**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
  function submit_button_form_alter(&$form, $form_state, $form_id) {
    // Normally a switch is used because you may want to alter more than
    // one form and it is easy to add a new case for each form.
    switch ($form_id) {
      // This is our form ID.
      case 'my_new_form_node_form':
        // Our kickin' mods go here.
        $form['buttons']['submit']['#value'] = t('SUBMIT');
     break;
  }
}

I'm assuming that it has something to do with either the function name or the case in the switch statement. Thanks for your help!

--amariotti

Alan D.’s picture

Put in some debugging steps:

<?php
  /**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
  function submit_button_form_alter(&$form, $form_state, $form_id) {
    // Normally a switch is used because you may want to alter more than
    // one form and it is easy to add a new case for each form.
   
// Find out if I'm called and if the id is right...
var_dump($form_id);
    switch ($form_id) {
      // This is our form ID.
      case 'my_new_form_node_form':
        // Our kickin' mods go here.
        $form['buttons']['submit']['#value'] = t('SUBMIT');
     break;
  }
}
?>

And is your module called "submit_button"?


Alan Davison
amariotti’s picture

Yeah.

I figured out where my problem was. His "case my_new_form_node_form" was for his "my_new_form" content type (strange content type, actually).

I changed it to resume_node_form and it works. Now I can customize it on a per content type basis, which is exactly what I wanted from the beginning. Thanks for your help on this discussion.

leewoodman’s picture

thanks alan that worked for me

georgechenley’s picture

Alan, nice solution, thanks!

big_ham’s picture

If you want to change it throughout the site, check out:

http://drupal.org/project/stringoverrides

This was the easiest mod I've ever used. I hid the "preview" button with CSS, then substituted "Submit" for "Save" using String Overrides.

vertazzar’s picture

yea but if you want different name for button for different form.. that isnt possible with that module

Alan D.’s picture

Rather than adding a module for this, you can update the strings in the settings.php. Look at the instructions in the settings.php file towards the bottom. As a general rule, less modules == less general overhead, which is always good :)


Alan Davison
TapSkill’s picture

This doesn't help if you only want to change the value given to buttons. You still need the function shown above (in previous posts). I'm thankful to the community for actively supporting common problems and solving most of them. I had forgotten how to get button text changed, and this thread helped.

---
I have created and maintained countless Drupal-powered sites and have made heavy modifications to modules on a site-by-site basis. I am an illustrator, a game developer, and a web developer. I also stream on Twitch in my spare time.

fnikola’s picture

Replace custom_type with your content type machine-readable name and replace theme name with your theme (replace basic with your theme name).

<?php
function basic_theme() {
  return array(
    'custom_type_node_form' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

function basic_custom_type_node_form($form) {
  $form['buttons']['submit']['#value'] = t("New button text");
  return drupal_render($form);
}
?>
hixster’s picture

tried this and the result is:

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'fusion_starter_myform_node_form' was given in /var/www/vhosts/xxxxxxx/httpdocs/xx/includes/theme.inc on line 656.

Button position is moved further up the form, but the text doesn't change?

Drupal Web Designers Surrey South East England www.lightflows.co.uk

Stoob’s picture

Laff! For all you folks pounding your head on the Drupal module wall, here is a way that needs no template changes, no new modules.

1. Create a block
2. Put that block in the footer or somewhere below the form
3. Set the block to only show up on the particular URL where you want the button changed... for instance click "Show on only the listed pages." and then put in the box "node/*/edit" or whatever you want
4. set the block body to FULL HTML input mode
5. put this in the block body
<script language="javascript">document.getElementById("edit-submit").value="NEW BUTTON TEXT";</script>

Tested and works in IE6, IE7, IE8, Firefox, Chrome. Assuming it works in Opera and Safari, but I don't have them browsers installed at the moment.

mrbert’s picture

quardz’s picture

Wow!. Smart way!!!!!!!!!!!

masher’s picture

On Safari if the form does not validate the text change moves to the next button (in my case the Search button)

Can "edit-submit" be a path in someway? e.g. "node-form.edit-submit"

Any java-scripters??

jdln’s picture

The template.php seems the simpist method but the code gives me a white screen.

Myx.Ostankin’s picture

Thanks a lot mate, was looking for an elegant way to customize Save button text for quite a while, and yours is just perfect!

Sepero’s picture

Change the text on Comment forms from 'Save' to 'Post'

First, create the folder sites/all/modules/comments_save_button

In that folder create 2 files:

Create one file "comments_save_button.info" with this text

; $Id$
name = "Comments Save Button"
description = "Comments Save Button"
core = 6.x
package = Other

Create a second file "comments_save_button.module" with this text

<?php
  /**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
  function comments_save_button_form_alter(&$form, $form_state, $form_id) {
    // Normally a switch is used because you may want to alter more than
    // one form and it is easy to add a new case for each form.
  
// Find out if I'm called and if the id is right...
//var_dump($form_id);  // Uncomment to print every form id name
    switch ($form_id) {
      case 'comment_form': // This is our form ID.
        //print_r($form); // Uncomment to print all form information
        $form['submit']['#value'] = t('Post');
        break;
//      case 'another_form': // This is our form ID.
//        //print_r($form); // Uncomment to print all form information
//        $form['buttons']['submit']['#value'] = t('Post');
//        break;
  }
}
?>

Then go into your modules and enable your new module called "Comments Save Button"

Narek’s picture

It doesn't work for me,,, and it gives me the below error
string(10) "user_login"
Does any one have any idea what does this mean?~ thank you.

in D7 the following statement works for me :

$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));

JonAnthony’s picture

This works well if you don't mind a dirty jquery hack
$('#forum-comments #edit-submit').val('Post');

adellefrank’s picture

First, create the folder sites/all/modules/change_default_button_values

In that folder create 2 files:

Create one file "change_default_button_values.info" with this text

; $Id$
name = "Change Default Button Values"
description = "Custom module to change button values, such as the submit button, on Drupal's default forms and content types."
core = 6.x
package = Other

Create a second file "change_default_button_values.module" with this text

<?php
  /**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
  function change_default_button_values_form_alter(&$form, $form_state, $form_id) {
    // Normally a switch is used because you may want to alter more than
    // one form and it is easy to add a new case for each form.
 
// Find out if I'm called and if the id is right...
//var_dump($form_id);  // Uncomment to print every form id name
    switch ($form_id) {
      case 'mytype_node_form': // The machine name of our content type, 'mytype', with '_node_form' at the end.
        //print_r($form); // Uncomment to print all form information
        $form['buttons']['submit']['#value'] = t('Post'); //wrapping text in t('text here'); code is an important security & multi-lingual coding step.
        $form['buttons']['preview']['#value'] = t('Review');
        $form['buttons']['delete']['#value'] = t('Trash');
        break;
//      case 'anothertype_node_form': // This is another form's ID.
//        //print_r($form); // Uncomment to print all form information
//        $form['buttons']['submit']['#value'] = t('Post');
//        $form['buttons']['preview']['#value'] = t('Review');
//        $form['buttons']['delete']['#value'] = t('Trash');
//        break;
  }
}
?>

Then go into your modules and enable your new module called "Change Default Button Values".

Thanks to Alan D. for the above comment with more detailed information on button names and properties, to moon.watcher for hints on more detailed button-setting code, and to Sepero whose detailed example code let me figure this out faster!

Alan D.’s picture

"wrapping text in t('text here'); code is an important security & multi-lingual coding step."

For contributed modules, yes this is important for multi-lingual support. Security, no. It is just a wrapper for translation and other cool things like the string overrides. It does not sanitise the string.

For Drupal 6 be careful not to nuke the content type settings page at the same time (the above targets both).

Use:

/**
 * Implementation of hook_form_alter(). Adds fields to the node form.
 */
function change_default_button_values_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
    if ($form['#node']->type == 'type') {
      ...
    }
  }
}

Alan Davison
kt-designs’s picture

Hi Sepero,
I followed your instructions exactly in my Drupal Commons 1.7 site (based on Drupal 6.x). It worked perfectly. I was able to produce the requested change with your solution immediately. Thanks for contributing!!
Kathy

-Mania-’s picture

These threads are a bit hard to follow sometimes with the number of posts and corrections made. I recently wrote about changing the 'save' button text and other form submit buttons in my blog which will hopefully help some of you that stumble here looking for answers.

spineless’s picture

I have created a new module for myself to change the comment "save" button to read "Save and continue". However the button has not changed. I am not sure why.

I took a look at the core module "comment.module" and found the "save" action located at line 2030. I created the following function to alter this text.

 function mymodule_cf_form_alter(&$form, &$form_state, $form_id) {
    switch ($form_id) {
         case 'comment_form': 
            $form['actions']['submit'] = array(
               '#type' => 'submit',
               '#value' => t('Save and continue'),      
  );
       break;
  }
}

This did not work. I also tried other variation similar to the comments above but the text is not being changed. What am I missing ?

Thanks,
spineless

stupiddingo’s picture

@Spineless - you're likely missing the correct form_id of the comment form.

Typically it includes your entity and content type in the form id.

You can check form_id's by writing them out inside a form_alter, e.g.

function mymodule_form_comment_form_alter(&$form, &$form_state, $form_id){
  // Print out Form_IDs
  drupal_set_message($form_id);

  switch ($form_id) {
    // Modify Article Comment Form
    case 'comment_node_article_form':
      $form['actions']['submit']['#value'] =t('comment');
      break;
  }

}
thejamesjones’s picture

I tried this and worked great for D7 https://drupal.org/project/node_buttons_edit

icf-vpathak’s picture