8

Does anyone know how to rename the "save" comment button? I am trying to change it to "Post". I am using Drupal 7 and the Zen sub theme.

  • Have you tried the String Overrides module? – geekgirlweb Nov 11 '11 at 15:49
  • no i haven't. I will try using the module. thanks :) – Jasmine Ahmed Nov 11 '11 at 16:05
  • 1
    This module will change "Save" in all the places it is used, not just in comments. – kiamlaluno Nov 11 '11 at 16:17
19

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,
  );
  • 1
    Just to be sure my answer is clear: You don't have to edit the code used by Drupal; you need to create a custom module that implements hook_form_FORM_ID_alter(). – kiamlaluno Nov 12 '11 at 10:16
  • I corrected the code to work also when the Drupal 6 site is forcing the preview of the submitted comments, and the form is not in preview mode. – kiamlaluno Jul 20 '16 at 5:56
2

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.

2

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.
};
1

No need for custom module or using the string overrides module. In your settings.php, around line 416, uncomment and modify the following using your overrides:

/**
String overrides:

To override specific strings on your site with or without enabling locale
module, add an entry to this list. This functionality allows you to change
 * a small number of your site's default English language interface strings.
 *
 * Remove the leading hash signs to enable.
 */
# $conf['locale_custom_strings_en'][''] = array(
#   'forum'      => 'Discussion board',
#   '@count min' => '@count minutes',
# );
  • 3
    Same problem as with the String Override module. Everytime a module calls t("Save"), the override would get used, such as the node edit form and a bunch of other places in the admin. – mpdonadio Nov 11 '11 at 21:37
1

As Andy Laken mentioned above

...new 'Post' button won't actually submit the form...

How to fix this:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id === 'comment_form') {
    // Rename submit button.
    $form['submit']['#value'] = t('Post');
    // Add new form validator.
    array_unshift($form['#validate'], 'MYMODULE_comment_form_validate');
  }
}

function MYMODULE_comment_form_validate(&$form, &$form_state) {
  // Restore native value.
  if ($form_state['values']['op'] === t('Post')) {
    $form['submit']['#value'] = t('Save');
    $form_state['values']['op'] = t('Save');
  }
}

That's it! Your validate function goes first and comment module will process form with native submit value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.