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

Issue #3370639 followup: Prevent trashed entities from being returned by entity_load

parent bb2eba95
No related branches found
Tags 3.0.0-beta7
No related merge requests found
......@@ -153,7 +153,7 @@ class TrashController extends ControllerBase implements ContainerInjectionInterf
$row['label']['data'] = [
'#type' => 'link',
'#title' => "{$entity->label()} ({$entity->id()})",
'#url' => $entity->toUrl(),
'#url' => $entity->toUrl('canonical', ['query' => ['in_trash' => TRUE]]),
];
}
else {
......
<?php
namespace Drupal\trash\Routing;
use Drupal\Core\Routing\EnhancerInterface;
use Drupal\Core\Routing\RouteObjectInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
* Sets the trash context for entity routes.
*/
class RouteEnhancer implements EnhancerInterface {
/**
* Constructor.
*/
public function __construct(
protected AccountInterface $currentUser,
) {}
/**
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request): array {
if ($this->applies($defaults[RouteObjectInterface::ROUTE_OBJECT], $request)) {
\Drupal::service('trash.manager')->setTrashContext('inactive');
}
return $defaults;
}
/**
* {@inheritdoc}
*/
protected function applies(Route $route, Request $request): bool {
$is_trash_route = (bool) $route->getOption('_trash_route');
$has_trash_query = $request->query->has('in_trash');
return ($is_trash_route || $has_trash_query) && $this->currentUser->hasPermission('view deleted entities');
}
}
......@@ -69,7 +69,8 @@ class RouteSubscriber extends RouteSubscriberBase {
'_permission' => 'access trash',
])
->setOption('parameters', $parameters)
->setOption('_admin_route', TRUE);
->setOption('_admin_route', TRUE)
->setOption('_trash_route', TRUE);
$collection->add("entity.$entity_type_id.restore", $route);
// Add a route for the purge form.
......@@ -83,7 +84,8 @@ class RouteSubscriber extends RouteSubscriberBase {
'_permission' => 'access trash',
])
->setOption('parameters', $parameters)
->setOption('_admin_route', TRUE);
->setOption('_admin_route', TRUE)
->setOption('_trash_route', TRUE);
$collection->add("entity.$entity_type_id.purge", $route);
}
}
......
......@@ -134,6 +134,16 @@ class TrashManager implements TrashManagerInterface {
return $this->trashContext ?? 'active';
}
/**
* {@inheritdoc}
*/
public function setTrashContext(string $context): static {
assert(in_array($context, ['active', 'inactive', 'ignore'], TRUE));
$this->trashContext = $context;
return $this;
}
/**
* {@inheritdoc}
*/
......
......@@ -62,7 +62,7 @@ interface TrashManagerInterface {
* @return bool
* TRUE whether Trash should alter entity and views queries.
*/
public function shouldAlterQueries() : bool;
public function shouldAlterQueries(): bool;
/**
* Determines the current trash context.
......@@ -70,7 +70,17 @@ interface TrashManagerInterface {
* @return string
* One of 'active', 'inactive' or 'ignore'.
*/
public function getTrashContext() : string;
public function getTrashContext(): string;
/**
* Sets the current trash context.
*
* @param string $context
* One of 'active', 'inactive' or 'ignore'.
*
* @return $this
*/
public function setTrashContext(string $context): static;
/**
* Executes the given callback function in a specific trash context.
......
......@@ -14,3 +14,11 @@ services:
arguments: ['@entity_type.manager', '@trash.manager']
tags:
- { name: event_subscriber }
trash.route_enhancer:
class: Drupal\trash\Routing\RouteEnhancer
arguments: ['@current_user']
tags:
# Use a higher priority than route_enhancer.param_conversion, so trashed
# entities can be loaded by the entity converter.
- { name: route_enhancer, priority: 6000 }
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