contact_storage_test.module

Same filename in other branches
  1. 9 core/modules/contact/tests/modules/contact_storage_test/contact_storage_test.module
  2. 8.9.x core/modules/contact/tests/modules/contact_storage_test/contact_storage_test.module
  3. 10 core/modules/contact/tests/modules/contact_storage_test/contact_storage_test.module

File

core/modules/contact/tests/modules/contact_storage_test/contact_storage_test.module

View source
<?php


/**
 * @file
 * Contains custom contact message functionality for ContactStorageTest.
 */
declare (strict_types=1);
use Drupal\contact\ContactFormInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_entity_base_field_info().
 */
function contact_storage_test_entity_base_field_info(EntityTypeInterface $entity_type) {
    if ($entity_type->id() == 'contact_message') {
        $fields = [];
        $fields['id'] = BaseFieldDefinition::create('integer')->setLabel(t('Message ID'))
            ->setDescription(t('The message ID.'))
            ->setReadOnly(TRUE)
            ->setSetting('unsigned', TRUE);
        return $fields;
    }
}

/**
 * Implements hook_entity_type_alter().
 */
function contact_storage_test_entity_type_alter(array &$entity_types) {
    
    /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
    // Set the controller class for nodes to an alternate implementation of the
    // Drupal\Core\Entity\EntityStorageInterface interface.
    $entity_types['contact_message']->setStorageClass('\\Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorage');
    $keys = $entity_types['contact_message']->getKeys();
    $keys['id'] = 'id';
    $entity_types['contact_message']->set('entity_keys', $keys);
    $entity_types['contact_message']->set('base_table', 'contact_message');
}

/**
 * Implements hook_form_FORM_ID_alter() for contact_form_form().
 */
function contact_storage_test_form_contact_form_form_alter(&$form, FormStateInterface $form_state) {
    
    /** @var \Drupal\contact\ContactFormInterface $contact_form */
    $contact_form = $form_state->getFormObject()
        ->getEntity();
    $form['send_a_pony'] = [
        '#type' => 'checkbox',
        '#title' => t('Send submitters a voucher for a free pony.'),
        '#description' => t('Enable to send an additional email with a free pony voucher to anyone who submits the form.'),
        '#default_value' => $contact_form->getThirdPartySetting('contact_storage_test', 'send_a_pony', FALSE),
    ];
    $form['#entity_builders'][] = 'contact_storage_test_contact_form_form_builder';
}

/**
 * Entity builder for the contact form edit form with third party options.
 *
 * @see contact_storage_test_form_contact_form_edit_form_alter()
 */
function contact_storage_test_contact_form_form_builder($entity_type, ContactFormInterface $contact_form, &$form, FormStateInterface $form_state) {
    $contact_form->setThirdPartySetting('contact_storage_test', 'send_a_pony', $form_state->getValue('send_a_pony'));
}

Functions

Title Deprecated Summary
contact_storage_test_contact_form_form_builder Entity builder for the contact form edit form with third party options.
contact_storage_test_entity_base_field_info Implements hook_entity_base_field_info().
contact_storage_test_entity_type_alter Implements hook_entity_type_alter().
contact_storage_test_form_contact_form_form_alter Implements hook_form_FORM_ID_alter() for contact_form_form().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.