Sunday, October 20, 2019

Drupal 7 / Ubercart 7 - Add Mobile number to Contact Form

Drupal core 7.67 / Ubercart 7.x-3.13

We would like to get the mobile number of the people who contact us, so we can reach them by WhatsApp, Line, WeChat, etc...

Unfortunately, adding a simple phone number field to the contact form is a real pain, so the fastest way to do this is to simply add some code to the contact form


Navigate to <Drupalroot>/modules/contact

Then, add these lines of code to, around line 98:

    $form['phone'] = array('#type' => 'textfield',
        '#title' => t('Phone number:'),
        '#maxlength' => 20,
        '#required' => TRUE,

See example below:

Next, we need to add the email functionality to Drupal to include the mobile field contents in the notification email:

Add these lines of code to contact.module, around line 184:

      // GL 2019-10-20  Add mobile number to contact form email - BEGIN
      $message['body'][] = t('Mobile:', $variables, array('langcode' => $language->language));
      $message['body'][] = $params['mobile'];
      // GL 2019-10-20  Add mobile number to contact form email - END

Remember to add the lines of code under both of these sections:

  • page_copy
  • user_copy

See example below:

Here's what the contact form looks like after these changes have been made:

Here's what the resulting email looks like:


No comments:

Post a Comment