Mercurial > hg > isophonics-drupal-site
comparison core/modules/contact/src/ContactFormEditForm.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\contact; | |
4 | |
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; | |
15 | |
16 /** | |
17 * Base form for contact form edit forms. | |
18 */ | |
19 class ContactFormEditForm extends EntityForm implements ContainerInjectionInterface { | |
20 use ConfigFormBaseTrait; | |
21 | |
22 /** | |
23 * The email validator. | |
24 * | |
25 * @var \Egulias\EmailValidator\EmailValidator | |
26 */ | |
27 protected $emailValidator; | |
28 | |
29 /** | |
30 * The path validator. | |
31 * | |
32 * @var \Drupal\Core\Path\PathValidatorInterface | |
33 */ | |
34 protected $pathValidator; | |
35 | |
36 /** | |
37 * Constructs a new ContactFormEditForm. | |
38 * | |
39 * @param \Egulias\EmailValidator\EmailValidator $email_validator | |
40 * The email validator. | |
41 * @param \Drupal\Core\Path\PathValidatorInterface $path_validator | |
42 * The path validator service. | |
43 */ | |
44 public function __construct(EmailValidator $email_validator, PathValidatorInterface $path_validator) { | |
45 $this->emailValidator = $email_validator; | |
46 $this->pathValidator = $path_validator; | |
47 } | |
48 | |
49 /** | |
50 * {@inheritdoc} | |
51 */ | |
52 public static function create(ContainerInterface $container) { | |
53 return new static( | |
54 $container->get('email.validator'), | |
55 $container->get('path.validator') | |
56 ); | |
57 } | |
58 | |
59 /** | |
60 * {@inheritdoc} | |
61 */ | |
62 protected function getEditableConfigNames() { | |
63 return ['contact.settings']; | |
64 } | |
65 | |
66 /** | |
67 * {@inheritdoc} | |
68 */ | |
69 public function form(array $form, FormStateInterface $form_state) { | |
70 $form = parent::form($form, $form_state); | |
71 | |
72 $contact_form = $this->entity; | |
73 $default_form = $this->config('contact.settings')->get('default_form'); | |
74 | |
75 $form['label'] = [ | |
76 '#type' => 'textfield', | |
77 '#title' => $this->t('Label'), | |
78 '#maxlength' => 255, | |
79 '#default_value' => $contact_form->label(), | |
80 '#description' => $this->t("Example: 'website feedback' or 'product information'."), | |
81 '#required' => TRUE, | |
82 ]; | |
83 $form['id'] = [ | |
84 '#type' => 'machine_name', | |
85 '#default_value' => $contact_form->id(), | |
86 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, | |
87 '#machine_name' => [ | |
88 'exists' => '\Drupal\contact\Entity\ContactForm::load', | |
89 ], | |
90 '#disabled' => !$contact_form->isNew(), | |
91 ]; | |
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."), | |
97 '#required' => TRUE, | |
98 ]; | |
99 $form['message'] = [ | |
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.'), | |
104 ]; | |
105 $form['redirect'] = [ | |
106 '#type' => 'path', | |
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.'), | |
111 ]; | |
112 $form['reply'] = [ | |
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.'), | |
117 ]; | |
118 $form['weight'] = [ | |
119 '#type' => 'weight', | |
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.'), | |
123 ]; | |
124 $form['selected'] = [ | |
125 '#type' => 'checkbox', | |
126 '#title' => $this->t('Make this the default form'), | |
127 '#default_value' => $default_form === $contact_form->id(), | |
128 ]; | |
129 | |
130 return $form; | |
131 } | |
132 | |
133 /** | |
134 * {@inheritdoc} | |
135 */ | |
136 public function validateForm(array &$form, FormStateInterface $form_state) { | |
137 parent::validateForm($form, $form_state); | |
138 | |
139 // Validate and each email recipient. | |
140 $recipients = explode(',', $form_state->getValue('recipients')); | |
141 | |
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])); | |
146 } | |
147 } | |
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 /.')); | |
153 } | |
154 } | |
155 } | |
156 | |
157 /** | |
158 * {@inheritdoc} | |
159 */ | |
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'); | |
164 | |
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]); | |
170 } | |
171 else { | |
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]); | |
174 } | |
175 | |
176 // Update the default form. | |
177 if ($form_state->getValue('selected')) { | |
178 $contact_settings | |
179 ->set('default_form', $contact_form->id()) | |
180 ->save(); | |
181 } | |
182 // If it was the default form, empty out the setting. | |
183 elseif ($contact_settings->get('default_form') == $contact_form->id()) { | |
184 $contact_settings | |
185 ->set('default_form', NULL) | |
186 ->save(); | |
187 } | |
188 | |
189 $form_state->setRedirectUrl($contact_form->urlInfo('collection')); | |
190 } | |
191 | |
192 } |