function contact_site_form

Page callback: Form constructor for the site-wide contact form.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

See also

contact_menu()

contact_site_form_submit()

Related topics

1 string reference to 'contact_site_form'
contact_menu in drupal/core/modules/contact/contact.module
Implements hook_menu().

File

drupal/core/modules/contact/contact.pages.inc, line 21
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('flood.limit');
  $interval = $config
    ->get('flood.interval');
  if (!drupal_container()
    ->get('flood')
    ->isAllowed('contact', $limit, $interval) && !user_access('administer contact forms')) {
    drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array(
      '%limit' => $limit,
      '@interval' => format_interval($interval),
    )), 'error');
    throw new AccessDeniedHttpException();
  }

  // Get an array of the categories and the current default category.
  $categories = entity_load_multiple('contact_category');

  // If there are no categories, do not display the form.
  if (!$categories) {
    if (user_access('administer contact forms')) {
      drupal_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 {
      throw new NotFoundHttpException();
    }
  }

  // Prepare array for select options.
  uasort($categories, 'Drupal\\Core\\Config\\Entity\\ConfigEntityBase::sort');
  $options = array();
  foreach ($categories as $category) {
    $options[$category
      ->id()] = $category
      ->label();
  }

  // If there is more than one category available and no default category has
  // been selected, prepend a default placeholder value.
  $default_category = $config
    ->get('default_category');
  if (!$default_category) {
    $default_category = !empty($categories) ? key($categories) : NULL;
  }
  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,
  );

  // Do not allow authenticated users to alter the name or e-mail values to
  // prevent the impersonation of other users.
  if ($user->uid) {

    // Hide the original name and e-mail address fields and display read-only
    // versions in their place.
    $form['name']['#access'] = $form['mail']['#access'] = FALSE;
    $form['name_display'] = array(
      '#type' => 'item',
      '#title' => t('Your name'),
      '#markup' => check_plain($form['name']['#default_value']),
    );
    $form['mail_display'] = array(
      '#type' => 'item',
      '#title' => t('Your e-mail address'),
      '#markup' => check_plain($form['mail']['#default_value']),
    );
  }
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#maxlength' => 255,
    '#required' => TRUE,
  );
  $form['category'] = array(
    '#type' => 'select',
    '#title' => t('Category'),
    '#default_value' => $default_category,
    '#options' => $options,
    '#empty_value' => 0,
    '#required' => TRUE,
    '#access' => count($categories) > 1,
  );
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#required' => TRUE,
  );

  // 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,
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send message'),
  );
  return $form;
}