Skip to content
Snippets Groups Projects
Commit 33a32160 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

Also generate a storage schema class so we can add the 'deleted' field to the proper indexes.

parent c1fa47ec
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
......@@ -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;
......
<?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;
}
}
......@@ -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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment