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

Comments

harro’s picture

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?

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));
}
harro’s picture

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’s picture

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

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].)

harro’s picture

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

mooffie’s picture

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') {})
mooffie’s picture

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:

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.

harro’s picture

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

drumdance’s picture

(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

keesje’s picture

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

Feonyx’s picture

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:

		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.

keesje’s picture

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

leahtard’s picture

Thanks kees@qrios. That's just the setting I couldn't find. I thought there should be an easier way to do this!

Cheers, Leah

stymied1’s picture

:-)

jonanthony’s picture

$('#forum-comments #edit-submit').val('Post');
If you don't mind the jquery hack this is quick, easy and puts less load on your server to process custom code.

tomvolek’s picture

Works great. Thanks JonAnthony. For beginners, just put this line in a file called foo.js in your themes js folder and make it look like:
Drupal.behaviors.aireaTreeView = function (context) {

$('#edit-submit-search-projects').val('Search');
};

The above will change the text of the submit button for element with id of edit-submit-search-projects to 'Search' .

BartVB’s picture

For Drupal 7 you need to use:

$form['actions']['submit']['#value'] = 'Submit Button Text';

if you go the form_alter route.

carlhinton’s picture

This is also the solution in Drupal 6 using version 6.x-3.15 of webform

blitux’s picture

Your solution does not work for me. Actually I did it with this line of code (drupal 7.15, webform 7.x-3.18):

$form['form']['actions']['submit']['#value'] = 'Custom Submit Text';

There is a 'form' parent item on the form array.

eawheeler’s picture

Thanks, BartVB. This is the solution that I needed. I've been attempting to change wording in forms through custom theming. While I was able to change certain attributes, a solution for the submit button was eluding me. Failing to include the 'actions' key was my downfall. In case it helps anyone, here's a working solution to change certain aspects of the login form in Drupal 7. I created the template.php file in the root directory of my custom theme and added the following function.

function hook_form_FORM_ID_alter(&$form, &$form_state) {

$form['name']['#attributes']['placeholder'] = t('add HTML5 placeholder text to the username field');
$form['pass']['#attributes']['placeholder'] = t('add HTML5 placeholder text to the password field');
$form['name']['#title_display'] = "invisible"; // remove the field title if you want
$form['pass']['#title_display'] = "invisible"; // remove the password title if you want
$form['name']['#description'] = t('add a description or instructions for the username field');
$form['pass']['#description'] = t('add a description or instructions for the password field');
$form['actions']['submit']['#value'] = t('Login'); // change the text of the submit button
}

Some of this solution comes from Dropol's post at https://www.drupal.org/node/1195754. Other useful resources for customizing core forms include:

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/func...
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h...
https://www.drupal.org/node/350634

ram_mere’s picture

Just to make it clear ...
1-create new file called webform-form-[nid].tpl.php ... where nid is the last number in the url when you view your form.

2-$form['actions']['submit']['#value']='[any value you want]';

3- print drupal_render_children($form); ...to render the what left of the form or ....print render($form).

jagadmarx’s picture

I have solved this by adding this function in my template.php file:

function YOURTHEMENAME_form_webform_client_form_WEBFORMID_alter(&$form, &$form_state) {

	$form['actions']['submit']['#value'] = t('Submit Your Application');  

}

Where YOURTHEMENAME is obviously the name of the your theme and the WEBFORMID is the id of the specific webform.

omzo’s picture

I resolved my problem ike the last comment before me (jagadmarx)
Just add this following function in your template file:
function Mythemename_form_webform_client_form_IDofmytheform_alter(&$form, &$form_state) {
$form['actions']['submit']['#value'] = t('My submit button name');
}

thirdender’s picture

I know this is seven years later, but Webform 7.x-4.x-dev now allows for the submit button text to be changed in the Webform form options. The above code options are all pretty much valid for many different form types, but it's now possible to change the submit button text for Webform forms easily through the UI.