class ContactStorageTestHooks

Hook implementations for contact_storage_test.

Hierarchy

Expanded class hierarchy of ContactStorageTestHooks

File

core/modules/contact/tests/modules/contact_storage_test/src/Hook/ContactStorageTestHooks.php, line 16

Namespace

Drupal\contact_storage_test\Hook
View source
class ContactStorageTestHooks {
  
  /**
   * Implements hook_entity_base_field_info().
   */
  public function entityBaseFieldInfo(EntityTypeInterface $entity_type) : array {
    $fields = [];
    if ($entity_type->id() == 'contact_message') {
      $fields['id'] = BaseFieldDefinition::create('integer')->setLabel('Message ID')
        ->setDescription('The message ID.')
        ->setReadOnly(TRUE)
        ->setSetting('unsigned', TRUE);
    }
    return $fields;
  }
  
  /**
   * Implements hook_entity_type_alter().
   */
  public function entityTypeAlter(array &$entity_types) : void {
    /** @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().
   */
  public function formContactFormFormAlter(&$form, FormStateInterface $form_state) : void {
    /** @var \Drupal\contact\ContactFormInterface $contact_form */
    $contact_form = $form_state->getFormObject()
      ->getEntity();
    $form['send_a_pony'] = [
      '#type' => 'checkbox',
      '#title' => 'Send submitters a voucher for a free pony.',
      '#description' => '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'][] = [
      $this,
      'contactFormBuilder',
    ];
  }
  
  /**
   * Entity builder for the contact form edit form with third party options.
   *
   * @see contact_storage_test_form_contact_form_edit_form_alter()
   */
  public function contactFormBuilder($entity_type, ContactFormInterface $contact_form, &$form, FormStateInterface $form_state) : void {
    $contact_form->setThirdPartySetting('contact_storage_test', 'send_a_pony', $form_state->getValue('send_a_pony'));
  }

}

Members

Title Sort descending Modifiers Object type Summary
ContactStorageTestHooks::contactFormBuilder public function Entity builder for the contact form edit form with third party options.
ContactStorageTestHooks::entityBaseFieldInfo public function Implements hook_entity_base_field_info().
ContactStorageTestHooks::entityTypeAlter public function Implements hook_entity_type_alter().
ContactStorageTestHooks::formContactFormFormAlter public function 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.