Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\contact\Controller;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Controller\ControllerBase;
|
Chris@0
|
6 use Drupal\contact\ContactFormInterface;
|
Chris@0
|
7 use Drupal\Core\Render\RendererInterface;
|
Chris@0
|
8 use Drupal\user\UserInterface;
|
Chris@0
|
9 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
10 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Controller routines for contact routes.
|
Chris@0
|
14 */
|
Chris@0
|
15 class ContactController extends ControllerBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * The renderer.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var \Drupal\Core\Render\RendererInterface
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $renderer;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Constructs a ContactController object.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @param \Drupal\Core\Render\RendererInterface $renderer
|
Chris@0
|
28 * The renderer.
|
Chris@0
|
29 */
|
Chris@0
|
30 public function __construct(RendererInterface $renderer) {
|
Chris@0
|
31 $this->renderer = $renderer;
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * {@inheritdoc}
|
Chris@0
|
36 */
|
Chris@0
|
37 public static function create(ContainerInterface $container) {
|
Chris@0
|
38 return new static(
|
Chris@0
|
39 $container->get('renderer')
|
Chris@0
|
40 );
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Presents the site-wide contact form.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param \Drupal\contact\ContactFormInterface $contact_form
|
Chris@0
|
47 * The contact form to use.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @return array
|
Chris@16
|
50 * The form as render array as expected by
|
Chris@16
|
51 * \Drupal\Core\Render\RendererInterface::render().
|
Chris@0
|
52 *
|
Chris@0
|
53 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
Chris@0
|
54 * Exception is thrown when user tries to access non existing default
|
Chris@0
|
55 * contact form.
|
Chris@0
|
56 */
|
Chris@0
|
57 public function contactSitePage(ContactFormInterface $contact_form = NULL) {
|
Chris@0
|
58 $config = $this->config('contact.settings');
|
Chris@0
|
59
|
Chris@0
|
60 // Use the default form if no form has been passed.
|
Chris@0
|
61 if (empty($contact_form)) {
|
Chris@18
|
62 $contact_form = $this->entityTypeManager()
|
Chris@0
|
63 ->getStorage('contact_form')
|
Chris@0
|
64 ->load($config->get('default_form'));
|
Chris@0
|
65 // If there are no forms, do not display the form.
|
Chris@0
|
66 if (empty($contact_form)) {
|
Chris@0
|
67 if ($this->currentUser()->hasPermission('administer contact forms')) {
|
Chris@17
|
68 $this->messenger()->addError($this->t('The contact form has not been configured. <a href=":add">Add one or more forms</a> .', [
|
Chris@0
|
69 ':add' => $this->url('contact.form_add'),
|
Chris@17
|
70 ]));
|
Chris@0
|
71 return [];
|
Chris@0
|
72 }
|
Chris@0
|
73 else {
|
Chris@0
|
74 throw new NotFoundHttpException();
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@18
|
79 $message = $this->entityTypeManager()
|
Chris@0
|
80 ->getStorage('contact_message')
|
Chris@0
|
81 ->create([
|
Chris@0
|
82 'contact_form' => $contact_form->id(),
|
Chris@0
|
83 ]);
|
Chris@0
|
84
|
Chris@0
|
85 $form = $this->entityFormBuilder()->getForm($message);
|
Chris@0
|
86 $form['#title'] = $contact_form->label();
|
Chris@0
|
87 $form['#cache']['contexts'][] = 'user.permissions';
|
Chris@0
|
88 $this->renderer->addCacheableDependency($form, $config);
|
Chris@0
|
89 return $form;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Form constructor for the personal contact form.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @param \Drupal\user\UserInterface $user
|
Chris@0
|
96 * The account for which a personal contact form should be generated.
|
Chris@0
|
97 *
|
Chris@0
|
98 * @return array
|
Chris@16
|
99 * The personal contact form as render array as expected by
|
Chris@16
|
100 * \Drupal\Core\Render\RendererInterface::render().
|
Chris@0
|
101 *
|
Chris@0
|
102 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
Chris@0
|
103 * Exception is thrown when user tries to access a contact form for a
|
Chris@0
|
104 * user who does not have an email address configured.
|
Chris@0
|
105 */
|
Chris@0
|
106 public function contactPersonalPage(UserInterface $user) {
|
Chris@0
|
107 // Do not continue if the user does not have an email address configured.
|
Chris@0
|
108 if (!$user->getEmail()) {
|
Chris@0
|
109 throw new NotFoundHttpException();
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@18
|
112 $message = $this->entityTypeManager()->getStorage('contact_message')->create([
|
Chris@0
|
113 'contact_form' => 'personal',
|
Chris@0
|
114 'recipient' => $user->id(),
|
Chris@0
|
115 ]);
|
Chris@0
|
116
|
Chris@0
|
117 $form = $this->entityFormBuilder()->getForm($message);
|
Chris@0
|
118 $form['#title'] = $this->t('Contact @username', ['@username' => $user->getDisplayName()]);
|
Chris@0
|
119 $form['#cache']['contexts'][] = 'user.permissions';
|
Chris@0
|
120 return $form;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 }
|