Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
RouteEnhancer.php 1.08 KiB
<?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');
  }

}