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
value is passed to the webform, but nothing appears
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?
And after yet another while
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?
...
The correct way to change a button label is via hook_form_alter:
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.
(BTW, do
['submitbutton'], not[submitbutton].)so... why?
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
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:
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=Previewand 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:
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
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
&$forminstead of$form, as the parameter, if wouldn't be forgotten, but it's wrong nevertheless).You should have written the function thus:
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
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?
(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:
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
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
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:
The output on the rendered page:
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
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
Thanks kees@qrios. That's
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
Perfect!
:-)
1 line jquery hack
$('#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.
Works great. Thanks
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' .
Drupal 7 solution
For Drupal 7 you need to use:
if you go the form_alter route.
This is also the solution in
This is also the solution in Drupal 6 using version 6.x-3.15 of webform
Custom submit button text, working solution for Drupal 7
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.
The theming route for core login form
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
Steps to do that ...
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).
I have solved this by
I have solved this by adding this function in my template.php file:
Where YOURTHEMENAME is obviously the name of the your theme and the WEBFORMID is the id of the specific webform.
I resolved my problem
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');
}
Webform 7.x-4.x-dev
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.