Commit f48a4676 authored by alexpott's avatar alexpott

Issue #2342551 by larowlan: Implement ThirdPartySettingsInterface in contact...

Issue #2342551 by larowlan: Implement ThirdPartySettingsInterface in contact module for contact form config entity.
parent 9deda262
......@@ -22,6 +22,11 @@ contact.form.*:
weight:
type: integer
label: 'Weight'
third_party_settings:
type: sequence
label: 'Third party settings'
sequence:
- type: contact_form.third_party.[%key]
contact.settings:
type: mapping
......
......@@ -8,11 +8,12 @@
namespace Drupal\contact;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
/**
* Provides an interface defining a contact form entity.
*/
interface ContactFormInterface extends ConfigEntityInterface {
interface ContactFormInterface extends ConfigEntityInterface, ThirdPartySettingsInterface {
/**
* Returns list of recipient e-mail addresses.
......
......@@ -9,8 +9,7 @@
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\contact\ContactFormInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\contact\CategoryInterface;
use Drupal\Core\Config\Entity\ThirdPartySettingsTrait;
/**
* Defines the contact form entity.
......@@ -42,6 +41,8 @@
*/
class ContactForm extends ConfigEntityBundleBase implements ContactFormInterface {
use ThirdPartySettingsTrait;
/**
* The form ID.
*
......
......@@ -331,14 +331,17 @@ function testAutoReply() {
* form.
* @param boolean $selected
* A Boolean indicating whether the form should be selected by default.
* @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) {
function addContactForm($id, $label, $recipients, $reply, $selected, $third_party_settings = []) {
$edit = array();
$edit['label'] = $label;
$edit['id'] = $id;
$edit['recipients'] = $recipients;
$edit['reply'] = $reply;
$edit['selected'] = ($selected ? TRUE : FALSE);
$edit += $third_party_settings;
$this->drupalPostForm('admin/structure/contact/add', $edit, t('Save'));
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\contact\Tests;
use Drupal\config\Tests\SchemaCheckTestTrait;
use Drupal\contact\Entity\Message;
/**
......@@ -14,6 +15,8 @@
*/
class ContactStorageTest extends ContactSitewideTest {
use SchemaCheckTestTrait;
/**
* Modules to enable.
*
......@@ -49,7 +52,9 @@ public function testContactStorage() {
$this->drupalLogin($admin_user);
// Create first valid contact form.
$mail = 'simpletest@example.com';
$this->addContactForm($id = drupal_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', array($mail)), '', TRUE);
$this->addContactForm($id = drupal_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', array($mail)), '', TRUE, [
'send_a_pony' => 1,
]);
$this->assertRaw(t('Contact form %label has been added.', array('%label' => $label)));
// Ensure that anonymous can submit site-wide contact form.
......@@ -65,9 +70,14 @@ public function testContactStorage() {
/** @var \Drupal\contact\Entity\Message $message */
$message = reset($messages);
$this->assertEqual($message->getContactForm()->id(), $id);
$this->assertTrue($message->getContactForm()->getThirdPartySetting('contact_storage_test', 'send_a_pony', FALSE));
$this->assertEqual($message->getSenderName(), $name);
$this->assertEqual($message->getSubject(), $subject);
$this->assertEqual($message->getSenderMail(), $mail);
$config = \Drupal::config("contact.form.$id");
$this->assertEqual($config->get('id'), $id);
$this->assertConfigSchema(\Drupal::service('config.typed'), $config->getName(), $config->get());
}
}
# Schema for configuration files of the Contact Storage Test module.
contact_form.third_party.contact_storage_test:
type: mapping
label: 'Per-contact form storage settings'
mapping:
send_a_pony:
type: boolean
label: 'Send a Pony'
......@@ -5,7 +5,9 @@
* Contains custom contact message functionality for ContactStorageTest.
*/
use Drupal\contact\ContactFormInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_entity_base_field_info().
......@@ -38,3 +40,26 @@ function contact_storage_test_entity_type_alter(array &$entity_types) {
$entity_types['contact_message']->set('entity_keys', $keys);
$entity_types['contact_message']->set('base_table', 'contact_message');
}
/**
* Implements hook_form_FORM_ID_alter() for contact_form_form().
*/
function contact_storage_test_form_contact_form_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\contact\ContactFormInterface $contact_form */
$contact_form = $form_state->getFormObject()->getEntity();
$form['send_a_pony'] = array(
'#type' => 'checkbox',
'#title' => t('Send submitters a voucher for a free pony.'),
'#description' => t('Enable to send an additional email with a free pony voucher to anyone who submits the form.'),
'#default_value' => $contact_form->getThirdPartySetting('contact_storage_test', 'send_a_pony', FALSE),
);
$form['#entity_builders'][] = 'contact_storage_test_contact_form_form_builder';
}
/**
* Entity builder for the contact form edit form with third party options.
*
* @see contact_storage_test_form_contact_form_edit_form_alter()
*/
function contact_storage_test_contact_form_form_builder($entity_type, ContactFormInterface $contact_form, &$form, FormStateInterface $form_state) {
$contact_form->setThirdPartySetting('contact_storage_test', 'send_a_pony', $form_state->getValue('send_a_pony'));
}
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