function ContactController::contactSitePage

Same name and namespace in other branches
  1. 9 core/modules/contact/src/Controller/ContactController.php \Drupal\contact\Controller\ContactController::contactSitePage()
  2. 8.9.x core/modules/contact/src/Controller/ContactController.php \Drupal\contact\Controller\ContactController::contactSitePage()
  3. 11.x core/modules/contact/src/Controller/ContactController.php \Drupal\contact\Controller\ContactController::contactSitePage()

Presents the site-wide contact form.

Parameters

\Drupal\contact\ContactFormInterface $contact_form: The contact form to use.

Return value

array The form as render array as expected by \Drupal\Core\Render\RendererInterface::render().

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Exception is thrown when user tries to access non existing default contact form.

1 string reference to 'ContactController::contactSitePage'
contact.routing.yml in core/modules/contact/contact.routing.yml
core/modules/contact/contact.routing.yml

File

core/modules/contact/src/Controller/ContactController.php, line 48

Class

ContactController
Controller routines for contact routes.

Namespace

Drupal\contact\Controller

Code

public function contactSitePage(?ContactFormInterface $contact_form = NULL) {
  $config = $this->config('contact.settings');
  // Use the default form if no form has been passed.
  if (empty($contact_form)) {
    $default_form = $config->get('default_form');
    // Load the default form, if configured.
    if (!is_null($default_form)) {
      $contact_form = $this->entityTypeManager()
        ->getStorage('contact_form')
        ->load($default_form);
    }
    // If there are no forms, do not display the form.
    if (empty($contact_form)) {
      if ($this->currentUser()
        ->hasPermission('administer contact forms')) {
        $this->messenger()
          ->addError($this->t('The contact form has not been configured. <a href=":add">Add one or more forms</a> .', [
          ':add' => Url::fromRoute('contact.form_add')->toString(),
        ]));
        return [];
      }
      else {
        throw new NotFoundHttpException();
      }
    }
  }
  $message = $this->entityTypeManager()
    ->getStorage('contact_message')
    ->create([
    'contact_form' => $contact_form->id(),
  ]);
  $form = $this->entityFormBuilder()
    ->getForm($message);
  $form['#title'] = $contact_form->label();
  $form['#cache']['contexts'][] = 'user.permissions';
  $this->renderer
    ->addCacheableDependency($form, $config);
  return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.