3 namespace Drupal\contact\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\contact\ContactFormInterface;
7 use Drupal\Core\Render\RendererInterface;
8 use Drupal\user\UserInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13 * Controller routines for contact routes.
15 class ContactController extends ControllerBase {
20 * @var \Drupal\Core\Render\RendererInterface
25 * Constructs a ContactController object.
27 * @param \Drupal\Core\Render\RendererInterface $renderer
30 public function __construct(RendererInterface $renderer) {
31 $this->renderer = $renderer;
37 public static function create(ContainerInterface $container) {
39 $container->get('renderer')
44 * Presents the site-wide contact form.
46 * @param \Drupal\contact\ContactFormInterface $contact_form
47 * The contact form to use.
50 * The form as render array as expected by drupal_render().
52 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
53 * Exception is thrown when user tries to access non existing default
56 public function contactSitePage(ContactFormInterface $contact_form = NULL) {
57 $config = $this->config('contact.settings');
59 // Use the default form if no form has been passed.
60 if (empty($contact_form)) {
61 $contact_form = $this->entityManager()
62 ->getStorage('contact_form')
63 ->load($config->get('default_form'));
64 // If there are no forms, do not display the form.
65 if (empty($contact_form)) {
66 if ($this->currentUser()->hasPermission('administer contact forms')) {
67 drupal_set_message($this->t('The contact form has not been configured. <a href=":add">Add one or more forms</a> .', [
68 ':add' => $this->url('contact.form_add'),
73 throw new NotFoundHttpException();
78 $message = $this->entityManager()
79 ->getStorage('contact_message')
81 'contact_form' => $contact_form->id(),
84 $form = $this->entityFormBuilder()->getForm($message);
85 $form['#title'] = $contact_form->label();
86 $form['#cache']['contexts'][] = 'user.permissions';
87 $this->renderer->addCacheableDependency($form, $config);
92 * Form constructor for the personal contact form.
94 * @param \Drupal\user\UserInterface $user
95 * The account for which a personal contact form should be generated.
98 * The personal contact form as render array as expected by drupal_render().
100 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
101 * Exception is thrown when user tries to access a contact form for a
102 * user who does not have an email address configured.
104 public function contactPersonalPage(UserInterface $user) {
105 // Do not continue if the user does not have an email address configured.
106 if (!$user->getEmail()) {
107 throw new NotFoundHttpException();
110 $message = $this->entityManager()->getStorage('contact_message')->create([
111 'contact_form' => 'personal',
112 'recipient' => $user->id(),
115 $form = $this->entityFormBuilder()->getForm($message);
116 $form['#title'] = $this->t('Contact @username', ['@username' => $user->getDisplayName()]);
117 $form['#cache']['contexts'][] = 'user.permissions';