Erich
Erich

Reputation: 545

How to customize submit button on Drupal 8 contact form?

I have a contact form on my Drupal 8 site and I would like to remove the preview button and customize the html for the submit button.

I've tried this in my theme:

function mytheme_form_contact_message_feedback_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  $form['submit']['#prefix'] = '<div class="contact-form-btn col-xs-12 col-md-10 col-md-offset-2 no-pad-left">';
  $form['submit']['#suffix'] = '</div>';
  $form['submit']['#value'] = 'Submit';
  $form['submit']['#title'] = 'Submit';

}

But that doesn't seem to change either the html wrapping it or the label on the button itself.

Also, if you have any advice on how to remove the preview button I'd appreciate it!

Upvotes: 0

Views: 4955

Answers (4)

vayu
vayu

Reputation: 81

Just to spell out what worked for me which I got from the link in the answer above.

In my mytheme.theme file I put the following code and I could both remove the preview button and also change the submit button text.

function mytheme_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  if (in_array($form_id, ['contact_message_feedback_form', ])) {
    $key = ($form_id == 'contact_message_feedback_form') ? 'actions' : 'basic';
    $form[$key]['submit']['#value'] = 'My Submit Message';
    $form[$key]['preview']['#access'] = FALSE;
  }
}

Upvotes: 0

sunil
sunil

Reputation: 99

Your code should be

function mytheme_form_contact_message_feedback_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

    $form['actions']['submit']['#prefix'] = '<div class="contact-form-btn col-xs-12 col-md-10 col-md-offset-2 no-pad-left">';
    $form['actions']['submit']['#suffix'] = '</div>';
    $form['actions']['submit']['#value'] = 'Your value';
}

Thanks

Upvotes: 2

S&#233;bastien Gicquel
S&#233;bastien Gicquel

Reputation: 4386

Maybe you can use contact storage (really useful with forms in Drupal 8). It allows you to store in DB datas sent with forms, customize button text, hide preview button.

Upvotes: 0

Erich
Erich

Reputation: 545

I'm not sure why it didn't work as I was doing it above - because that works for all the other fields on the form, but the solution posted here works.

Upvotes: 1

Related Questions