Skip to content
Snippets Groups Projects
Commit 552c484a authored by Adam Bramley's avatar Adam Bramley
Browse files

Strict types, constructor promo, minor tidy ups

parent b5a95e74
No related branches found
No related tags found
No related merge requests found
<?php
declare(strict_types=1);
namespace Drupal\Core\Menu;
/**
......
<?php
declare(strict_types=1);
namespace Drupal\Core\Menu;
use Drupal\Component\EventDispatcher\Event;
......@@ -9,27 +11,6 @@
*/
class MenuLinkTreeManipulatorsAlterEvent extends Event {
/**
* The menu tree to manipulate.
*
* @var \Drupal\Core\Menu\MenuLinkTreeElement[]
*/
protected $tree;
/**
* The menu link tree manipulators.
*
* @var array
*/
protected $manipulators;
/**
* The menu link tree.
*
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
*/
protected $menuLinkTree;
/**
* MenuLinkTreeManipulatorsAlterEvent constructor.
*
......@@ -40,10 +21,11 @@ class MenuLinkTreeManipulatorsAlterEvent extends Event {
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menuLinkTree
* The menu link tree.
*/
public function __construct(array $tree, array &$manipulators, MenuLinkTreeInterface $menuLinkTree) {
$this->tree = $tree;
$this->manipulators = &$manipulators;
$this->menuLinkTree = $menuLinkTree;
public function __construct(
protected array $tree,
protected array $manipulators,
protected MenuLinkTreeInterface $menuLinkTree,
) {
}
/**
......@@ -62,7 +44,7 @@ public function getTree(): array {
* @return array
* The menu tree manipulators.
*/
public function &getManipulators(): array {
public function getManipulators(): array {
return $this->manipulators;
}
......
<?php
declare(strict_types=1);
namespace Drupal\menu_test\EventSubscriber;
use Drupal\Core\Menu\MenuLinkTreeEvents;
......@@ -19,9 +21,10 @@ class MenuLinkTreeEventSubscriber implements EventSubscriberInterface {
* The event.
*/
public static function alterMenuLinkManipulators(MenuLinkTreeManipulatorsAlterEvent $event): void {
$manipulators = &$event->getManipulators();
$manipulators = $event->getManipulators();
// Append the test menu link manipulator.
$manipulators[] = ['callable' => MenuLinkManipulators::getTestManipulator()];
$manipulators[] = ['callable' => MenuLinkManipulators::class . ':testManipulator'];
$event->setManipulators($manipulators);
}
/**
......
<?php
declare(strict_types=1);
namespace Drupal\menu_test;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
......@@ -8,14 +10,7 @@
/**
* A menu manipulator.
*/
class MenuLinkManipulators implements ContainerInjectionInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static();
}
final class MenuLinkManipulators {
/**
* Add the class menu-test-link to certain links.
......@@ -26,7 +21,7 @@ public static function create(ContainerInterface $container) {
* @return array
* The menu manipulators.
*/
public function testManipulator(array $tree) {
public function testManipulator(array $tree): array {
foreach ($tree as $key => $element) {
$link = $tree[$key]->link;
$url = $link->getUrlObject();
......@@ -40,14 +35,4 @@ public function testManipulator(array $tree) {
return $tree;
}
/**
* Gets the name of menu manipulator.
*
* @return string
* The manipulator name.
*/
public static function getTestManipulator() {
return self::class . ":testManipulator";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment