You are here

public function ContactStorageController::contactSitePage in Contact Storage 8

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.

Overrides ContactController::contactSitePage

File

src/Controller/ContactStorageController.php, line 18

Class

ContactStorageController
Controller routines for contact storage routes.

Namespace

Drupal\contact_storage\Controller

Code

public function contactSitePage(ContactFormInterface $contact_form = NULL) {

  // This is an override of ContactController::contactSitePage() that uses
  // the entity view builder. This is necessary to show the close message for
  // disabled forms.
  $config = $this
    ->config('contact.settings');

  // Use the default form if no form has been passed.
  $manager = $this
    ->entityTypeManager();
  if (empty($contact_form)) {
    $contact_form = $manager
      ->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();
      }
    }
  }
  $view_builder = $manager
    ->getViewBuilder('contact_form');
  return $view_builder
    ->view($contact_form, 'full', $contact_form
    ->language());
}