Version 1
[yaffs-website] / web / core / modules / contact / tests / modules / contact_storage_test / contact_storage_test.module
1 <?php
2
3 /**
4  * @file
5  * Contains custom contact message functionality for ContactStorageTest.
6  */
7
8 use Drupal\contact\ContactFormInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Field\BaseFieldDefinition;
11 use Drupal\Core\Form\FormStateInterface;
12
13 /**
14  * Implements hook_entity_base_field_info().
15  */
16 function contact_storage_test_entity_base_field_info(EntityTypeInterface $entity_type) {
17   if ($entity_type->id() == 'contact_message') {
18     $fields = [];
19
20     $fields['id'] = BaseFieldDefinition::create('integer')
21       ->setLabel(t('Message ID'))
22       ->setDescription(t('The message ID.'))
23       ->setReadOnly(TRUE)
24       ->setSetting('unsigned', TRUE);
25
26     return $fields;
27   }
28 }
29
30
31 /**
32  * Implements hook_entity_type_alter().
33  */
34 function contact_storage_test_entity_type_alter(array &$entity_types) {
35   /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
36   // Set the controller class for nodes to an alternate implementation of the
37   // Drupal\Core\Entity\EntityStorageInterface interface.
38   $entity_types['contact_message']->setStorageClass('\Drupal\Core\Entity\Sql\SqlContentEntityStorage');
39   $keys = $entity_types['contact_message']->getKeys();
40   $keys['id'] = 'id';
41   $entity_types['contact_message']->set('entity_keys', $keys);
42   $entity_types['contact_message']->set('base_table', 'contact_message');
43 }
44
45 /**
46  * Implements hook_form_FORM_ID_alter() for contact_form_form().
47  */
48 function contact_storage_test_form_contact_form_form_alter(&$form, FormStateInterface $form_state) {
49   /** @var \Drupal\contact\ContactFormInterface $contact_form */
50   $contact_form = $form_state->getFormObject()->getEntity();
51   $form['send_a_pony'] = [
52     '#type' => 'checkbox',
53     '#title' => t('Send submitters a voucher for a free pony.'),
54     '#description' => t('Enable to send an additional email with a free pony voucher to anyone who submits the form.'),
55     '#default_value' => $contact_form->getThirdPartySetting('contact_storage_test', 'send_a_pony', FALSE),
56   ];
57   $form['#entity_builders'][] = 'contact_storage_test_contact_form_form_builder';
58 }
59 /**
60  * Entity builder for the contact form edit form with third party options.
61  *
62  * @see contact_storage_test_form_contact_form_edit_form_alter()
63  */
64 function contact_storage_test_contact_form_form_builder($entity_type, ContactFormInterface $contact_form, &$form, FormStateInterface $form_state) {
65   $contact_form->setThirdPartySetting('contact_storage_test', 'send_a_pony', $form_state->getValue('send_a_pony'));
66 }