Hi,

Anyone know how I can make the "Subject" field on the Contact form optional i.e. NOT required. If that is not possible, then how can I remove it completely?

I have created different Categories which suffice as a subject. I know want to make it as easy as possible for the user to "Contact Us"

Much appreciate,

JJ

Comments

wavesailor’s picture

ping

MadTogger’s picture

Hi,

I was searching for how to do this also and found nothing but I have eventually sorted it out myself.

Open up the Contact Module and look for the file called, contact.pages.inc.

Around about line 54 you will see:-

    $form['subject'] = array('#type' => 'textfield',
    '#title' => t('Subject'),
    '#maxlength' => 255,
    '#required' => TRUE,
    );

I just commented it out like this:-

   /**
      * $form['subject'] = array('#type' => 'textfield',
      * '#title' => t('Subject'),
      * '#maxlength' => 255,
      * '#required' => TRUE,
      * );
      */

So far it is working fine on my site, see it at; www.madtogger.co.uk/contact

Whether this is the correct way of doing it I have no idea but it works.

The reason I just commented it out was incase for some reason it stopped working I could clear the comments.

Hope this helps.

Regards..,

MT

gpk’s picture

Status: Active » Fixed

The Drupal way would be to implement hook_form_alter() in a tiny custom module and then set $form['subject']['#required'] = FALSE, or unset($form['subject']); to get rid of the field completely.

Alternatively try webform module which lets you create completely customized forms for whatever purpose you want.

dave reid’s picture

Don't unset. Please use the #access attribute to 'remove' things properly.

function mymodule_form_contact_mail_page_alter(&$form, $form_state) {
  $form['subject']['#access'] = FALSE;
}

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

therobyouknow’s picture

another approach would be to use CSS class with display: none property in a DIV around the field.

andread’s picture

Issue summary: View changes

For Drupal 7 it is slighlty different:

function [Your_module_name]_form_contact_site_form_alter(&$form, $form_state) {
$form['subject']['#access'] = FALSE;
}

The function name have changed.
If you use #access then the field is remove totally, if you use #required than the field is visiable, but not required. ;)

Hope it helps someone in the future!

waqarit’s picture

#8 worked for me.

hongpong’s picture

Drupal 8 technique as follows:

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

if (in_array($form_id, ['contact_message_YOURFORMNAME_form'])) {
  $form['subject']['#access'] = FALSE;
}
}

dump the $form_id if it's not known.

sébastien-fr’s picture

#8 worked for me.