Commit db6e47dc authored by larowlan's avatar larowlan Committed by larowlan

Issue #2669794 by ultimike, jamadar, larowlan: Add ability to change the "Send message" button text

parent a1f77e2c
......@@ -8,3 +8,6 @@ contact.form.*.third_party.contact_storage:
submit_text:
type: string
label: 'Submit Text'
show_preview:
type: boolean
label: 'Show preview button'
......@@ -5,6 +5,7 @@
* Contains main module logic.
*/
use Drupal\contact\MessageForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
......@@ -19,19 +20,25 @@ use Drupal\contact\Entity\ContactForm;
function contact_storage_form_contact_form_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\contact\ContactFormInterface $contact_form */
$contact_form = $form_state->getFormObject()->getEntity();
$form['contact_storage_uri'] = array(
$form['contact_storage_uri'] = [
'#type' => 'textfield',
'#title' => t('Redirect Page'),
'#description' => t('Input the Uri (entity:node/NODE-ID) of the Page to redirect the form after Submit.'),
'#default_value' => $contact_form->getThirdPartySetting('contact_storage', 'redirect_uri', FALSE),
);
$form['#entity_builders'][] = 'contact_storage_contact_form_form_builder';
$form['contact_storage_submit_text'] = array(
];
$form['contact_storage_submit_text'] = [
'#type' => 'textfield',
'#title' => t('Submit button text'),
'#description' => t('Override the submit button\'s default "Send message" text.'),
'#description' => t("Override the submit button's default <em>Send message</em> text."),
'#default_value' => $contact_form->getThirdPartySetting('contact_storage', 'submit_text', 'Send message'),
);
];
$form['contact_storage_preview'] = [
'#type' => 'checkbox',
'#title' => t('Allow preview'),
'#description' => t('Show the preview button?'),
'#default_value' => $contact_form->getThirdPartySetting('contact_storage', 'show_preview', TRUE),
];
$form['#entity_builders'][] = 'contact_storage_contact_form_form_builder';
}
/**
* Entity builder for the contact form edit form with third party options.
......@@ -41,6 +48,7 @@ function contact_storage_form_contact_form_form_alter(&$form, FormStateInterface
function contact_storage_contact_form_form_builder($entity_type, ContactFormInterface $contact_form, &$form, FormStateInterface $form_state) {
$contact_form->setThirdPartySetting('contact_storage', 'redirect_uri', $form_state->getValue('contact_storage_uri'));
$contact_form->setThirdPartySetting('contact_storage', 'submit_text', $form_state->getValue('contact_storage_submit_text'));
$contact_form->setThirdPartySetting('contact_storage', 'show_preview', $form_state->getValue('contact_storage_preview'));
}
/**
......@@ -48,12 +56,20 @@ function contact_storage_contact_form_form_builder($entity_type, ContactFormInte
*/
function contact_storage_form_contact_message_form_alter(&$form, &$form_state, $form_id) {
$form['actions']['submit']['#submit'][] = 'contact_storage_contact_message_redirect_submit';
/** @var \Drupal\Core\Entity\ContentEntityForm $form_object */
$form_object = $form_state->getFormObject();
/* @var \Drupal\contact\MessageInterface $contact_message */
$contact_message = $form_state->getFormObject()->getEntity();
$contact_message = $form_object->getEntity();
$contact_form = ContactForm::load($contact_message->bundle());
if ($submit_text = $contact_form->getThirdPartySetting('contact_storage', 'submit_text', FALSE)) {
$form['actions']['submit']['#value'] = $submit_text;
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_mode */
if ($form_object instanceof MessageForm) {
if ($submit_text = $contact_form->getThirdPartySetting('contact_storage', 'submit_text', FALSE)) {
$form['actions']['submit']['#value'] = $submit_text;
}
if (!$contact_form->getThirdPartySetting('contact_storage', 'show_preview', TRUE)) {
$form['actions']['preview']['#access'] = FALSE;
}
}
}
......
......@@ -84,9 +84,9 @@ class ContactStorageTest extends ContactStorageTestBase {
// Click the view link and make sure name, subject and email are displayed
// by default.
$this->clickLink(t('View'));
foreach ($display_fields as $label) {
$this->assertText($label);
}
foreach ($display_fields as $label) {
$this->assertText($label);
}
// Make sure the stored message is correct.
$this->drupalGet('admin/structure/contact/messages');
......@@ -96,8 +96,9 @@ class ContactStorageTest extends ContactStorageTestBase {
$this->assertFieldById('edit-subject-0-value', 'Test_subject');
$this->assertFieldById('edit-message-0-value', 'Test_message');
// Submit should redirect back to listing.
$this->drupalPostForm('admin/structure/contact/messages', array(), $headers = array(), $form_html_id =NULL, t('Save'));
$this->assertUrl('admin/structure/contact/messages',[]);
$this->drupalPostForm(NULL, array(), t('Save'));
$this->assertUrl('admin/structure/contact/messages');
// Delete the message.
$this->clickLink(t('Delete'));
$this->drupalPostForm(NULL, NULL, t('Delete'));
......@@ -120,14 +121,22 @@ class ContactStorageTest extends ContactStorageTestBase {
$this->assertText('Your message has been sent.');
$this->assertEqual($this->url, $admin_user->urlInfo()->setAbsolute()->toString());
// Fill the "Submit button text" field and assert the form can still be submitted.
$edit = ['contact_storage_submit_text' => 'Submit the form'];
// Fill the "Submit button text" field and assert the form can still be
// submitted.
$edit = [
'contact_storage_submit_text' => 'Submit the form',
'contact_storage_preview' => FALSE,
];
$this->drupalPostForm('admin/structure/contact/manage/test_id', $edit, t('Save'));
$edit = [
'subject[0][value]' => 'Test subject',
'message[0][value]' => 'Test message',
];
$this->drupalPostForm('contact', $edit, t('Submit the form'));
$this->drupalGet('contact');
$element = $this->cssSelect('#edit-preview');
// Preview button is hidden.
$this->assertTrue(empty($element));
$this->drupalPostForm(NULL, $edit, t('Submit the form'));
$this->assertText('Your message has been sent.');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment