change text on specific webform submit button

harro - April 30, 2007 - 21:33

Hi all,

I am trying to change the text on the submit button of a specific webform. Changing the text on alle buttons is easy, by modifying th module a bit, but how to do it for one specific form?

This posts have a few pointers for changing the button to an image: http://drupal.org/node/62647

This one looks promising, describes how to theme your webform: http://drupal.org/node/79086 But this returns a blank page for me when I implement it in Drupal 4.7.x. Somehow the function is not called properly, or, when you modify part of the form, it loses the rest of the content?

If anyone can give me pointers as to how it would be possible to modify the text of the submit button in just on specific webform... you'd make my day :)

Thank you,

Harro

Example of what I tried to use in my template.php in my theme folder:

function phptemplate_webform_form_23 ($form) {
  $form = _phptemplate_callback('webform_form_23', array('form' => $form));

// I tried the two methods below, (not at the same time) but both returned a blanc schreen.

// option #1:
  $form['field']['submit'] = array ( '#type' => 'submit', '#value' => t("my text"),  '#weight' => 23, );
//option #2:   
  $form['submitbutton'] = array('#type' => 'submit','#value' => t('my text'),'#weight' => 1000,  );

  return _phptemplate_callback('webform_form_23', array('form' => $form));
}

value is passed to the webform, but nothing appears

harro - April 30, 2007 - 23:04

I have advanced to the point where the text on the submit button is changed in the $form array (I can print the content on the webform page) but the webform itself does not load ! It shows only the description text that belongs to the specific webform.

Maybe that makes it clearer what might be missing in my approach? The variable $form[submitbutton]['#value'] did show the 'default' text for my submit buttons, before I changed the value. Somehow changing the $form array, causes the webform to stop processing itself... why?

<?php
function phptemplate_webform_form_23 ($form) {
   
$form[submitbutton]['#value']='My text here';
    echo
'hi, this is the submit-button text value: '.$form[submitbutton]['#value'];
   
//print_r($form[submitbutton]);
   
return _phptemplate_callback('webform_form_23', array('form' => $form));
}
?>

And after yet another while

harro - April 30, 2007 - 23:50

And after yet another while of tinkering, I discovered that you MUST have the (in my case) webform_form_23.tpl.php in the same directory, even if it is completely empty... Now my form is visible again, even with the 'new' value entered. Babysteps.

On thing that still doesn't work is that, although I defined a new text for the submit button AND it arrives at the webform, it is just plainly ignored! Just used the 'normal' submit text. Printing the $form does not even show the code for the submit button!

okay, check out the differences:

Without defining the text on the submit button, the last part of the $form is:
[submitbutton] => Array ( [#type] => submit [#value] => Doorgaan [#weight] => 1000 [#tree] => [#parents] => Array ( [0] => submitbutton ) [#processed] => [#description] => [#attributes] => Array ( ) [#required] => [#input] => 1 [#name] => op [#button_type] => submit [#executes_submit_callback] => 1 [#id] => edit-submitbutton ) [#theme_used] => 1 [#value] => ) 

when I DO define the text on the submit button, I find this:

[submitbutton] => Array ( [#type] => submit [#value] => Ik meld me af als expert [#weight] => 1000 [#name] => op ) [#theme_used] => 1 [#value] => )

I tried to match all the array variables, but even then, the custom values are just ignored... any ideas why?

...

mooffie - May 1, 2007 - 15:07

The correct way to change a button label is via hook_form_alter:

<?php
function mymodule_form_alter($form_id, &$form) {
  if (
$form_id == 'webform_client_form'
     
&& $form['details']['nid']['#value'] == 23) {
   
$form['submitbutton']['#value'] = t('My personal label on this very personal button');
  }
}
?>

Create a 'mymodule' module and place this code there.

(Note that some evil modules can only work when the text on the button is 'Submit'. Let's hope you'll never encounter them.)

=====

There are two problems with your code.

Your themeing function is not supposed to have any effect on the button text. If you want to know why, ask.

It's possible to change your themeing function so that it has the effect. However, changing a button text in the themeing phase is wrong. It would work in your case, I'm afraid, but it's very wrong to do this there. If you want to know why, ask.

$form[submitbutton]['#value']='My text here';

(BTW, do ['submitbutton'], not [submitbutton].)

so... why?

harro - May 1, 2007 - 22:17

Hi mooffie,

thank you for your clear response. Since you tickeld my interest I will ask you "why" on the first question (why theming is not supposed to have effect on the button text).

The part about using quotes for array variables, I bumped in to already while I was searching the Drupal documentation on the site (and yes, I overlooked using the quotes afterwards...).

For one moment I was about to kick myself when you suggested it might work if I added the quotes... that would have saved me the better part of a day. However... :) It doesn't work (for me), even with the quotes.

I will try the code when the dust settles a bit - have to deliver the site for first inspection tomorrow, so for now I changed the submit text to something generic enough to fit in all my forms, yet not be just "submit".

Once again thanks, it is an eye-opener!

Harro

Question #1: Why changing button labels through theming is bad

mooffie - May 2, 2007 - 09:43

First I'll answer the more important question: why a theme function is the wrong place to change a button label.

In HTML, typical Drupalish buttons look like:

<input type="submit" name="op" value="Submit" />
<input type="submit" name="op" value="Preview" />

Now,

When you click a button, the browser sends its name and value back to the server so that the script running there know which button was pressed. For example, if you click the "Preview" button, your Drupal module would see op=Preview and know you want to preview the form.

The notorious "Form API" too see this the "op=" parameter. In order to decide which button was pressed, it compares this 'op' against the '#value's of all the buttons in the $form array.

Now,

The problem is that the Form API is oblivious to anything you do in the theming phase. This phase is carried out after the Form API has finished its working. So you change the label of the button there, but only your browser sees this change. When Form API sees the "op=My Modified Label" the browser sends it doesn't know which button this label belongs to. It never see a button with such label. The theming phase is too late a place for changing button labels (but not for changing labels of almost anything else).

Notes:

  1. Actually, in your specific case, because there's only one button on the form, you _can_ change the label on the theming phase. It works because Form API contains code that circumvents a bug in Internet Explorer where the "op=" is not sent (by the browser) in some cases.
  2. Some buttons you can't change their label (well, you can, but only through the 'locale' mechanism). This happens when a module examines the "op=" value to determine which button was clicked. (It's possible to write code in a way that allows a button label to change (if ($form_value['op'] == $form_value['preview']) {}), but the current "conventions" in Drupal are a bit dumb (if ($form_value['op'] == t('Preview') {})

Question #2: why your theming function didn't change the label

mooffie - May 2, 2007 - 09:59

Your function didn't change the label because you didn't ask drupal, in this function, to draw the button. Because this function terminates without drawing anything, Drupal continues to do so itself. Whatever modifications you make to $form elements inside this function are forgotten (maybe if you did &$form instead of $form, as the parameter, if wouldn't be forgotten, but it's wrong nevertheless).

You should have written the function thus:

<?php
function phptemplate_webform_form_23($form) {
 
$form['submitbutton']['#value'] = 'My text here';
  return
drupal_render($form); // draw the form
}
?>

But don't do it. Delete this function. I just explained why it's not a good idea to change button labels in theming functions.

Eye-opener

harro - May 7, 2007 - 03:41

Moofie, thank you for taking the time to write this. It is an eye-opener! The process is a lot clearer now (for this particular part of drupal only, of course) and I understand your last point about doing something with the manually modified data.

The next step is to have a look at connecting to the API and doing things the right way.

Bye!

(happy) Harro

How about this workaround?

drumdance - September 7, 2007 - 19:35

(Found this thread after a bit of Googling...)

I understand why the above is verboten, but how about this workaround? I really want to be able to change the label of the button. I just tested this and it works:

<?php
function phptemplate_webform_form_23($form) {
   
$form['submitbutton']['#type']='hidden'; //change the button's type to hidden to preserve the element => value binding
   
$form['my_submitbutton']=array('#type'=>'submit',
                                  
'#value'=>'My new submit text',
                                  
'#weight'=>100); //set weight to a high number to make sure it's at the bottom of the form
   
return drupal_render($form);
}
?>

I believe this will work fine with webform because (as far as I know) it only generates one submit button per form. On a node form that has multiple buttons for the same variable 'op', this will cause problems.

---
The following sentence is true.
The preceding sentence is false.
http://www.AskDerekScruggs.com

Confirmed as working. This

kees@qrios - October 4, 2007 - 10:37

Confirmed as working. This is the nicest way to change the buttontext I tried so far.

Thanks for sharing!!

Webbased applicaties, content management systemen, websites, webdesign

I have tried this to no avail

Feonyx - November 21, 2009 - 22:00

I have spent about 2 hours working on this and have given up searching for answers and am going to just go ahead and ask for help :)

The code snippet in my page-rewards.tpl.php:

<?php
       
function phptemplate_webform_form_2 ($form) {
           
$form['submitbutton']['#type']='hidden'; //change the button's type to hidden to preserve the element => value binding
           
$form['my_submitbutton']=array('#type'=>'submit',
               
'#value'=>'My new submit text',
               
'#weight'=>100); //set weight to a high number to make sure it's at the bottom of the form
           
return drupal_render($form);
        }   
   
?>

The output on the rendered page:

    <form action="/drupal/rewards"  accept-charset="UTF-8" method="post" id="webform-client-form-2" class="webform-client-form" enctype="multipart/form-data">

      blah blah...

      <input type="hidden" name="form_id" id="edit-webform-client-form-2" value="webform_client_form_2"  />
      <input type="submit" name="op" id="edit-submit" value="Submit"  class="form-submit" />
    </form>

For the life of me I can not get the text on the submit button to change. I have searched and tried about 5 different methods including editing the specific CSS file... hook_form_alter etc... creating a "webform-form-2.tpl.php" file and using:

$form['submitbutton']['#value'] = 'My text here';
print drupal_render($form);

Thanks much in advance,

Feonyx

PS. Yes I have cleared cached data... even though I don't have caching turned on.

If you're on D6 (cant imagine

kees@qrios - December 10, 2009 - 10:27

If you're on D6 (cant imagine building new projects in D5 now), you can set the button text per webform in the "advanced" settings fieldset.

D6 theme registry is rebuild upon submitting the theme admin form /admin/build/themes

 
 

Drupal is a registered trademark of Dries Buytaert.