annotate core/modules/contact/src/ContactFormEditForm.php @ 11:bfffd8d7479a

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