You are here

public function ContactController::contactPersonalPage in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/contact/src/Controller/ContactController.php \Drupal\contact\Controller\ContactController::contactPersonalPage()

Form constructor for the personal contact form.

Parameters

\Drupal\user\UserInterface $user: The account for which a personal contact form should be generated.

Return value

array The personal contact 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 a contact form for a user who does not have an email address configured.

1 string reference to 'ContactController::contactPersonalPage'
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 107

Class

ContactController
Controller routines for contact routes.

Namespace

Drupal\contact\Controller

Code

public function contactPersonalPage(UserInterface $user) {

  // Do not continue if the user does not have an email address configured.
  if (!$user
    ->getEmail()) {
    throw new NotFoundHttpException();
  }
  $message = $this
    ->entityTypeManager()
    ->getStorage('contact_message')
    ->create([
    'contact_form' => 'personal',
    'recipient' => $user
      ->id(),
  ]);
  $form = $this
    ->entityFormBuilder()
    ->getForm($message);
  $form['#title'] = $this
    ->t('Contact @username', [
    '@username' => $user
      ->getDisplayName(),
  ]);
  $form['#cache']['contexts'][] = 'user.permissions';
  return $form;
}