"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.9/core/modules/contact/src/Controller/ContactController.php" (18 Nov 2020, 3841 Bytes) of package /linux/www/drupal-8.9.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ContactController.php" see the Fossies "Dox" file reference documentation.

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