class 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;
    
    /**
     * Constructs the FieldStorageCreateCheckSubscriber object.
     *
     * @param \Drupal\Core\Database\Connection $connection
     *   The database connection.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
     *   The entity type manager service.
     * @param bool $throwLogicExceptionOrDeprecationWarning
     *   When the value is TRUE a LogicException will be thrown, when the value is
     *   FALSE a deprecation warning will be given. The value defaults to FALSE.
     */
    public function __construct(Connection $connection, EntityTypeManagerInterface $entityTypeManager, bool $throwLogicExceptionOrDeprecationWarning = FALSE) {
        $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)) {
                    if ($this->throwLogicExceptionOrDeprecationWarning) {
                        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));
                    }
                    else {
                        @trigger_error('Creating the "' . $event->getFieldStorageDefinition()
                            ->id() . '" field storage definition without the entity schema "' . $entity_type_id . '" being installed is deprecated in drupal:11.2.0 and will be replaced by a LogicException in drupal:12.0.0. See https://www.drupal.org/node/3493981', E_USER_DEPRECATED);
                    }
                }
            }
        }
    }

}

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 Constructs the FieldStorageCreateCheckSubscriber object.

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