class FieldStorageCreateCheckSubscriber

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Test/EventSubscriber/FieldStorageCreateCheckSubscriber.php \Drupal\Core\Test\EventSubscriber\FieldStorageCreateCheckSubscriber

Response subscriber to field storage events.

In Kernel test field storages can be created without the entity schema, on which the field storage is based, not being created. For database driver that store the whole entity instance in a single JSON object, like the database driver for MongoDB is doing, the kernel test will fail.

@internal

Hierarchy

Expanded class hierarchy of FieldStorageCreateCheckSubscriber

2 files declare their use of FieldStorageCreateCheckSubscriber
FieldStorageCreateCheckDeprecationTest.php in core/tests/Drupal/KernelTests/Core/Field/FieldStorageCreateCheckDeprecationTest.php
KernelTestBase.php in core/tests/Drupal/KernelTests/KernelTestBase.php

File

core/lib/Drupal/Core/Test/EventSubscriber/FieldStorageCreateCheckSubscriber.php, line 23

Namespace

Drupal\Core\Test\EventSubscriber
View source
final class FieldStorageCreateCheckSubscriber implements EventSubscriberInterface {
  
  /**
   * The schema object for this connection.
   */
  protected Schema $schema;
  public function __construct(protected Connection $connection, protected EntityTypeManagerInterface $entityTypeManager) {
    $this->schema = $this->connection
      ->schema();
  }
  
  /**
   * Gets the subscribed events.
   *
   * @return array
   *   An array of subscribed event names.
   *
   * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface::getSubscribedEvents()
   */
  public static function getSubscribedEvents() : array {
    return [
      FieldStorageDefinitionEvents::CREATE => [
        'onFieldStorageDefinitionCreateEvent',
      ],
    ];
  }
  
  /**
   * Listener method for any field storage definition create event.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionEvent $event
   *   The field storage definition event object.
   * @param string $event_name
   *   The event name.
   */
  public function onFieldStorageDefinitionCreateEvent(FieldStorageDefinitionEvent $event, $event_name) : void {
    $entity_type_id = $event->getFieldStorageDefinition()
      ->getTargetEntityTypeId();
    if ($entity_type_id) {
      $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
      if ($storage instanceof SqlEntityStorageInterface) {
        $base_table = $storage->getTableMapping()
          ->getBaseTable();
        if (!$this->schema
          ->tableExists($base_table)) {
          throw new \LogicException(sprintf('Creating the "%s" field storage definition without the entity schema "%s" being installed is not allowed.', $event->getFieldStorageDefinition()
            ->id(), $entity_type_id));
        }
      }
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary
FieldStorageCreateCheckSubscriber::$schema protected property The schema object for this connection.
FieldStorageCreateCheckSubscriber::getSubscribedEvents public static function Gets the subscribed events.
FieldStorageCreateCheckSubscriber::onFieldStorageDefinitionCreateEvent public function Listener method for any field storage definition create event.
FieldStorageCreateCheckSubscriber::__construct public function

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