PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/contact/src/ContactFormEditForm.php

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