You are here

public function ContactController::contactSitePage in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/contact/src/Controller/ContactController.php \Drupal\contact\Controller\ContactController::contactSitePage()
  2. 9 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 58

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)) {
    $contact_form = $this
      ->entityTypeManager()
      ->getStorage('contact_form')
      ->load($config
      ->get('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;
}