### Problem/Motivation
Currently, Coffee only displays admin menu items that the current user has permission to access. While this makes sense for a quick navigation tool, it limits discoverability and transparency about available administrative features on the site.
**Use cases where showing all routes would be beneficial:**
1. **Training & Documentation**: Content editors and site builders can see what admin functionality exists even if they don't have access yet, helping them understand the full scope of the site's capabilities
2. **Permission Auditing**: Site administrators can quickly see which routes they cannot access, making it easier to identify permission gaps
3. **Feature Discovery**: Users can discover available admin features and request access if needed
4. **Transparency**: Users can see the URL path and understand where functionality lives, even if they're not authorized to access it
### Proposed Resolution
Add a configuration option (or alter hook support) that allows Coffee to display **all** admin routes regardless of user permissions, with clear visual indicators for routes the user cannot access.
**Key requirements:**
1. Restricted routes should be clearly labeled (e.g., with a suffix like "(You are not authorized to access)")
2. Restricted routes should show the route title and URL path for reference
3. Restricted routes should NOT be clickable (value set to '#' to prevent navigation)
4. This should be configurable, defaulting to current behavior (only show accessible routes)
### Example Implementation
I've successfully implemented this functionality using `hook_coffee_commands()`:
<code><code><?php
```php
/**
* Implements hook_coffee_commands().
*/
function mymodule_coffee_commands() {
$commands = [];
// Load the admin menu tree without access checks.
$menu_tree = \Drupal::menuTree();
$parameters = new MenuTreeParameters();
$tree = $menu_tree->load('admin', $parameters);
// Flatten the menu tree for Coffee.
_mymodule_flatten_menu_for_coffee($tree, $commands);
return $commands;
}
/**
* Helper function to flatten menu tree for Coffee commands.
*/
function _mymodule_flatten_menu_for_coffee($tree, &$commands) {
$current_user = \Drupal::currentUser();
$access_manager = \Drupal::service('access_manager');
foreach ($tree as $item) {
if ($item->link) {
try {
$url_obj = $item->link->getUrlObject();
// Skip external URLs.
if ($url_obj->isExternal()) {
continue;
}
$route_name = $url_obj->getRouteName();
$route_params = $url_obj->getRouteParameters();
$title = $item->link->getTitle();
$url = $url_obj->toString();
// Check if user has access to this route.
$has_access = $access_manager->checkNamedRoute($route_name, $route_params, $current_user);
if ($has_access) {
// User has access - show normally.
$commands[] = [
'value' => $url,
'label' => $title,
'command' => ':admin ' . $title,
];
}
else {
// User doesn't have access - show with restriction message but not clickable.
$commands[] = [
'value' => '#',
'label' => $title . ' - ' . $url . ' (You are not authorized to access)',
'command' => ':admin ' . $title,
];
}
}
catch (\Exception $e) {
// Skip routes that can't be generated.
\Drupal::logger('coffee')->warning('Could not generate URL for route');
}
}
// Recursively process child menu items.
if ($item->subtree) {
_mymodule_flatten_menu_for_coffee($item->subtree, $commands);
}
}
}
```
?>
**Benefits of this approach:**
- Users can search for and discover all admin routes
- Restricted routes are clearly labeled with "(You are not authorized to access)" message
- The URL path is visible in the label for reference
- Clicking a restricted route does nothing (value is '#')
- Works via hook without modifying Coffee core
### Proposed Configuration
Add a settings form for Coffee with an option:
**"Show all admin routes regardless of permissions"** (checkbox, default: unchecked)
When enabled:
- All admin menu items appear in Coffee search results
- Routes the user cannot access show with "(You are not authorized to access)" suffix
- Restricted routes are not clickable
### Alternative Approaches
1. **Configuration option**: Add a checkbox in Coffee settings (preferred approach)
2. **Alter hook**: Provide a `hook_coffee_commands_alter()` for modules to modify commands after processing
3. **Plugin-based**: If Coffee moves to plugin architecture (issue #2396379), add this as a plugin option
### Steps to Reproduce (current behavior)
1. Log in as a user with limited permissions (e.g., content editor)
2. Open Coffee (Alt+D)
3. Type "permissions" or other admin route names
4. Notice that routes you don't have access to don't appear in results
### Expected Behavior (with this feature)
1. Log in as a user with limited permissions
2. Open Coffee (Alt+D)
3. Type "permissions"
4. See result: "Permissions - /admin/people/permissions (You are not authorized to access)"
5. Clicking it does nothing (non-clickable)
### Additional Information
- Tested on Drupal 10.5.6 with Coffee 2.0.1
- This could be particularly useful for multi-site environments or training scenarios
Comments
Comment #2
sandeepraib commentedComment #3
sandeepraib commented