5 * Contains custom contact message functionality for ContactStorageTest.
8 use Drupal\contact\ContactFormInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Field\BaseFieldDefinition;
11 use Drupal\Core\Form\FormStateInterface;
14 * Implements hook_entity_base_field_info().
16 function contact_storage_test_entity_base_field_info(EntityTypeInterface $entity_type) {
17 if ($entity_type->id() == 'contact_message') {
20 $fields['id'] = BaseFieldDefinition::create('integer')
21 ->setLabel(t('Message ID'))
22 ->setDescription(t('The message ID.'))
24 ->setSetting('unsigned', TRUE);
32 * Implements hook_entity_type_alter().
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();
41 $entity_types['contact_message']->set('entity_keys', $keys);
42 $entity_types['contact_message']->set('base_table', 'contact_message');
46 * Implements hook_form_FORM_ID_alter() for contact_form_form().
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),
57 $form['#entity_builders'][] = 'contact_storage_test_contact_form_form_builder';
60 * Entity builder for the contact form edit form with third party options.
62 * @see contact_storage_test_form_contact_form_edit_form_alter()
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'));