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);
31 * Implements hook_entity_type_alter().
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();
40 $entity_types['contact_message']->set('entity_keys', $keys);
41 $entity_types['contact_message']->set('base_table', 'contact_message');
45 * Implements hook_form_FORM_ID_alter() for contact_form_form().
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),
56 $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'));