diff --git a/src/TrashStorageTrait.php b/src/TrashStorageTrait.php index 8028f0c7b423504513e50b7b2d6a397a69f897e2..86962be6ebcca260ef8c5b32f64c9b0c5316c3f3 100644 --- a/src/TrashStorageTrait.php +++ b/src/TrashStorageTrait.php @@ -23,4 +23,19 @@ trait TrashStorageTrait { } } + /** + * {@inheritdoc} + */ + protected function getStorageSchema() { + if (!isset($this->storageSchema)) { + $class = $this->entityType->getHandlerClass('storage_schema') ?: 'Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema'; + + // Ensure that we use our generated storage schema class. + $class = _trash_generate_storage_class($class, 'storage_schema'); + + $this->storageSchema = new $class($this->entityTypeManager, $this->entityType, $this, $this->database, $this->entityFieldManager); + } + return $this->storageSchema; + } + } diff --git a/templates/TrashStorage.php.twig b/templates/TrashStorage.php.twig index 2eca4a93ffa2d7ccf18dc86f7165062f9e8a4378..7b3b3b97a7cff46ab0b4466d25957371421e6573 100644 --- a/templates/TrashStorage.php.twig +++ b/templates/TrashStorage.php.twig @@ -5,7 +5,7 @@ use Drupal\trash\TrashStorageTrait; /** * Provides a custom storage class for trash-enabled entity types. */ -class {{ trash_storage_class }} extends \{{ actual_storage_class }} { +class {{ trash_class }} extends \{{ original_class }} { use TrashStorageTrait; diff --git a/templates/TrashStorageSchema.php.twig b/templates/TrashStorageSchema.php.twig new file mode 100644 index 0000000000000000000000000000000000000000..404d40b36a7cfacb26e86cc1a85c6f33ccdf196a --- /dev/null +++ b/templates/TrashStorageSchema.php.twig @@ -0,0 +1,21 @@ +<?php + +use Drupal\Core\Field\FieldStorageDefinitionInterface; + +/** + * Provides a custom storage schema class for trash-enabled entity types. + */ +class {{ trash_class }} extends \{{ original_class }} { + + /** + * {@inheritdoc} + */ + protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { + $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); + + // @todo Add the 'deleted' field to the required indexes. + + return $schema; + } + +} diff --git a/trash.module b/trash.module index 75df63a514478b665cf66cbb6012b80ce9feba17..d565a5f3713edd01dbd09374dd03802acf82cbff 100644 --- a/trash.module +++ b/trash.module @@ -37,9 +37,7 @@ function trash_entity_base_field_info(EntityTypeInterface $entity_type) { ->setDescription(t('Time when the item got deleted')) ->setInternal(TRUE) ->setTranslatable(FALSE) - ->setRevisionable(TRUE) - ->setInitialValue(0) - ->setDefaultValue(0); + ->setRevisionable(TRUE); return $base_field_definitions; } @@ -76,23 +74,27 @@ function trash_cache_flush() { PhpStorageFactory::get('trash')->deleteAll(); } -function _trash_generate_storage_class($original_storage_class) { +function _trash_generate_storage_class($original_class, $type = 'storage') { + // Assert that only the supported class types are used. + assert($type === 'storage' || $type === 'storage_schema'); + $php_storage = PhpStorageFactory::get('trash'); - $trash_class = str_replace('\\', '__', $original_storage_class) . 'Trash'; + $trash_class = str_replace('\\', '__', $original_class) . 'Trash'; if (!$php_storage->exists($trash_class . '.php')) { - $code = \Drupal::service('twig')->render('@trash/TrashStorage.php.twig', [ - 'trash_storage_class' => $trash_class, - 'actual_storage_class' => $original_storage_class, + $template = ($type === 'storage') ? '@trash/TrashStorage.php.twig' : '@trash/TrashStorageSchema.php.twig'; + $code = \Drupal::service('twig')->render($template, [ + 'trash_class' => $trash_class, + 'original_class' => $original_class, ]); $php_storage->save($trash_class . '.php', $code); } if ($php_storage->load($trash_class . '.php')) { - // @todo Log a critical error when the storage class could not be loaded? + // @todo Log a critical error when the generated class could not be loaded? return $trash_class; } - return $original_storage_class; + return $original_class; }