3 namespace Drupal\contact;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Drupal\Core\Entity\EntityForm;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Form\ConfigFormBaseTrait;
11 use Drupal\Core\Form\FormStateInterface;
12 use Egulias\EmailValidator\EmailValidator;
13 use Drupal\Core\Path\PathValidatorInterface;
14 use Drupal\Core\Render\Element\PathElement;
17 * Base form for contact form edit forms.
19 class ContactFormEditForm extends EntityForm implements ContainerInjectionInterface {
20 use ConfigFormBaseTrait;
23 * The email validator.
25 * @var \Egulias\EmailValidator\EmailValidator
27 protected $emailValidator;
32 * @var \Drupal\Core\Path\PathValidatorInterface
34 protected $pathValidator;
37 * Constructs a new ContactFormEditForm.
39 * @param \Egulias\EmailValidator\EmailValidator $email_validator
40 * The email validator.
41 * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
42 * The path validator service.
44 public function __construct(EmailValidator $email_validator, PathValidatorInterface $path_validator) {
45 $this->emailValidator = $email_validator;
46 $this->pathValidator = $path_validator;
52 public static function create(ContainerInterface $container) {
54 $container->get('email.validator'),
55 $container->get('path.validator')
62 protected function getEditableConfigNames() {
63 return ['contact.settings'];
69 public function form(array $form, FormStateInterface $form_state) {
70 $form = parent::form($form, $form_state);
72 $contact_form = $this->entity;
73 $default_form = $this->config('contact.settings')->get('default_form');
76 '#type' => 'textfield',
77 '#title' => $this->t('Label'),
79 '#default_value' => $contact_form->label(),
80 '#description' => $this->t("Example: 'website feedback' or 'product information'."),
84 '#type' => 'machine_name',
85 '#default_value' => $contact_form->id(),
86 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
88 'exists' => '\Drupal\contact\Entity\ContactForm::load',
90 '#disabled' => !$contact_form->isNew(),
92 $form['recipients'] = [
93 '#type' => 'textarea',
94 '#title' => $this->t('Recipients'),
95 '#default_value' => implode(', ', $contact_form->getRecipients()),
96 '#description' => $this->t("Example: 'webmaster@example.com' or 'sales@example.com,support@example.com' . To specify multiple recipients, separate each email address with a comma."),
100 '#type' => 'textarea',
101 '#title' => $this->t('Message'),
102 '#default_value' => $contact_form->getMessage(),
103 '#description' => $this->t('The message to display to the user after submission of this form. Leave blank for no message.'),
105 $form['redirect'] = [
107 '#title' => $this->t('Redirect path'),
108 '#convert_path' => PathElement::CONVERT_NONE,
109 '#default_value' => $contact_form->getRedirectPath(),
110 '#description' => $this->t('Path to redirect the user to after submission of this form. For example, type "/about" to redirect to that page. Use a relative path with a slash in front.'),
113 '#type' => 'textarea',
114 '#title' => $this->t('Auto-reply'),
115 '#default_value' => $contact_form->getReply(),
116 '#description' => $this->t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
120 '#title' => $this->t('Weight'),
121 '#default_value' => $contact_form->getWeight(),
122 '#description' => $this->t('When listing forms, those with lighter (smaller) weights get listed before forms with heavier (larger) weights. Forms with equal weights are sorted alphabetically.'),
124 $form['selected'] = [
125 '#type' => 'checkbox',
126 '#title' => $this->t('Make this the default form'),
127 '#default_value' => $default_form === $contact_form->id(),
136 public function validateForm(array &$form, FormStateInterface $form_state) {
137 parent::validateForm($form, $form_state);
139 // Validate and each email recipient.
140 $recipients = explode(',', $form_state->getValue('recipients'));
142 foreach ($recipients as &$recipient) {
143 $recipient = trim($recipient);
144 if (!$this->emailValidator->isValid($recipient)) {
145 $form_state->setErrorByName('recipients', $this->t('%recipient is an invalid email address.', ['%recipient' => $recipient]));
148 $form_state->setValue('recipients', $recipients);
149 $redirect_url = $form_state->getValue('redirect');
150 if ($redirect_url && $this->pathValidator->isValid($redirect_url)) {
151 if (Unicode::substr($redirect_url, 0, 1) !== '/') {
152 $form_state->setErrorByName('redirect', $this->t('The path should start with /.'));
160 public function save(array $form, FormStateInterface $form_state) {
161 $contact_form = $this->entity;
162 $status = $contact_form->save();
163 $contact_settings = $this->config('contact.settings');
165 $edit_link = $this->entity->link($this->t('Edit'));
166 $view_link = $contact_form->link($contact_form->label(), 'canonical');
167 if ($status == SAVED_UPDATED) {
168 drupal_set_message($this->t('Contact form %label has been updated.', ['%label' => $view_link]));
169 $this->logger('contact')->notice('Contact form %label has been updated.', ['%label' => $contact_form->label(), 'link' => $edit_link]);
172 drupal_set_message($this->t('Contact form %label has been added.', ['%label' => $view_link]));
173 $this->logger('contact')->notice('Contact form %label has been added.', ['%label' => $contact_form->label(), 'link' => $edit_link]);
176 // Update the default form.
177 if ($form_state->getValue('selected')) {
179 ->set('default_form', $contact_form->id())
182 // If it was the default form, empty out the setting.
183 elseif ($contact_settings->get('default_form') == $contact_form->id()) {
185 ->set('default_form', NULL)
189 $form_state->setRedirectUrl($contact_form->urlInfo('collection'));