StaticMenuLinkOverrides.php in Drupal 10
File
core/lib/Drupal/Core/Menu/StaticMenuLinkOverrides.php
View source
<?php
namespace Drupal\Core\Menu;
use Drupal\Core\Config\ConfigFactoryInterface;
class StaticMenuLinkOverrides implements StaticMenuLinkOverridesInterface {
protected $configName = 'core.menu.static_menu_link_overrides';
protected $config;
protected $configFactory;
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
protected function getConfig() {
if (empty($this->config)) {
$this->config = $this->configFactory
->getEditable($this->configName);
}
return $this->config;
}
public function reload() {
$this->config = NULL;
$this->configFactory
->reset($this->configName);
}
public function loadOverride($id) {
assert(is_string($id), 'Menu link plugin ID should be a string.');
$all_overrides = $this
->getConfig()
->get('definitions');
$id = static::encodeId($id);
return $all_overrides[$id] ?? [];
}
public function deleteMultipleOverrides(array $ids) {
$all_overrides = $this
->getConfig()
->get('definitions');
$save = FALSE;
foreach ($ids as $id) {
$id = static::encodeId($id);
if (isset($all_overrides[$id])) {
unset($all_overrides[$id]);
$save = TRUE;
}
}
if ($save) {
$this
->getConfig()
->set('definitions', $all_overrides)
->save();
}
return $save;
}
public function deleteOverride($id) {
return $this
->deleteMultipleOverrides([
$id,
]);
}
public function loadMultipleOverrides(array $ids) {
$result = [];
if ($ids) {
$all_overrides = $this
->getConfig()
->get('definitions') ?: [];
foreach ($ids as $id) {
$encoded_id = static::encodeId($id);
if (isset($all_overrides[$encoded_id])) {
$result[$id] = $all_overrides[$encoded_id];
}
}
}
return $result;
}
public function saveOverride($id, array $definition) {
$expected = [
'menu_name' => '',
'parent' => '',
'weight' => 0,
'expanded' => FALSE,
'enabled' => FALSE,
];
$definition = array_intersect_key($definition, $expected);
$definition = $definition + $expected;
if ($definition) {
$definition['menu_name'] = (string) $definition['menu_name'];
$definition['parent'] = (string) $definition['parent'];
$definition['weight'] = (int) $definition['weight'];
$definition['expanded'] = (bool) $definition['expanded'];
$definition['enabled'] = (bool) $definition['enabled'];
$id = static::encodeId($id);
$all_overrides = $this
->getConfig()
->get('definitions');
$all_overrides[$id] = $definition + $this
->loadOverride($id);
$this
->getConfig()
->set('definitions', $all_overrides)
->save(TRUE);
}
return array_keys($definition);
}
public function getCacheTags() {
return $this
->getConfig()
->getCacheTags();
}
protected static function encodeId($id) {
return strtr($id, [
'.' => '__',
'__' => '___',
]);
}
}