8

How to turn off preview in contact form in Drupal 8.1.x

enter image description here

| improve this question | | | | |
  • Use some css and remove it from the display. – Joshua Kissoon Jun 29 '18 at 2:53
14

A custom module can utilize hook_form_alter() to strip out the preview button form element in any form:

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, $form_state, $form_id) {

  // Look for any form provided by the contact module.
  // If you want to target a specific form you'll use the whole form ID
  // (e.g. Website feedback = 'contact_message_feedback_form').
  if (strpos($form_id, 'contact_message_') !== FALSE) {
    $form['actions']['preview']['#access'] = FALSE;
  }
}
| improve this answer | | | | |
  • 1
    If I remember well the preferred way to remove a form element instead of using unset() is by setting the #access property to FALSE, so: $form['actions']['preview']['#access'] = FALSE; – batigolix Oct 5 '16 at 13:17
  • It doesn't work for action buttons in D8. If this has been fixed or there's an alternative (I didn't have the time for research what's going on there), feel free to update the answer accordingly. – Shawn Conn Oct 5 '16 at 17:33
  • Seems to work fine. I updated the snippet – batigolix Dec 21 '16 at 10:26
  • Did it via theming instead, hook_form_form_id_alter() hook, full code snippet (works for a default contact form): function THEME_form_contact_message_feedback_form_alter(&$form, &$form_state, $form_id) { $form['actions']['preview']['#access'] = FALSE; } – tulvit Sep 21 '18 at 13:56
1

It's works for me The best way to do that Add this code your YOURPROFILENAME.profile file

/**
 * Implements hook_form_alter().
 */
function YOURPROFILENAME_form_alter(&$form, $form_state, $form_id) {
   if (strpos($form_id, 'contact_message_') !== FALSE) {
    $form['actions']['preview']['#access'] = FALSE;
  }
}
| improve this answer | | | | |
1

There are patches under work and testing but no commits yet https://www.drupal.org/project/drupal/issues/2960353. Patches work on latest D8 to date, but until a patch has been committed, suggest to use Hide Preview Button module https://www.drupal.org/project/hide_preview works well for other form pages, too. In case, this helps those not comfortable with working with hooks and patches.

| improve this answer | | | | |

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.