annotate 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
rev   line source
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@0 50 * The form as render array as expected by drupal_render().
Chris@0 51 *
Chris@0 52 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
Chris@0 53 * Exception is thrown when user tries to access non existing default
Chris@0 54 * contact form.
Chris@0 55 */
Chris@0 56 public function contactSitePage(ContactFormInterface $contact_form = NULL) {
Chris@0 57 $config = $this->config('contact.settings');
Chris@0 58
Chris@0 59 // Use the default form if no form has been passed.
Chris@0 60 if (empty($contact_form)) {
Chris@0 61 $contact_form = $this->entityManager()
Chris@0 62 ->getStorage('contact_form')
Chris@0 63 ->load($config->get('default_form'));
Chris@0 64 // If there are no forms, do not display the form.
Chris@0 65 if (empty($contact_form)) {
Chris@0 66 if ($this->currentUser()->hasPermission('administer contact forms')) {
Chris@0 67 drupal_set_message($this->t('The contact form has not been configured. <a href=":add">Add one or more forms</a> .', [
Chris@0 68 ':add' => $this->url('contact.form_add'),
Chris@0 69 ]), 'error');
Chris@0 70 return [];
Chris@0 71 }
Chris@0 72 else {
Chris@0 73 throw new NotFoundHttpException();
Chris@0 74 }
Chris@0 75 }
Chris@0 76 }
Chris@0 77
Chris@0 78 $message = $this->entityManager()
Chris@0 79 ->getStorage('contact_message')
Chris@0 80 ->create([
Chris@0 81 'contact_form' => $contact_form->id(),
Chris@0 82 ]);
Chris@0 83
Chris@0 84 $form = $this->entityFormBuilder()->getForm($message);
Chris@0 85 $form['#title'] = $contact_form->label();
Chris@0 86 $form['#cache']['contexts'][] = 'user.permissions';
Chris@0 87 $this->renderer->addCacheableDependency($form, $config);
Chris@0 88 return $form;
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * Form constructor for the personal contact form.
Chris@0 93 *
Chris@0 94 * @param \Drupal\user\UserInterface $user
Chris@0 95 * The account for which a personal contact form should be generated.
Chris@0 96 *
Chris@0 97 * @return array
Chris@0 98 * The personal contact form as render array as expected by drupal_render().
Chris@0 99 *
Chris@0 100 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
Chris@0 101 * Exception is thrown when user tries to access a contact form for a
Chris@0 102 * user who does not have an email address configured.
Chris@0 103 */
Chris@0 104 public function contactPersonalPage(UserInterface $user) {
Chris@0 105 // Do not continue if the user does not have an email address configured.
Chris@0 106 if (!$user->getEmail()) {
Chris@0 107 throw new NotFoundHttpException();
Chris@0 108 }
Chris@0 109
Chris@0 110 $message = $this->entityManager()->getStorage('contact_message')->create([
Chris@0 111 'contact_form' => 'personal',
Chris@0 112 'recipient' => $user->id(),
Chris@0 113 ]);
Chris@0 114
Chris@0 115 $form = $this->entityFormBuilder()->getForm($message);
Chris@0 116 $form['#title'] = $this->t('Contact @username', ['@username' => $user->getDisplayName()]);
Chris@0 117 $form['#cache']['contexts'][] = 'user.permissions';
Chris@0 118 return $form;
Chris@0 119 }
Chris@0 120
Chris@0 121 }