Mercurial > hg > isophonics-drupal-site
comparison core/modules/contact/src/Controller/ContactController.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | c2387f117808 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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'), | |
69 ]), 'error'); | |
70 return []; | |
71 } | |
72 else { | |
73 throw new NotFoundHttpException(); | |
74 } | |
75 } | |
76 } | |
77 | |
78 $message = $this->entityManager() | |
79 ->getStorage('contact_message') | |
80 ->create([ | |
81 'contact_form' => $contact_form->id(), | |
82 ]); | |
83 | |
84 $form = $this->entityFormBuilder()->getForm($message); | |
85 $form['#title'] = $contact_form->label(); | |
86 $form['#cache']['contexts'][] = 'user.permissions'; | |
87 $this->renderer->addCacheableDependency($form, $config); | |
88 return $form; | |
89 } | |
90 | |
91 /** | |
92 * Form constructor for the personal contact form. | |
93 * | |
94 * @param \Drupal\user\UserInterface $user | |
95 * The account for which a personal contact form should be generated. | |
96 * | |
97 * @return array | |
98 * The personal contact form as render array as expected by drupal_render(). | |
99 * | |
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. | |
103 */ | |
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(); | |
108 } | |
109 | |
110 $message = $this->entityManager()->getStorage('contact_message')->create([ | |
111 'contact_form' => 'personal', | |
112 'recipient' => $user->id(), | |
113 ]); | |
114 | |
115 $form = $this->entityFormBuilder()->getForm($message); | |
116 $form['#title'] = $this->t('Contact @username', ['@username' => $user->getDisplayName()]); | |
117 $form['#cache']['contexts'][] = 'user.permissions'; | |
118 return $form; | |
119 } | |
120 | |
121 } |