class OverrideMenuLinks
Overrides static menu links defined in code.
This is essentially a thin wrapper around \Drupal\Core\Menu\StaticMenuLinkOverridesInterface. The value passed to the config action should be an associative array whose keys are the plugin IDs of menu links defined in code, and whose values are either:
- An array of properties to override for that menu link
- NULL to delete any existing override for that menu link
Usage example:
core.menu.static_menu_link_overrides:
overrideMenuLinks:
navigation.content.node_type.blog:
weight: -10
user.create:
enabled: false
admin.reports: null
Attributes
#[ConfigAction(id: 'overrideMenuLinks', admin_label: new TranslatableMarkup('Override static menu links'))]
Hierarchy
- class \Drupal\Core\Menu\Plugin\ConfigAction\OverrideMenuLinks implements \Drupal\Core\Config\Action\ConfigActionPluginInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface uses \Drupal\Core\DependencyInjection\AutowiredInstanceTrait
Expanded class hierarchy of OverrideMenuLinks
1 file declares its use of OverrideMenuLinks
- OverrideMenuLinksConfigActionTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Menu/ OverrideMenuLinksConfigActionTest.php
2 string references to 'OverrideMenuLinks'
- OverrideMenuLinksConfigActionTest::testConfigName in core/
tests/ Drupal/ KernelTests/ Core/ Menu/ OverrideMenuLinksConfigActionTest.php - Tests that the action only works on core.menu.static_menu_link_overrides.
- OverrideMenuLinksConfigActionTest::testOverrideLinks in core/
tests/ Drupal/ KernelTests/ Core/ Menu/ OverrideMenuLinksConfigActionTest.php - Tests overriding static menu links.
File
-
core/
lib/ Drupal/ Core/ Menu/ Plugin/ ConfigAction/ OverrideMenuLinks.php, line 43
Namespace
Drupal\Core\Menu\Plugin\ConfigActionView source
final readonly class OverrideMenuLinks implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {
use AutowiredInstanceTrait;
public function __construct(private MenuLinkManagerInterface $menuLinkManager, private StaticMenuLinkOverridesInterface $linkOverrides, #[Autowire(service: 'logger.channel.menu')] private LoggerInterface $logger) {
}
/**
* {@inheritdoc}
*/
public function apply(string $configName, mixed $value) : void {
if ($configName !== 'core.menu.static_menu_link_overrides') {
throw new ConfigActionException('This config action can only be used on the core.menu.static_menu_link_overrides config object.');
}
// We want to be sure we have the latest menu link data.
$this->menuLinkManager
->rebuild();
assert(is_array($value));
foreach ($value as $link_id => $definition) {
if ($definition === NULL) {
$this->linkOverrides
->deleteOverride($link_id);
continue;
}
try {
$this->linkOverrides
->saveOverride($link_id, $definition + $this->menuLinkManager
->getDefinition($link_id));
} catch (PluginNotFoundException) {
$this->logger
->warning('The @link_id menu link was not overridden because it does not exist.', [
'@link_id' => $link_id,
]);
}
}
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
return self::createInstanceAutowired($container);
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|
| AutowiredInstanceTrait::createInstanceAutowired | public static | function | Instantiates a new instance of the implementing class using autowiring. | |
| OverrideMenuLinks::apply | public | function | Applies the config action. | Overrides ConfigActionPluginInterface::apply |
| OverrideMenuLinks::create | public static | function | Creates an instance of the plugin. | Overrides ContainerFactoryPluginInterface::create |
| OverrideMenuLinks::__construct | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.