1 contact.pages.inc contact_site_form($form, &$form_state)

Form constructor for the site-wide contact form.

See also

contact_menu()

contact_site_form_validate()

contact_site_form_submit()

Related topics

File

core/modules/contact/contact.pages.inc, line 16
Page callbacks for the Contact module.

Code

function contact_site_form($form, &$form_state) {
  global $user;

  // Check if flood control has been activated for sending e-mails.
  $config = config('contact.settings');
  $limit = $config->get('contact_threshold_limit');
  $window = $config->get('contact_threshold_window');
  if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms')) {
    backdrop_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error');
    return array();
  }

  // Get an array of the categories.
  $config_data = contact_config_data();
  $categories = $config_data['categories'];
  $default_category = $config_data['contact_default_category'];

  // If there are no categories, do not display the form.
  if (!$categories) {
    if (user_access('administer contact forms')) {
      backdrop_set_message(t('The contact form has not been configured. <a href="@add">Add one or more categories</a> to the form.', array('@add' => url('admin/structure/contact/add'))), 'error');
    }
    else {
      backdrop_not_found();
      backdrop_exit();
    }
  }

  $options = array();
  foreach ($categories as $cat) {
    $options[$cat['cid']] = $cat['category'];
  }

  // If there is more than one category available and no default category has
  // been selected, prepend a default placeholder value.
  if ($default_category == 0) {
    if (count($options) > 1) {
      $options = array(0 => t('- Please choose -')) + $options;
    }
    else {
      $default_category = key($categories);
    }
  }

  if (!$user->uid) {
    $form['#attached']['library'][] = array('system', 'jquery.cookie');
    $form['#attributes']['class'][] = 'user-info-from-cookie';
  }

  $form['#attributes']['class'][] = 'contact-form';
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your name'),
    '#maxlength' => 255,
    '#default_value' => $user->uid ? user_format_name($user) : '',
    '#required' => TRUE,
  );
  $form['mail'] = array(
    '#type' => 'email',
    '#title' => t('Your e-mail address'),
    '#default_value' => $user->uid ? $user->mail : '',
    '#required' => TRUE,
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#maxlength' => 255,
    '#required' => TRUE,
  );

  $form['cid'] = array(
    '#type' => 'select',
    '#title' => t('Category'),
    '#default_value' => $default_category,
    '#options' => $options,
    '#required' => TRUE,
    '#access' => count($options) > 1,
  );
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#required' => TRUE,
  );
  // We do not allow anonymous users to send themselves a copy
  // because it can be abused to spam people.
  $form['copy'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send yourself a copy.'),
    '#access' => $user->uid && contact_show_personal_copy_checkbox(),
  );
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send message'),
  );

  return $form;
}