FieldStorageDefinitionListener.php 6.3 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Field;

5
use Drupal\Core\Database\DatabaseExceptionWrapper;
6 7 8
use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Entity\FieldableEntityStorageInterface;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Reacts to field storage definition CRUD on behalf of the Entity system.
 *
 * @see \Drupal\Core\Field\FieldStorageDefinitionEvents
 */
class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * The entity definition manager.
   *
   * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
   */
  protected $entityLastInstalledSchemaRepository;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

47 48 49 50 51 52 53
  /**
   * The deleted fields repository.
   *
   * @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface
   */
  protected $deletedFieldsRepository;

54 55 56 57 58 59 60 61 62 63 64
  /**
   * Constructs a new FieldStorageDefinitionListener.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository
   *   The entity last installed schema repository.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
65 66
   * @param \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository
   *   The deleted fields repository.
67
   */
68
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, EntityFieldManagerInterface $entity_field_manager, DeletedFieldsRepositoryInterface $deleted_fields_repository) {
69 70 71 72
    $this->entityTypeManager = $entity_type_manager;
    $this->eventDispatcher = $event_dispatcher;
    $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
    $this->entityFieldManager = $entity_field_manager;
73
    $this->deletedFieldsRepository = $deleted_fields_repository;
74 75 76 77 78 79 80 81 82 83
  }

  /**
   * {@inheritdoc}
   */
  public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
    $entity_type_id = $storage_definition->getTargetEntityTypeId();

    // @todo Forward this to all interested handlers, not only storage, once
    //   iterating handlers is possible: https://www.drupal.org/node/2332857.
84
    $storage = $this->entityTypeManager->getStorage($entity_type_id);
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    if ($storage instanceof FieldStorageDefinitionListenerInterface) {
      $storage->onFieldStorageDefinitionCreate($storage_definition);
    }

    $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::CREATE, new FieldStorageDefinitionEvent($storage_definition));

    $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition);
    $this->entityFieldManager->clearCachedFieldDefinitions();
  }

  /**
   * {@inheritdoc}
   */
  public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
    $entity_type_id = $storage_definition->getTargetEntityTypeId();

    // @todo Forward this to all interested handlers, not only storage, once
    //   iterating handlers is possible: https://www.drupal.org/node/2332857.
104
    $storage = $this->entityTypeManager->getStorage($entity_type_id);
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    if ($storage instanceof FieldStorageDefinitionListenerInterface) {
      $storage->onFieldStorageDefinitionUpdate($storage_definition, $original);
    }

    $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::UPDATE, new FieldStorageDefinitionEvent($storage_definition, $original));

    $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition);
    $this->entityFieldManager->clearCachedFieldDefinitions();
  }

  /**
   * {@inheritdoc}
   */
  public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
    $entity_type_id = $storage_definition->getTargetEntityTypeId();

    // @todo Forward this to all interested handlers, not only storage, once
    //   iterating handlers is possible: https://www.drupal.org/node/2332857.
124
    $storage = $this->entityTypeManager->getStorage($entity_type_id);
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    // Keep the field definition in the deleted fields repository so we can use
    // it later during field_purge_batch(), but only if the field has data.
    try {
      if ($storage_definition instanceof BaseFieldDefinition && $storage instanceof FieldableEntityStorageInterface && $storage->countFieldData($storage_definition, TRUE)) {
        $deleted_storage_definition = clone $storage_definition;
        $deleted_storage_definition->setDeleted(TRUE);
        $this->deletedFieldsRepository->addFieldDefinition($deleted_storage_definition);
        $this->deletedFieldsRepository->addFieldStorageDefinition($deleted_storage_definition);
      }
    }
    catch (DatabaseExceptionWrapper $e) {
      // This may happen when changing field storage schema, since we are not
      // able to use a table mapping matching the passed storage definition.
      // @todo Revisit this once we are able to instantiate the table mapping
      //   properly. See https://www.drupal.org/node/2274017.
    }

143 144 145 146 147 148 149 150 151 152 153
    if ($storage instanceof FieldStorageDefinitionListenerInterface) {
      $storage->onFieldStorageDefinitionDelete($storage_definition);
    }

    $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::DELETE, new FieldStorageDefinitionEvent($storage_definition));

    $this->entityLastInstalledSchemaRepository->deleteLastInstalledFieldStorageDefinition($storage_definition);
    $this->entityFieldManager->clearCachedFieldDefinitions();
  }

}