Skip to content
Snippets Groups Projects
Commit 2b90522e authored by catch's avatar catch
Browse files

Issue #3441434 by narendraR, Wim Leers: Add validation constraints to core.menu.schema.yml

parent 8e2de62d
No related branches found
No related tags found
No related merge requests found
core.menu.static_menu_link_overrides:
type: config_object
label: 'Static menu link overrides'
constraints:
FullyValidatable: ~
mapping:
definitions:
type: sequence
......@@ -12,9 +14,22 @@ core.menu.static_menu_link_overrides:
menu_name:
type: string
label: 'Menu name'
# This is the id of system.menu.* config.
# @see core/modules/system/config/schema/system.schema.yml
ConfigExists:
prefix: 'system.menu.'
parent:
type: string
label: 'Parent'
# Menu link plugins specify the empty string if there is no parent. But the empty string is not a valid menu
# link plugin ID. In this config representation, "no parent" is therefore represented as `null`, not `''`.
# @see \Drupal\Core\Menu\MenuLinkInterface::getParent()
# @see \Drupal\Core\Menu\StaticMenuLinkOverrides::saveOverride()
nullable: true
constraints:
PluginExists:
manager: plugin.manager.menu.link
interface: 'Drupal\Core\Menu\MenuLinkInterface'
weight:
type: weight
label: 'Weight'
......
......@@ -139,7 +139,10 @@ public function saveOverride($id, array $definition) {
if ($definition) {
// Cast keys to avoid config schema during save.
$definition['menu_name'] = (string) $definition['menu_name'];
$definition['parent'] = (string) $definition['parent'];
// Map `''` to `NULL`. The inverse operation is handled by the menu link manager.
// @see core/config/schema/core.menu.schema.yml
// @see \Drupal\Core\Menu\MenuLinkManager::processDefinition()
$definition['parent'] = empty($definition['parent']) ? NULL : (string) $definition['parent'];
$definition['weight'] = (int) $definition['weight'];
$definition['expanded'] = (bool) $definition['expanded'];
$definition['enabled'] = (bool) $definition['enabled'];
......
<?php
/**
* @file
* Post update functions for config.
*/
/**
* Fix core.menu.static_menu_link_overrides:definitions.*.parent value to null.
*/
function config_post_update_set_menu_parent_value_to_null(): void {
$config = \Drupal::configFactory()->getEditable('core.menu.static_menu_link_overrides');
$all_overrides = $config->get('definitions') ?: [];
foreach ($all_overrides as $definition_key => $definition_value) {
if ($definition_value['parent'] === '') {
$config->set('definitions.' . $definition_key . '.parent', NULL)->save();
}
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\config\Functional\Update;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* Tests update of core.menu.static_menu_link_overrides:definitions.*.parent.
*
* @group config
* @covers \config_post_update_set_menu_parent_value_to_null
*/
class UpdateMenuParentUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-10.3.0.bare.standard.php.gz',
];
}
/**
* Tests update of core.menu.static_menu_link_overrides:definitions.*.parent.
*/
public function testUpdate(): void {
$this->assertNotNull($this->config('core.menu.static_menu_link_overrides')->get('definitions.contact__site_page.parent'));
$this->runUpdates();
$this->assertNull($this->config('core.menu.static_menu_link_overrides')->get('definitions.contact__site_page.parent'));
}
}
definitions:
contact__site_page:
menu_name: footer
parent: ''
parent: null
weight: 0
expanded: false
enabled: true
definitions:
contact__site_page:
menu_name: footer
parent: ''
parent: null
weight: 0
expanded: false
enabled: true
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment