Skip to main content

Post categories

  • Drupal Tips & Tricks
  • Drupal Howtos
  • Drupal Themes
  • X Twitter
  • youtube
Home
Drupal 4U
Drupal Development, Drupal Web Design, Drupal SEO
  • 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
355

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
355
1
min read
A- A+
  • facebook-f
  • twitter
  • envelope
  • print

Contact Drupal 4U

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

Contact Us

We aim to reply the latest within 48h.

About Drupal 4U

We are a group of Drupal designers, developers and webmasters and we launched this website to share our experience and knowledge with you. Drupal is a free software package that allows you to easily organize, manage and publish your content, with an endless variety of customization. Drupal is open source software maintained and developed by a very large community of developers and users.

  • X Twitter
  • youtube

Drupal Services

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

Drupal 4U

  • Services
  • Showcases
  • Themes
  • Blog
  • About

Drupal Links

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

Footer menu

  • Contact
Home
Drupal 4U
Drupal Development, Drupal Web Design, Drupal SEO
  • Home
  • Showcases
  • Services
  • Blog
  • Themes
  • About
  • Contact
Clear keys input element