Chris@0: emailValidator = $email_validator; Chris@0: $this->pathValidator = $path_validator; 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('email.validator'), Chris@0: $container->get('path.validator') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function getEditableConfigNames() { Chris@0: return ['contact.settings']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function form(array $form, FormStateInterface $form_state) { Chris@0: $form = parent::form($form, $form_state); Chris@0: Chris@0: $contact_form = $this->entity; Chris@0: $default_form = $this->config('contact.settings')->get('default_form'); Chris@0: Chris@0: $form['label'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('Label'), Chris@0: '#maxlength' => 255, Chris@0: '#default_value' => $contact_form->label(), Chris@0: '#description' => $this->t("Example: 'website feedback' or 'product information'."), Chris@0: '#required' => TRUE, Chris@0: ]; Chris@0: $form['id'] = [ Chris@0: '#type' => 'machine_name', Chris@0: '#default_value' => $contact_form->id(), Chris@0: '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, Chris@0: '#machine_name' => [ Chris@0: 'exists' => '\Drupal\contact\Entity\ContactForm::load', Chris@0: ], Chris@0: '#disabled' => !$contact_form->isNew(), Chris@0: ]; Chris@0: $form['recipients'] = [ Chris@0: '#type' => 'textarea', Chris@0: '#title' => $this->t('Recipients'), Chris@0: '#default_value' => implode(', ', $contact_form->getRecipients()), Chris@0: '#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: '#required' => TRUE, Chris@0: ]; Chris@0: $form['message'] = [ Chris@0: '#type' => 'textarea', Chris@0: '#title' => $this->t('Message'), Chris@0: '#default_value' => $contact_form->getMessage(), Chris@0: '#description' => $this->t('The message to display to the user after submission of this form. Leave blank for no message.'), Chris@0: ]; Chris@0: $form['redirect'] = [ Chris@0: '#type' => 'path', Chris@0: '#title' => $this->t('Redirect path'), Chris@0: '#convert_path' => PathElement::CONVERT_NONE, Chris@0: '#default_value' => $contact_form->getRedirectPath(), Chris@0: '#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: ]; Chris@0: $form['reply'] = [ Chris@0: '#type' => 'textarea', Chris@0: '#title' => $this->t('Auto-reply'), Chris@0: '#default_value' => $contact_form->getReply(), Chris@0: '#description' => $this->t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'), Chris@0: ]; Chris@0: $form['weight'] = [ Chris@0: '#type' => 'weight', Chris@0: '#title' => $this->t('Weight'), Chris@0: '#default_value' => $contact_form->getWeight(), Chris@0: '#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: ]; Chris@0: $form['selected'] = [ Chris@0: '#type' => 'checkbox', Chris@0: '#title' => $this->t('Make this the default form'), Chris@0: '#default_value' => $default_form === $contact_form->id(), Chris@0: ]; Chris@0: Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function validateForm(array &$form, FormStateInterface $form_state) { Chris@0: parent::validateForm($form, $form_state); Chris@0: Chris@0: // Validate and each email recipient. Chris@0: $recipients = explode(',', $form_state->getValue('recipients')); Chris@0: Chris@0: foreach ($recipients as &$recipient) { Chris@0: $recipient = trim($recipient); Chris@0: if (!$this->emailValidator->isValid($recipient)) { Chris@0: $form_state->setErrorByName('recipients', $this->t('%recipient is an invalid email address.', ['%recipient' => $recipient])); Chris@0: } Chris@0: } Chris@0: $form_state->setValue('recipients', $recipients); Chris@0: $redirect_url = $form_state->getValue('redirect'); Chris@0: if ($redirect_url && $this->pathValidator->isValid($redirect_url)) { Chris@17: if (mb_substr($redirect_url, 0, 1) !== '/') { Chris@0: $form_state->setErrorByName('redirect', $this->t('The path should start with /.')); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function save(array $form, FormStateInterface $form_state) { Chris@0: $contact_form = $this->entity; Chris@0: $status = $contact_form->save(); Chris@0: $contact_settings = $this->config('contact.settings'); Chris@0: Chris@18: $edit_link = $this->entity->toLink($this->t('Edit'))->toString(); Chris@18: $view_link = $contact_form->toLink($contact_form->label(), 'canonical')->toString(); Chris@0: if ($status == SAVED_UPDATED) { Chris@17: $this->messenger()->addStatus($this->t('Contact form %label has been updated.', ['%label' => $view_link])); Chris@0: $this->logger('contact')->notice('Contact form %label has been updated.', ['%label' => $contact_form->label(), 'link' => $edit_link]); Chris@0: } Chris@0: else { Chris@17: $this->messenger()->addStatus($this->t('Contact form %label has been added.', ['%label' => $view_link])); Chris@0: $this->logger('contact')->notice('Contact form %label has been added.', ['%label' => $contact_form->label(), 'link' => $edit_link]); Chris@0: } Chris@0: Chris@0: // Update the default form. Chris@0: if ($form_state->getValue('selected')) { Chris@0: $contact_settings Chris@0: ->set('default_form', $contact_form->id()) Chris@0: ->save(); Chris@0: } Chris@0: // If it was the default form, empty out the setting. Chris@0: elseif ($contact_settings->get('default_form') == $contact_form->id()) { Chris@0: $contact_settings Chris@0: ->set('default_form', NULL) Chris@0: ->save(); Chris@0: } Chris@0: Chris@18: $form_state->setRedirectUrl($contact_form->toUrl('collection')); Chris@0: } Chris@0: Chris@0: }