ContactController.php 3.63 KB
Newer Older
1 2 3 4
<?php

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 10
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11 12 13 14

/**
 * Controller routines for contact routes.
 */
15
class ContactController extends ControllerBase {
16

17 18 19 20 21 22 23
  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

24 25 26
  /**
   * Constructs a ContactController object.
   *
27 28
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
29
   */
30
  public function __construct(RendererInterface $renderer) {
31
    $this->renderer = $renderer;
32 33 34 35 36 37 38
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
39
      $container->get('renderer')
40 41 42 43 44 45
    );
  }

  /**
   * Presents the site-wide contact form.
   *
46 47
   * @param \Drupal\contact\ContactFormInterface $contact_form
   *   The contact form to use.
48 49 50 51 52 53
   *
   * @return array
   *   The form as render array as expected by drupal_render().
   *
   * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
   *   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 60 61 62
    // Use the default form if no form has been passed.
    if (empty($contact_form)) {
      $contact_form = $this->entityManager()
        ->getStorage('contact_form')
63
        ->load($config->get('default_form'));
64 65
      // If there are no forms, do not display the form.
      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 69
            ':add' => $this->url('contact.form_add'),
          ]), 'error');
70
          return [];
71 72 73 74 75 76 77 78
        }
        else {
          throw new NotFoundHttpException();
        }
      }
    }

    $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 93 94 95 96 97 98
   * Form constructor for the personal contact form.
   *
   * @param \Drupal\user\UserInterface $user
   *   The account for which a personal contact form should be generated.
   *
   * @return array
   *   The personal contact form as render array as expected by drupal_render().
99 100 101
   *
   * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
   *   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 107 108 109
    if (!$user->getEmail()) {
      throw new NotFoundHttpException();
    }

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 119 120
    return $form;
  }

121
}