Skip to main content
  • X Twitter
  • youtube
Home
Drupal 4U
Built for AI. Powered by Drupal.
  • Home
  • Showcases
  • Services
  • Blog
  • Themes
  • About
  • Contact

Breadcrumb

  1. Home
  2. HowTos
  3. How to remove the preview button on a Drupal 8 contact form

How to remove the preview button on a Drupal 8 contact form

HowTos
August 31, 2017
Profile picture for user drupal
By drupal
  • facebook-f
  • twitter
  • envelope
  • print
361

This howto shows you 2 ways how you can easily remove the preview button from a Drupal 8 contact form (included in core).

Option 1. Use hook_form_alter()

One proposal is to utilize hook_form_alter() to strip out the preview button form element in any form. We did not test this solution, but according to the feedbacks on https://drupal.stackexchange.com it seem to work.

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

Source: https://drupal.stackexchange.com/questions/209078/how-can-i-remove-previ...

Option 2: Modify MessageForm.php

Another not so elegant solution, because it is a hack to Drupal core, is to edit the MessageForm.php in /core/modules/contact/src.

Change:

  public function actions(array $form, FormStateInterface $form_state) {
    $elements = parent::actions($form, $form_state);
    $elements['submit']['#value'] = $this->t('Send message');
    $elements['preview'] = [
      '#type' => 'submit',
      '#value' => $this->t('Preview'),
      '#submit' => ['::submitForm', '::preview'],
    ];
    return $elements;
  }

To:

 public function actions(array $form, FormStateInterface $form_state) {
    $elements = parent::actions($form, $form_state);
    $elements['submit']['#value'] = $this->t('Send message');
    return $elements;
  }

Important note: Because it is a hack use it at your own risk. Being a hack to Drupal core might make it complicated to apply site updates , it will make it difficult for those that come after to maintain the site and you could possibly leave your site vulnerable to exploits. For us it worked perfectly and we don't see problems associated with this simple modification.

How to remove the preview button on a Drupal 8 contact form
Modules

Read more articles

Newer
How to add a custom html.html.twig to your Drupal 8 theme
Older
Troubleshooting "500 Internal Server Error" Drupal
  • Log in to post comments
Profile picture for user drupal
drupal
361
1
min read
A- A+
  • facebook-f
  • twitter
  • envelope
  • print

Contact Us

Get in touch for questions and our Drupal development, design and optimization services.

Contact Us

We aim to reply the latest within 48h.

Drupal 4U: Built for AI. Powered by Drupal.

We are a group of Drupal designers, developers and webmasters. We are Drupal experts! We provide comprehensive services that go beyond traditional web development, focusing on how AI understands, interprets, and cites your content. With our deep expertise in Drupal's architecture, we build the intelligent core of your digital presence, ensuring your brand remains authoritative and visible in a world where AI is the primary gatekeeper of information. Partner with us and let's future-proof your digital strategy together.

  • X Twitter
  • youtube

Drupal Services

  • Drupal Web Design
  • Drupal Development
  • Drupal SEO
  • Drupal GEO
  • Drupal Migration

Drupal 4U

  • Services
  • Showcases
  • Themes
  • Blog
  • About

Drupal Links

  • Official Drupal Website
  • Theme Forest
  • Gaivas Themes
  • More Than Themes
  • DXPR: Drupal for Marketers
  • Drupal Answers
2012 - 2025, Drupal 4U

Footer menu

  • Contact
Home
Drupal 4U
Built for AI. Powered by Drupal.
  • Home
  • Showcases
  • Services
  • Blog
  • Themes
  • About
  • Contact
Clear keys input element