b8b69d5f19a5f93c42af0f9b77b32423054d7f9b
[yaffs-website] / web / core / modules / contact / src / Controller / ContactController.php
1 <?php
2
3 namespace Drupal\contact\Controller;
4
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;
11
12 /**
13  * Controller routines for contact routes.
14  */
15 class ContactController extends ControllerBase {
16
17   /**
18    * The renderer.
19    *
20    * @var \Drupal\Core\Render\RendererInterface
21    */
22   protected $renderer;
23
24   /**
25    * Constructs a ContactController object.
26    *
27    * @param \Drupal\Core\Render\RendererInterface $renderer
28    *   The renderer.
29    */
30   public function __construct(RendererInterface $renderer) {
31     $this->renderer = $renderer;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static(
39       $container->get('renderer')
40     );
41   }
42
43   /**
44    * Presents the site-wide contact form.
45    *
46    * @param \Drupal\contact\ContactFormInterface $contact_form
47    *   The contact form to use.
48    *
49    * @return array
50    *   The form as render array as expected by drupal_render().
51    *
52    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
53    *   Exception is thrown when user tries to access non existing default
54    *   contact form.
55    */
56   public function contactSitePage(ContactFormInterface $contact_form = NULL) {
57     $config = $this->config('contact.settings');
58
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')]), 'error');
69           return [];
70         }
71         else {
72           throw new NotFoundHttpException();
73         }
74       }
75     }
76
77     $message = $this->entityManager()
78       ->getStorage('contact_message')
79       ->create([
80         'contact_form' => $contact_form->id(),
81       ]);
82
83     $form = $this->entityFormBuilder()->getForm($message);
84     $form['#title'] = $contact_form->label();
85     $form['#cache']['contexts'][] = 'user.permissions';
86     $this->renderer->addCacheableDependency($form, $config);
87     return $form;
88   }
89
90   /**
91    * Form constructor for the personal contact form.
92    *
93    * @param \Drupal\user\UserInterface $user
94    *   The account for which a personal contact form should be generated.
95    *
96    * @return array
97    *   The personal contact form as render array as expected by drupal_render().
98    *
99    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
100    *   Exception is thrown when user tries to access a contact form for a
101    *   user who does not have an email address configured.
102    */
103   public function contactPersonalPage(UserInterface $user) {
104     // Do not continue if the user does not have an email address configured.
105     if (!$user->getEmail()) {
106       throw new NotFoundHttpException();
107     }
108
109     $message = $this->entityManager()->getStorage('contact_message')->create([
110       'contact_form' => 'personal',
111       'recipient' => $user->id(),
112     ]);
113
114     $form = $this->entityFormBuilder()->getForm($message);
115     $form['#title'] = $this->t('Contact @username', ['@username' => $user->getDisplayName()]);
116     $form['#cache']['contexts'][] = 'user.permissions';
117     return $form;
118   }
119
120 }