diff --git a/core/modules/contact/config/install/contact.form.personal.yml b/core/modules/contact/config/install/contact.form.personal.yml index c766fdd..6b4e543 100644 --- a/core/modules/contact/config/install/contact.form.personal.yml +++ b/core/modules/contact/config/install/contact.form.personal.yml @@ -6,3 +6,7 @@ label: 'Personal contact form' recipients: { } reply: '' weight: 0 +message: 'Your message has been sent.' +route: + route_name: '' + route_params: { } diff --git a/core/modules/contact/config/schema/contact.schema.yml b/core/modules/contact/config/schema/contact.schema.yml index 5a62e9d..5103a21 100644 --- a/core/modules/contact/config/schema/contact.schema.yml +++ b/core/modules/contact/config/schema/contact.schema.yml @@ -10,6 +10,9 @@ contact.form.*: label: type: label label: 'Label' + message: + type: string + label: 'Message' recipients: type: sequence label: 'Recipients' @@ -19,6 +22,9 @@ contact.form.*: reply: type: text label: 'Auto-reply' + route: + type: route + label: 'Redirect path' weight: type: integer label: 'Weight' diff --git a/core/modules/contact/src/ContactFormEditForm.php b/core/modules/contact/src/ContactFormEditForm.php index b4adde6..f5b5e02 100644 --- a/core/modules/contact/src/ContactFormEditForm.php +++ b/core/modules/contact/src/ContactFormEditForm.php @@ -14,6 +14,7 @@ use Drupal\Core\Form\ConfigFormBaseTrait; use Drupal\Core\Form\FormStateInterface; use Egulias\EmailValidator\EmailValidator; +use Drupal\Core\Render\Element\PathElement; /** * Base form for contact form edit forms. @@ -80,6 +81,12 @@ public function form(array $form, FormStateInterface $form_state) { ), '#disabled' => !$contact_form->isNew(), ); + $form['message'] = array( + '#type' => 'textarea', + '#title' => $this->t('Message'), + '#default_value' => $contact_form->getMessage(), + '#description' => $this->t('The message to display to the user after submission of this form. Leave blank for no message.'), + ); $form['recipients'] = array( '#type' => 'textarea', '#title' => $this->t('Recipients'), @@ -87,6 +94,17 @@ public function form(array $form, FormStateInterface $form_state) { '#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."), '#required' => TRUE, ); + $path = ''; + if ($url = $contact_form->getRedirectUrl()) { + $path = $url->toString(); + } + $form['url'] = array( + '#type' => 'path', + '#title' => $this->t('Redirect path'), + '#convert_path' => PathElement::CONVERT_URL, + '#default_value' => $contact_form->isNew() ? '' : $path, + '#description' => $this->t('The path you would like to redirect to after this form has been submitted. Enter to direct to front page.'), + ); $form['reply'] = array( '#type' => 'textarea', '#title' => $this->t('Auto-reply'), diff --git a/core/modules/contact/src/ContactFormInterface.php b/core/modules/contact/src/ContactFormInterface.php index 8f588ee..78d670d 100644 --- a/core/modules/contact/src/ContactFormInterface.php +++ b/core/modules/contact/src/ContactFormInterface.php @@ -8,6 +8,7 @@ namespace Drupal\contact; use Drupal\Core\Config\Entity\ConfigEntityInterface; +use Drupal\Core\Url; /** * Provides an interface defining a contact form entity. @@ -15,6 +16,14 @@ interface ContactFormInterface extends ConfigEntityInterface { /** + * Returns the message to be displayed to user. + * + * @return string + * A user message. + */ + public function getMessage(); + + /** * Returns list of recipient email addresses. * * @return array @@ -26,11 +35,19 @@ public function getRecipients(); * Returns an auto-reply message to send to the message author. * * @return string - * An auto-reply message + * An auto-reply message. */ public function getReply(); /** + * Returns the route for redirect. + * + * @return \Drupal\core\Url + * The redirect URL + */ + public function getRedirectUrl(); + + /** * Returns the weight of this category (used for sorting). * * @return int @@ -39,6 +56,16 @@ public function getReply(); public function getWeight(); /** + * Sets the message to be displayed to the user. + * + * @param string $message + * The message to display after form is submitted. + * + * @return $this + */ + public function setMessage($message); + + /** * Sets list of recipient email addresses. * * @param array $recipients @@ -59,6 +86,16 @@ public function setRecipients($recipients); public function setReply($reply); /** + * Sets the redirect route. + * + * @param \Drupal\core\Url $url + * The desired route. + * + * @return $this + */ + public function setRedirectUrl(Url $url); + + /** * Sets the weight. * * @param int $weight diff --git a/core/modules/contact/src/Entity/ContactForm.php b/core/modules/contact/src/Entity/ContactForm.php index 1a9f73a..0b13c35 100644 --- a/core/modules/contact/src/Entity/ContactForm.php +++ b/core/modules/contact/src/Entity/ContactForm.php @@ -9,6 +9,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; use Drupal\contact\ContactFormInterface; +use Drupal\Core\Url; /** * Defines the contact form entity. @@ -41,7 +42,9 @@ * config_export = { * "id", * "label", + * "message", * "recipients", + * "route", * "reply", * "weight", * } @@ -64,6 +67,13 @@ class ContactForm extends ConfigEntityBundleBase implements ContactFormInterface protected $label; /** + * The message displayed to user. + * + * @var string + */ + protected $message; + + /** * List of recipient email addresses. * * @var array @@ -71,6 +81,13 @@ class ContactForm extends ConfigEntityBundleBase implements ContactFormInterface protected $recipients = array(); /** + * The URL to redirect to. + * + * @var \Drupal\core\Url + */ + protected $url; + + /** * An auto-reply message. * * @var string @@ -84,6 +101,24 @@ class ContactForm extends ConfigEntityBundleBase implements ContactFormInterface */ protected $weight = 0; + public function __construct(array $values, $entity_type) { + parent::__construct($values, $entity_type); + + if (!empty($values['route']['route_name'])) { + $this->url = new Url($values['route']['route_name']); + if (!empty($values['route']['route_parameters'])) { + $this->url->setRouteParameters($values['route']['route_parameters']); + } + } + } + + /** + * {@inheritdoc} + */ + public function getMessage() { + return $this->get('message'); + } + /** * {@inheritdoc} */ @@ -109,6 +144,21 @@ public function getReply() { /** * {@inheritdoc} */ + public function getRedirectUrl() { + return $this->url; + } + + /** + * {@inheritdoc} + */ + public function setMessage($message) { + $this->set('message', $message); + return $this; + } + + /** + * {@inheritdoc} + */ public function setReply($reply) { $this->reply = $reply; return $this; @@ -124,9 +174,36 @@ public function getWeight() { /** * {@inheritdoc} */ + public function setRedirectUrl(Url $url) { + $this->url = $url; + return $this; + } + + /** + * {@inheritdoc} + */ public function setWeight($weight) { $this->weight = $weight; return $this; } + /** + * {@inheritdoc} + */ + public function toArray() { + $properties = parent::toArray(); + if ($this->url) { + $properties['route'] = [ + 'route_name' => $this->url->getRouteName(), + 'route_params' => $this->url->getRouteParameters(), + ]; + } + else { + $properties['route'] = [ + 'route_name' => '', + 'route_params' => [], + ]; + } + return $properties; + } } diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 46bb5d9..bba9df4 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -211,15 +211,21 @@ public function save(array $form, FormStateInterface $form_state) { $message = $this->entity; $user = $this->currentUser(); $this->mailHandler->sendMailMessages($message, $user); + $contact_form = $message->getContactForm(); $this->flood->register('contact', $this->config('contact.settings')->get('flood.interval')); - drupal_set_message($this->t('Your message has been sent.')); + if ($contact_message = $contact_form->getMessage()) { + drupal_set_message($contact_message); + } // To avoid false error messages caused by flood control, redirect away from // the contact form; either to the contacted user account or the front page. if ($message->isPersonal() && $user->hasPermission('access user profiles')) { $form_state->setRedirectUrl($message->getPersonalRecipient()->urlInfo()); } + elseif ($url = $contact_form->getRedirectUrl()) { + $form_state->setRedirectUrl($url); + } else { $form_state->setRedirect(''); } @@ -227,6 +233,7 @@ public function save(array $form, FormStateInterface $form_state) { // implement message storage, this will make the task of swapping in a real // storage controller straight-forward. $message->save(); + } } diff --git a/core/modules/contact/src/Tests/ContactSitewideTest.php b/core/modules/contact/src/Tests/ContactSitewideTest.php index 39cd34a..595e8be 100644 --- a/core/modules/contact/src/Tests/ContactSitewideTest.php +++ b/core/modules/contact/src/Tests/ContactSitewideTest.php @@ -10,6 +10,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\contact\Entity\ContactForm; use Drupal\Core\Mail\MailFormatHelper; +use Drupal\Core\Url; use Drupal\field_ui\Tests\FieldUiTestTrait; use Drupal\simpletest\WebTestBase; use Drupal\Core\Entity\EntityTypeInterface; @@ -299,6 +300,27 @@ function testSiteWideContact() { $this->assertEqual($mail['subject'], t('[@label] @subject', array('@label' => $label, '@subject' => $edit['subject[0][value]']))); $this->assertTrue(strpos($mail['body'], $field_label)); $this->assertTrue(strpos($mail['body'], $edit[$field_name . '[0][value]'])); + + // Test messages and redirect. + /** @var \Drupal\contact\ContactFormInterface $form */ + $form = ContactForm::load($contact_form); + $form->setMessage('Thanks for your submission'); + $form->setRedirectUrl(Url::fromRoute('entity.user.canonical', + ['user' => $admin_user->id(),] + )); + $form->save(); + // Check that the field is displayed. + $this->drupalGet('contact/' . $contact_form); + + // Submit the contact form and verify the content. + $edit = array( + 'subject[0][value]' => $this->randomMachineName(), + 'message[0][value]' => $this->randomMachineName(), + $field_name . '[0][value]' => $this->randomMachineName(), + ); + $this->drupalPostForm(NULL, $edit, t('Send message')); + $this->assertText('Thanks for your submission'); + $this->assertUrl('user/' . $admin_user->id()); } /** @@ -360,16 +382,20 @@ function testAutoReply() { * form. * @param bool $selected * A Boolean indicating whether the form should be selected by default. + * @param string $message + * The message that will be displayed to a user upon completing the contact + * form. * @param array $third_party_settings * Array of third party settings to be added to the posted form data. */ - function addContactForm($id, $label, $recipients, $reply, $selected, $third_party_settings = []) { + function addContactForm($id, $label, $recipients, $reply, $selected, $message = 'Your message has been sent.', $third_party_settings = []) { $edit = array(); $edit['label'] = $label; $edit['id'] = $id; $edit['recipients'] = $recipients; $edit['reply'] = $reply; $edit['selected'] = ($selected ? TRUE : FALSE); + $edit['message'] = $message; $edit += $third_party_settings; $this->drupalPostForm('admin/structure/contact/add', $edit, t('Save')); }