Chris@0: renderer = $renderer; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('renderer') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Presents the site-wide contact form. Chris@0: * Chris@0: * @param \Drupal\contact\ContactFormInterface $contact_form Chris@0: * The contact form to use. Chris@0: * Chris@0: * @return array Chris@16: * The form as render array as expected by Chris@16: * \Drupal\Core\Render\RendererInterface::render(). Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException Chris@0: * Exception is thrown when user tries to access non existing default Chris@0: * contact form. Chris@0: */ Chris@0: public function contactSitePage(ContactFormInterface $contact_form = NULL) { Chris@0: $config = $this->config('contact.settings'); Chris@0: Chris@0: // Use the default form if no form has been passed. Chris@0: if (empty($contact_form)) { Chris@18: $contact_form = $this->entityTypeManager() Chris@0: ->getStorage('contact_form') Chris@0: ->load($config->get('default_form')); Chris@0: // If there are no forms, do not display the form. Chris@0: if (empty($contact_form)) { Chris@0: if ($this->currentUser()->hasPermission('administer contact forms')) { Chris@17: $this->messenger()->addError($this->t('The contact form has not been configured. Add one or more forms .', [ Chris@0: ':add' => $this->url('contact.form_add'), Chris@17: ])); Chris@0: return []; Chris@0: } Chris@0: else { Chris@0: throw new NotFoundHttpException(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@18: $message = $this->entityTypeManager() Chris@0: ->getStorage('contact_message') Chris@0: ->create([ Chris@0: 'contact_form' => $contact_form->id(), Chris@0: ]); Chris@0: Chris@0: $form = $this->entityFormBuilder()->getForm($message); Chris@0: $form['#title'] = $contact_form->label(); Chris@0: $form['#cache']['contexts'][] = 'user.permissions'; Chris@0: $this->renderer->addCacheableDependency($form, $config); Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form constructor for the personal contact form. Chris@0: * Chris@0: * @param \Drupal\user\UserInterface $user Chris@0: * The account for which a personal contact form should be generated. Chris@0: * Chris@0: * @return array Chris@16: * The personal contact form as render array as expected by Chris@16: * \Drupal\Core\Render\RendererInterface::render(). Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException Chris@0: * Exception is thrown when user tries to access a contact form for a Chris@0: * user who does not have an email address configured. Chris@0: */ Chris@0: public function contactPersonalPage(UserInterface $user) { Chris@0: // Do not continue if the user does not have an email address configured. Chris@0: if (!$user->getEmail()) { Chris@0: throw new NotFoundHttpException(); Chris@0: } Chris@0: Chris@18: $message = $this->entityTypeManager()->getStorage('contact_message')->create([ Chris@0: 'contact_form' => 'personal', Chris@0: 'recipient' => $user->id(), Chris@0: ]); Chris@0: Chris@0: $form = $this->entityFormBuilder()->getForm($message); Chris@0: $form['#title'] = $this->t('Contact @username', ['@username' => $user->getDisplayName()]); Chris@0: $form['#cache']['contexts'][] = 'user.permissions'; Chris@0: return $form; Chris@0: } Chris@0: Chris@0: }