contact_storage_test.module 2.4 KB

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