7

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

enter image description here

  • Use some css and remove it from the display. – Joshua Kissoon Jun 29 '18 at 2:53
11

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;
  }
}
  • 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
0

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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