If I use hook_form_alter to change the label of the Save button of comment forms, it is impossible to submit comments.

This is because in function comment_form it decides whether to show the submit button by checking a bunch of conditions, one of which is $op == t('Preview') || $op == t('Save'). If I change the label of the button, the value of $op is different so the button is not added. This in turn seems to mean that the submit handler is not called.

My workaround for this is that in the hook_form_alter routine, if there is no form item named submit, then it adds submit element with #disabled set to true and #prefix and #suffix set to wrap it in a span element that is set display:none.

When I create a form with multiple action buttons, I give them different name attributes and then test for the presence of a form variable namedop-preview, say to see whether that is the button that was clicked. That way the labels don't affect the control flow. Is there some subtle flaw with this approach that the Drupal op convention is avoiding?

Comments

obrigado’s picture

I just ran into this as well. Completely bizarre.

Anonymous’s picture

Title: Can't change label of the Save button on comment form » Yes, it is

I have found this bug too. When I change the value of the "Save" button, for example to "Add", and then I try to add some comment, push the button, but it lose its submit function. I am moved to reply page. Is there any ideas? How to overcome this bug?

Anonymous’s picture

Title: Yes, it is » Can't change label of the Save button on comment form
Anonymous’s picture

I got, why I could not change the label of the button. I tried to do change the label through theme system. And I read that: Why changing button labels through theming is bad
Now, I utilize the hook_form_alter for that.

My code is:

function ModuleName_form_alter(&$form, $form_state, $form_id) {
	if($form_id == 'comment_form') {
		$form['submit']['#value'] = 'Add'; // We have changed the label of the "Save" button on the comment form
	}
}

And there is no losing of the submit function, it just works.

If there are some questions, ask, and I try to answer.

tiwiex’s picture

Hello,

I created my form using the create content type. Where do i insert this code. My form name is application_form.

Thanks for the great community support.

sun’s picture

Status: Active » Closed (works as designed)

This has been fixed for D7 already, but cannot be backported.

z33k3r’s picture

Just what I was looking for. Thanks!

Sepero’s picture

I added this to my template.php file in the function MyTheme_comment_form(). Be sure to replace "MyTheme" with the name of your theme. Here is what mine looks like.

function sky_comment_form($form) {
  $form['submit']['#value'] = "Post";
  return drupal_render($form);
}

EDIT: My code doesn't work for some reason. It doesn't post correctly.