3b49f0aef65c33e2cd2658906b8fc69238fe6b54
[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  * Implements hook_entity_type_alter().
32  */
33 function contact_storage_test_entity_type_alter(array &$entity_types) {
34   /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
35   // Set the controller class for nodes to an alternate implementation of the
36   // Drupal\Core\Entity\EntityStorageInterface interface.
37   $entity_types['contact_message']->setStorageClass('\Drupal\Core\Entity\Sql\SqlContentEntityStorage');
38   $keys = $entity_types['contact_message']->getKeys();
39   $keys['id'] = 'id';
40   $entity_types['contact_message']->set('entity_keys', $keys);
41   $entity_types['contact_message']->set('base_table', 'contact_message');
42 }
43
44 /**
45  * Implements hook_form_FORM_ID_alter() for contact_form_form().
46  */
47 function contact_storage_test_form_contact_form_form_alter(&$form, FormStateInterface $form_state) {
48   /** @var \Drupal\contact\ContactFormInterface $contact_form */
49   $contact_form = $form_state->getFormObject()->getEntity();
50   $form['send_a_pony'] = [
51     '#type' => 'checkbox',
52     '#title' => t('Send submitters a voucher for a free pony.'),
53     '#description' => t('Enable to send an additional email with a free pony voucher to anyone who submits the form.'),
54     '#default_value' => $contact_form->getThirdPartySetting('contact_storage_test', 'send_a_pony', FALSE),
55   ];
56   $form['#entity_builders'][] = 'contact_storage_test_contact_form_form_builder';
57 }
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 }