Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\contact;
|
Chris@0
|
4
|
Chris@18
|
5 use Drupal\Component\Utility\EmailValidatorInterface;
|
Chris@0
|
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
Chris@0
|
7 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityForm;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@0
|
10 use Drupal\Core\Form\ConfigFormBaseTrait;
|
Chris@0
|
11 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
12 use Drupal\Core\Path\PathValidatorInterface;
|
Chris@0
|
13 use Drupal\Core\Render\Element\PathElement;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Base form for contact form edit forms.
|
Chris@14
|
17 *
|
Chris@14
|
18 * @internal
|
Chris@0
|
19 */
|
Chris@0
|
20 class ContactFormEditForm extends EntityForm implements ContainerInjectionInterface {
|
Chris@0
|
21 use ConfigFormBaseTrait;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The email validator.
|
Chris@0
|
25 *
|
Chris@18
|
26 * @var \Drupal\Component\Utility\EmailValidatorInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $emailValidator;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * The path validator.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var \Drupal\Core\Path\PathValidatorInterface
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $pathValidator;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Constructs a new ContactFormEditForm.
|
Chris@0
|
39 *
|
Chris@18
|
40 * @param \Drupal\Component\Utility\EmailValidatorInterface $email_validator
|
Chris@0
|
41 * The email validator.
|
Chris@0
|
42 * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
|
Chris@0
|
43 * The path validator service.
|
Chris@0
|
44 */
|
Chris@18
|
45 public function __construct(EmailValidatorInterface $email_validator, PathValidatorInterface $path_validator) {
|
Chris@0
|
46 $this->emailValidator = $email_validator;
|
Chris@0
|
47 $this->pathValidator = $path_validator;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * {@inheritdoc}
|
Chris@0
|
52 */
|
Chris@0
|
53 public static function create(ContainerInterface $container) {
|
Chris@0
|
54 return new static(
|
Chris@0
|
55 $container->get('email.validator'),
|
Chris@0
|
56 $container->get('path.validator')
|
Chris@0
|
57 );
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * {@inheritdoc}
|
Chris@0
|
62 */
|
Chris@0
|
63 protected function getEditableConfigNames() {
|
Chris@0
|
64 return ['contact.settings'];
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * {@inheritdoc}
|
Chris@0
|
69 */
|
Chris@0
|
70 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
71 $form = parent::form($form, $form_state);
|
Chris@0
|
72
|
Chris@0
|
73 $contact_form = $this->entity;
|
Chris@0
|
74 $default_form = $this->config('contact.settings')->get('default_form');
|
Chris@0
|
75
|
Chris@0
|
76 $form['label'] = [
|
Chris@0
|
77 '#type' => 'textfield',
|
Chris@0
|
78 '#title' => $this->t('Label'),
|
Chris@0
|
79 '#maxlength' => 255,
|
Chris@0
|
80 '#default_value' => $contact_form->label(),
|
Chris@0
|
81 '#description' => $this->t("Example: 'website feedback' or 'product information'."),
|
Chris@0
|
82 '#required' => TRUE,
|
Chris@0
|
83 ];
|
Chris@0
|
84 $form['id'] = [
|
Chris@0
|
85 '#type' => 'machine_name',
|
Chris@0
|
86 '#default_value' => $contact_form->id(),
|
Chris@0
|
87 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
|
Chris@0
|
88 '#machine_name' => [
|
Chris@0
|
89 'exists' => '\Drupal\contact\Entity\ContactForm::load',
|
Chris@0
|
90 ],
|
Chris@0
|
91 '#disabled' => !$contact_form->isNew(),
|
Chris@0
|
92 ];
|
Chris@0
|
93 $form['recipients'] = [
|
Chris@0
|
94 '#type' => 'textarea',
|
Chris@0
|
95 '#title' => $this->t('Recipients'),
|
Chris@0
|
96 '#default_value' => implode(', ', $contact_form->getRecipients()),
|
Chris@0
|
97 '#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."),
|
Chris@0
|
98 '#required' => TRUE,
|
Chris@0
|
99 ];
|
Chris@0
|
100 $form['message'] = [
|
Chris@0
|
101 '#type' => 'textarea',
|
Chris@0
|
102 '#title' => $this->t('Message'),
|
Chris@0
|
103 '#default_value' => $contact_form->getMessage(),
|
Chris@0
|
104 '#description' => $this->t('The message to display to the user after submission of this form. Leave blank for no message.'),
|
Chris@0
|
105 ];
|
Chris@0
|
106 $form['redirect'] = [
|
Chris@0
|
107 '#type' => 'path',
|
Chris@0
|
108 '#title' => $this->t('Redirect path'),
|
Chris@0
|
109 '#convert_path' => PathElement::CONVERT_NONE,
|
Chris@0
|
110 '#default_value' => $contact_form->getRedirectPath(),
|
Chris@0
|
111 '#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.'),
|
Chris@0
|
112 ];
|
Chris@0
|
113 $form['reply'] = [
|
Chris@0
|
114 '#type' => 'textarea',
|
Chris@0
|
115 '#title' => $this->t('Auto-reply'),
|
Chris@0
|
116 '#default_value' => $contact_form->getReply(),
|
Chris@0
|
117 '#description' => $this->t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
|
Chris@0
|
118 ];
|
Chris@0
|
119 $form['weight'] = [
|
Chris@0
|
120 '#type' => 'weight',
|
Chris@0
|
121 '#title' => $this->t('Weight'),
|
Chris@0
|
122 '#default_value' => $contact_form->getWeight(),
|
Chris@0
|
123 '#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.'),
|
Chris@0
|
124 ];
|
Chris@0
|
125 $form['selected'] = [
|
Chris@0
|
126 '#type' => 'checkbox',
|
Chris@0
|
127 '#title' => $this->t('Make this the default form'),
|
Chris@0
|
128 '#default_value' => $default_form === $contact_form->id(),
|
Chris@0
|
129 ];
|
Chris@0
|
130
|
Chris@0
|
131 return $form;
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * {@inheritdoc}
|
Chris@0
|
136 */
|
Chris@0
|
137 public function validateForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
138 parent::validateForm($form, $form_state);
|
Chris@0
|
139
|
Chris@0
|
140 // Validate and each email recipient.
|
Chris@0
|
141 $recipients = explode(',', $form_state->getValue('recipients'));
|
Chris@0
|
142
|
Chris@0
|
143 foreach ($recipients as &$recipient) {
|
Chris@0
|
144 $recipient = trim($recipient);
|
Chris@0
|
145 if (!$this->emailValidator->isValid($recipient)) {
|
Chris@0
|
146 $form_state->setErrorByName('recipients', $this->t('%recipient is an invalid email address.', ['%recipient' => $recipient]));
|
Chris@0
|
147 }
|
Chris@0
|
148 }
|
Chris@0
|
149 $form_state->setValue('recipients', $recipients);
|
Chris@0
|
150 $redirect_url = $form_state->getValue('redirect');
|
Chris@0
|
151 if ($redirect_url && $this->pathValidator->isValid($redirect_url)) {
|
Chris@17
|
152 if (mb_substr($redirect_url, 0, 1) !== '/') {
|
Chris@0
|
153 $form_state->setErrorByName('redirect', $this->t('The path should start with /.'));
|
Chris@0
|
154 }
|
Chris@0
|
155 }
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@0
|
159 * {@inheritdoc}
|
Chris@0
|
160 */
|
Chris@0
|
161 public function save(array $form, FormStateInterface $form_state) {
|
Chris@0
|
162 $contact_form = $this->entity;
|
Chris@0
|
163 $status = $contact_form->save();
|
Chris@0
|
164 $contact_settings = $this->config('contact.settings');
|
Chris@0
|
165
|
Chris@18
|
166 $edit_link = $this->entity->toLink($this->t('Edit'))->toString();
|
Chris@18
|
167 $view_link = $contact_form->toLink($contact_form->label(), 'canonical')->toString();
|
Chris@0
|
168 if ($status == SAVED_UPDATED) {
|
Chris@17
|
169 $this->messenger()->addStatus($this->t('Contact form %label has been updated.', ['%label' => $view_link]));
|
Chris@0
|
170 $this->logger('contact')->notice('Contact form %label has been updated.', ['%label' => $contact_form->label(), 'link' => $edit_link]);
|
Chris@0
|
171 }
|
Chris@0
|
172 else {
|
Chris@17
|
173 $this->messenger()->addStatus($this->t('Contact form %label has been added.', ['%label' => $view_link]));
|
Chris@0
|
174 $this->logger('contact')->notice('Contact form %label has been added.', ['%label' => $contact_form->label(), 'link' => $edit_link]);
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 // Update the default form.
|
Chris@0
|
178 if ($form_state->getValue('selected')) {
|
Chris@0
|
179 $contact_settings
|
Chris@0
|
180 ->set('default_form', $contact_form->id())
|
Chris@0
|
181 ->save();
|
Chris@0
|
182 }
|
Chris@0
|
183 // If it was the default form, empty out the setting.
|
Chris@0
|
184 elseif ($contact_settings->get('default_form') == $contact_form->id()) {
|
Chris@0
|
185 $contact_settings
|
Chris@0
|
186 ->set('default_form', NULL)
|
Chris@0
|
187 ->save();
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@18
|
190 $form_state->setRedirectUrl($contact_form->toUrl('collection'));
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 }
|