StaticMenuLinkOverrides.php in Zircon Profile 8.0
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) {
$all_overrides = $this
->getConfig()
->get('definitions');
$id = static::encodeId($id);
return $id && isset($all_overrides[$id]) ? $all_overrides[$id] : array();
}
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(array(
$id,
));
}
public function loadMultipleOverrides(array $ids) {
$result = array();
if ($ids) {
$all_overrides = $this
->getConfig()
->get('definitions') ?: array();
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 = array(
'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, array(
'.' => '__',
'__' => '___',
));
}
}