Skip to content

Commit 6655340

Browse files
authored
Merge pull request #177 from ock-php/use-hooks-for-links-from-routes
Use hooks for links from routes
2 parents 219f59a + 3ae3796 commit 6655340

28 files changed

Lines changed: 366 additions & 415 deletions

.github/workflows/ci-per-module.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- themekit
2121
php: [8.3]
2222
composer-flags: ['', '--prefer-lowest']
23-
drupal-constraint: ['^11.0.5']
23+
drupal-constraint: ['^11.1.8']
2424

2525
defaults:
2626
run:

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
"donquixote/nicetrace": "^0.0.4",
4242
"drupal/admin_toolbar": "^3.4",
4343
"drupal/controller_attributes": "self.version",
44-
"drupal/core-composer-scaffold": "^11.0.5",
45-
"drupal/core-project-message": "^11.0.5",
46-
"drupal/core-recommended": "^11.0.5",
44+
"drupal/core-composer-scaffold": "^11.1.8",
45+
"drupal/core-project-message": "^11.1.8",
46+
"drupal/core-recommended": "^11.1.8",
4747
"drupal/devel": "^5.2",
4848
"drupal/ock": "self.version",
4949
"drupal/renderkit": "self.version",
@@ -57,7 +57,7 @@
5757
"require-dev": {
5858
"ext-dom": "*",
5959
"ext-libxml": "*",
60-
"drupal/core-dev": "^11.0.5",
60+
"drupal/core-dev": "^11.1.8",
6161
"league/container": "^4.2.2",
6262
"ock/drupal-testing": "self.version",
6363
"phpspec/prophecy-phpunit": "^2.2.0",

modules/controller_attributes/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
"license": "GPL-2.0-or-later",
66
"require": {
77
"php": ">=8.3",
8-
"drupal/core": "^11.0.5"
8+
"drupal/core": "^11.1.8"
99
},
1010
"require-dev": {
11-
"ock/drupal-testing": "self.version"
11+
"ock/drupal-testing": "self.version",
12+
"phpunit/phpunit": "^10.5.38"
1213
},
1314
"minimum-stability": "dev",
1415
"prefer-stable": true

modules/controller_attributes/controller_attributes.links.action.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

modules/controller_attributes/controller_attributes.links.menu.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

modules/controller_attributes/controller_attributes.links.task.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

modules/controller_attributes/phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
bootstrap="web/core/tests/bootstrap.php"
66
displayDetailsOnIncompleteTests="true"
77
displayDetailsOnSkippedTests="true"
8+
displayDetailsOnPhpunitDeprecations="true"
89
displayDetailsOnTestsThatTriggerDeprecations="true"
910
displayDetailsOnTestsThatTriggerErrors="true"
1011
displayDetailsOnTestsThatTriggerNotices="true"
@@ -13,9 +14,11 @@
1314
failOnIncomplete="true"
1415
failOnNotice="true"
1516
failOnSkipped="true"
17+
failOnPhpunitDeprecation="true"
1618
failOnDeprecation="true"
1719
failOnRisky="true"
1820
failOnWarning="true"
21+
requireCoverageMetadata="true"
1922
>
2023
<php>
2124
<!-- See https://gist.github.com/davidjguru/2d59eed50818f74710ae3b0f87fb947d -->
@@ -47,9 +50,19 @@
4750
<!-- Example for changing the driver args to webdriver tests MINK_DRIVER_ARGS_WEBDRIVER value: '["chrome", { "chromeOptions": { "w3c": false } }, "http://localhost:4444/wd/hub"]' For using the Firefox browser, replace "chrome" with "firefox" -->
4851
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value=''/>
4952
</php>
53+
<coverage>
54+
<report>
55+
<clover outputFile="build/logs/clover.xml"/>
56+
</report>
57+
</coverage>
5058
<testsuites>
5159
<testsuite name="default">
5260
<directory>./tests/src</directory>
5361
</testsuite>
5462
</testsuites>
63+
<source>
64+
<include>
65+
<directory>./src/</directory>
66+
</include>
67+
</source>
5568
</phpunit>
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Drupal\controller_attributes\Hook;
6+
7+
use Drupal\Core\Hook\Attribute\Hook;
8+
use Drupal\Core\Menu\LocalActionManagerInterface;
9+
use Drupal\Core\Menu\LocalTaskManagerInterface;
10+
use Drupal\Core\Plugin\DefaultPluginManager;
11+
use Drupal\Core\Routing\RouteProviderInterface;
12+
use Symfony\Component\Routing\Route;
13+
14+
class LinksFromRoutes {
15+
16+
public function __construct(
17+
protected RouteProviderInterface $provider,
18+
protected LocalActionManagerInterface $localActionManager,
19+
protected LocalTaskManagerInterface $localTaskManager,
20+
) {}
21+
22+
#[Hook('menu_links_discovered_alter')]
23+
public function menuLinksDiscoveredAlter(&$links): void {
24+
$definitions = [];
25+
foreach ($this->provider->getAllRoutes() as $k => $route) {
26+
if (NULL === $link = $route->getOption('_menu_link')) {
27+
continue;
28+
}
29+
if (!\is_array($link)) {
30+
// @todo Skip invalid values like this.
31+
$link = [];
32+
}
33+
$link += [
34+
'title' => $route->getDefault('_title'),
35+
'route_name' => $k,
36+
];
37+
if (!isset($link['parent'])) {
38+
$link['parent'] = $this->routeGetParentName($route);
39+
}
40+
$definitions[$k] = $link;
41+
}
42+
43+
foreach ($definitions as $k => $link) {
44+
if (isset($definitions[$link['parent']])) {
45+
$link['parent'] = 'routelink:' . $link['parent'];
46+
}
47+
$links['routelink:' . $k] = $link;
48+
}
49+
}
50+
51+
#[Hook('menu_local_actions_alter')]
52+
public function menuLocalActionsAlter(array &$local_actions): void {
53+
foreach ($this->provider->getAllRoutes() as $k => $route) {
54+
if (!is_array($link = $route->getOption('_action_link'))) {
55+
continue;
56+
}
57+
$link += [
58+
'title' => $route->getDefault('_title'),
59+
'route_name' => $k,
60+
];
61+
if (!isset($link['appears_on'])) {
62+
if (NULL === $parentRouteName = $this->routeGetParentName($route)) {
63+
continue;
64+
}
65+
$link['appears_on'] = [$parentRouteName];
66+
}
67+
elseif (!\is_array($link['appears_on'])) {
68+
continue;
69+
}
70+
if ($this->localActionManager instanceof DefaultPluginManager) {
71+
$this->localActionManager->processDefinition($link, 'routelink:' . $k);
72+
}
73+
$local_actions['routelink:' . $k] = $link;
74+
}
75+
}
76+
77+
/**
78+
* Implements hook_local_tasks_alter().
79+
*/
80+
#[Hook('local_tasks_alter')]
81+
public function localTasksAlter(array &$local_tasks): void {
82+
foreach ($this->provider->getAllRoutes() as $k => $route) {
83+
if (is_array($link = $route->getOption('_task_link'))) {
84+
$is_default_task = FALSE;
85+
}
86+
elseif (is_array($link = $route->getOption('_task_link_default'))) {
87+
$is_default_task = TRUE;
88+
}
89+
else {
90+
continue;
91+
}
92+
if (!$is_default_task) {
93+
$is_default_task = !empty($link['is_default_task']);
94+
}
95+
$link += [
96+
'title' => $route->getDefault('_title'),
97+
'route_name' => $k,
98+
];
99+
if (!isset($link['base_route'])) {
100+
if ($is_default_task) {
101+
$link['base_route'] = $k;
102+
}
103+
else {
104+
$link['base_route'] = $this->routeGetParentName($route);
105+
}
106+
}
107+
if ($this->localTaskManager instanceof DefaultPluginManager) {
108+
$this->localTaskManager->processDefinition($link, 'routelink:' . $k);
109+
}
110+
$local_tasks['routelink:' . $k] = $link;
111+
}
112+
}
113+
114+
/**
115+
* Attempts to find a parent route for a given route, based on the path.
116+
*
117+
* @param \Symfony\Component\Routing\Route $route
118+
* The route for which to find a parent.
119+
*
120+
* @return string|null
121+
* A parent route name, or NULL if none found.
122+
*/
123+
protected function routeGetParentName(Route $route): ?string {
124+
125+
$path = $route->getPath();
126+
$parent_path = \dirname($path);
127+
if ($parent_path === '/' || $parent_path === '') {
128+
return NULL;
129+
}
130+
131+
$candidate_routes = $this->provider->getRoutesByPattern($parent_path);
132+
foreach ($candidate_routes as $candidate_name => $candidate_route) {
133+
if ($candidate_route->getPath() === $parent_path) {
134+
return $candidate_name;
135+
}
136+
}
137+
138+
return NULL;
139+
}
140+
141+
}

modules/controller_attributes/src/PluginDeriver/LinkPluginDeriverBase.php

Lines changed: 0 additions & 54 deletions
This file was deleted.

modules/controller_attributes/src/PluginDeriver/PluginDeriverBase.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)