How to customize submit button on Drupal 8 contact form?

时间:2016-06-18 20:11:25

标签: drupal drupal-8 drupal-forms drupal-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!

4 个答案:

答案 0 :(得分:2)

您的代码应为

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

由于

答案 1 :(得分:1)

我不确定为什么它不能像我上面那样工作 - 因为它适用于表单上的所有其他字段,但发布的here解决方案有效。

答案 2 :(得分:0)

也许您可以使用contact storage(对Drupal 8中的表单非常有用)。它允许您存储与表单一起发送的数据库数据,自定义按钮文本,隐藏预览按钮

答案 3 :(得分:0)

只需说出我从上面答案中的链接获得的对我有用的东西。

在mytheme.theme文件中,我输入了以下代码,既可以删除预览按钮,也可以更改提交按钮的文本。

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