Score:0

How do I grant to a role the permission to flush the cache?

us flag

I removed the Administer site configuration permission from a role, but I need users with that role to flush the cache from /admin/config/development/performance.

I know, there is hook_menu_alter() in Drupal 7, but what about Drupal 8?

leymannx avatar
ne flag
Does this answer your question? [How to give an 'editor' role rebuild/clear cache permission?](https://drupal.stackexchange.com/questions/234270/how-to-give-an-editor-role-rebuild-clear-cache-permission)
Score:1
us flag

You can alter the permission used to access a route, as described in Altering existing routes and adding new routes based on dynamic ones. In the specific case, the code used in alterRoutes(RouteCollection $collection) would be the following one.

Since the administer site performance permission is a custom one, it needs to be added as new permission by the module that implements the route subscriber class, as described in How to create a custom permission on Drupal 8 - 9.

For a module whose machine name is mymodule, the code would be similar to the following one.

mymodule.permissions.yml

administer site performance:
  title: 'Administer site performance'
  description: 'Access the site performance settings page.'
  restrict access: true

mymodule.services.yml

services:
  mymodule.route_subscriber:
    class: Drupal\mymodule\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }

src/Routing/RouteSubscriber.php

namespace Drupal\mymodule\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    if ($route = $collection->get('system.performance_settings')) {
      $route->setRequirement('_permission', 'administer site performance');
    }
  }

}

If the users with that role should not access the settings in that page (Caching and Bandwidth optimization), but only be able to clear the cache, I would implement a custom route that shows a confirmation form and clear the cache (which means calling drupal_flush_all_caches()) when the user confirms the operation.

ANDREW-LVIV avatar
us flag
Thanks! Works like a charm.
leymannx avatar
ne flag
That would be an awesome answer for https://drupal.stackexchange.com/questions/234270/how-to-give-an-editor-role-rebuild-clear-cache-permission
apaderno avatar
us flag
@leymannx I should expand the answer to show more code than it does, including the code to use in the second scenario.
ANDREW-LVIV avatar
us flag
The current suggestion is exactly what I was looking for.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.