Newer
Older
<?php
namespace Drupal\trash;

Andrei Mateescu
committed
/**
* Provides the ability to soft-delete entities at the storage level.
*/
trait TrashStorageTrait {
/**
* {@inheritdoc}
*/
public function delete(array $entities) {
if ($this->getTrashManager()->getTrashContext() !== 'active') {
parent::delete($entities);

Andrei Mateescu
committed
return;
$field_name = 'deleted';
$revisionable = $this->getEntityType()->isRevisionable();
foreach ($entities as $entity) {
$entity->set($field_name, \Drupal::time()->getRequestTime());
// Always create a new revision if the entity type is revisionable.
if ($revisionable) {
/** @var \Drupal\Core\Entity\RevisionableInterface $entity */
$entity->setNewRevision(TRUE);
}
$entity->save();
}
}

Andrei Mateescu
committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* {@inheritdoc}
*/
protected function buildQuery($ids, $revision_ids = FALSE) {
$query = parent::buildQuery($ids, $revision_ids);
if ($this->getTrashManager()->getTrashContext() !== 'active') {
return $query;
}
$table_mapping = $this->getTableMapping();
$deleted_column = $table_mapping->getFieldColumnName($this->fieldStorageDefinitions['deleted'], 'value');
// Ensure that entity_load excludes deleted entities.
if ($data_table = $this->getDataTable()) {
$query->join($data_table, 'data', "[data].[{$this->idKey}] = [base].[{$this->idKey}]");
$query->condition("data.$deleted_column", NULL, 'IS NULL');
}
else {
$query->condition("base.$deleted_column", NULL, 'IS NULL');
}
return $query;
}
/**
* {@inheritdoc}
*/
protected function setPersistentCache($entities) {
if (!$this->entityType->isPersistentlyCacheable()) {
return;
}
// Ensure that deleted entities are never stored in the persistent cache.
foreach ($entities as $id => $entity) {
if (trash_entity_is_deleted($entity)) {
unset($entities[$id]);
}
}
parent::setPersistentCache($entities);
}

Andrei Mateescu
committed
/**
* {@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;
}
/**
* Gets the trash manager service.
*/
private function getTrashManager(): TrashManagerInterface {
return \Drupal::service('trash.manager');
}