Drupal - How do you rename the "save" comment button?

For Drupal 7, you need to create a custom module that implements hook_form_FORM_ID_alter() using code similar to the following one (replace "mymodule" with the short name of the module you are writing):

function mymodule_form_comment_form_alter(&$form, &$form_state) {
  if (isset($form['actions']['submit'])) {
    $form['actions']['submit']['#value'] = t('Post');
  }
}

comment_form() uses the following code, to define the form buttons:

  // Only show the save button if comment previews are optional or if we are
  // already previewing the submission.
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Save'), 
    '#access' => ($comment->cid && user_access('administer comments')) || variable_get('comment_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_REQUIRED || isset($form_state['comment_preview']), 
    '#weight' => 19,
  );
  $form['actions']['preview'] = array(
    '#type' => 'submit', 
    '#value' => t('Preview'), 
    '#access' => (variable_get('comment_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_DISABLED), 
    '#weight' => 20, 
    '#submit' => array('comment_form_build_preview'),

For Drupal 6, the code should be the following one:

function mymodule_form_comment_form_alter(&$form, &$form_state) {
  if (isset($form['submit'])) {
    $form['submit']['#value'] = t('Post');
  }
}

I added the if (isset($form['submit'])) {} part because in Drupal 6, comment_form() defines the form buttons using the following code, and the button you are trying to change could not be present in the form.

  // Only show save button if preview is optional or if we are in preview mode.
  // We show the save button in preview mode even if there are form errors so that
  // optional form elements (e.g., captcha) can be updated in preview mode.
  if (!form_get_errors() && ((variable_get('comment_preview_' . $node->type, COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) || ($op == t('Preview')) || ($op == t('Save')))) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#weight' => 19,
    );
  }

  $form['preview'] = array(
    '#type' => 'button',
    '#value' => t('Preview'),
    '#weight' => 20,
  );

I prefer using hook_form_alter vs String Overrides.

function YOURMODULENAME_form_comment_form_alter(&$form, &$form_state) {
  $form['buttons']['submit']['#value'] = 'Submit Comment'; //Your text for the submit button goes here.
};

For Drupal 6, the answers above suggesting using hook_form_alter will not work, although you'd think it would. Typically you'd do this like:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ('comment_form' == $form_id) {
    $form['submit']['#value'] = t('Post');
  }
}

While this appears to work, and you'll see a button with text 'Post', in fact you'll find two problems:

  1. If your site is set up to force preview of comments before saving, you'll find that the 'Post' button is added to the initial comment form, where there should be only the 'Preview' button. This is easily fixed, though.
  2. Your new 'Post' button won't actually submit the form - D6 comment.module looks for the button value to do its logic, and if you change it to something other than 'Save' this breaks the submit logic.

To actually make this work you'd need to hide the button and use a custom form submit handler. If I do that I'll return here and post working code.

Tags:

Comments

7