Commit a575a5b2 authored by catch's avatar catch

Issue #3015699 by alexpott, longwave, johndevman, Berdir: Properly deprecate...

Issue #3015699 by alexpott, longwave, johndevman, Berdir: Properly deprecate MENU_MAX_MENU_NAME_LENGTH_UI
parent 06d496f7
......@@ -256,8 +256,8 @@ public function save(EntityInterface $entity) {
// @see \Drupal\Core\Config\Entity\ConfigEntityStorage::MAX_ID_LENGTH
// @todo Consider moving this to a protected method on the parent class, and
// abstracting it for all entity types.
if (strlen($entity->get($this->idKey)) > self::MAX_ID_LENGTH) {
throw new ConfigEntityIdLengthException("Configuration entity ID {$entity->get($this->idKey)} exceeds maximum allowed length of " . self::MAX_ID_LENGTH . " characters.");
if (strlen($entity->get($this->idKey)) > static::MAX_ID_LENGTH) {
throw new ConfigEntityIdLengthException("Configuration entity ID {$entity->get($this->idKey)} exceeds maximum allowed length of " . static::MAX_ID_LENGTH . " characters.");
}
return parent::save($entity);
......
......@@ -25,10 +25,10 @@
* Maximum length of menu name as entered by the user.
*
* @deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use
* \Drupal\Core\Config\Entity\ConfigEntityStorage::MAX_ID_LENGTH because the
* menu name is a configuration entity ID.
* \Drupal\system\MenuStorage::MAX_ID_LENGTH instead.
*
* @see https://www.drupal.org/node/2831620
* @see \Drupal\system\MenuStorage::MAX_ID_LENGTH
*/
const MENU_MAX_MENU_NAME_LENGTH_UI = 27;
......
......@@ -17,6 +17,7 @@
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\menu_link_content\MenuLinkContentStorageInterface;
use Drupal\menu_link_content\Plugin\Menu\MenuLinkContent;
use Drupal\system\MenuStorage;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -112,7 +113,7 @@ public function form(array $form, FormStateInterface $form_state) {
'#type' => 'machine_name',
'#title' => $this->t('Menu name'),
'#default_value' => $menu->id(),
'#maxlength' => MENU_MAX_MENU_NAME_LENGTH_UI,
'#maxlength' => MenuStorage::MAX_ID_LENGTH,
'#description' => $this->t('A unique name to construct the URL for the menu. It must only contain lowercase letters, numbers and hyphens.'),
'#machine_name' => [
'exists' => [$this, 'menuNameExists'],
......
......@@ -10,6 +10,7 @@
use Drupal\system\Entity\Menu;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\system\MenuStorage;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\menu_ui\Traits\MenuUiTrait;
......@@ -167,7 +168,7 @@ public function testMenu() {
*/
public function addCustomMenuCRUD() {
// Add a new custom menu.
$menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
$menu_name = strtolower($this->randomMachineName(MenuStorage::MAX_ID_LENGTH));
$label = $this->randomMachineName(16);
$menu = Menu::create([
......@@ -198,7 +199,7 @@ public function addCustomMenuCRUD() {
public function addCustomMenu() {
// Try adding a menu using a menu_name that is too long.
$this->drupalGet('admin/structure/menu/add');
$menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1);
$menu_name = strtolower($this->randomMachineName(MenuStorage::MAX_ID_LENGTH + 1));
$label = $this->randomMachineName(16);
$edit = [
'id' => $menu_name,
......@@ -211,19 +212,19 @@ public function addCustomMenu() {
// message.
$this->assertRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', [
'@name' => t('Menu name'),
'%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
'%max' => MenuStorage::MAX_ID_LENGTH,
'%length' => mb_strlen($menu_name),
]));
// Change the menu_name so it no longer exceeds the maximum length.
$menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
$menu_name = strtolower($this->randomMachineName(MenuStorage::MAX_ID_LENGTH));
$edit['id'] = $menu_name;
$this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
// Verify that no validation error is given for menu_name length.
$this->assertNoRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', [
'@name' => t('Menu name'),
'%max' => MENU_MAX_MENU_NAME_LENGTH_UI,
'%max' => MenuStorage::MAX_ID_LENGTH,
'%length' => mb_strlen($menu_name),
]));
// Verify that the confirmation message is displayed.
......
......@@ -4,6 +4,7 @@
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\system\Entity\Menu;
use Drupal\system\MenuStorage;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
use Drupal\Tests\menu_ui\Traits\MenuUiTrait;
......@@ -74,7 +75,7 @@ public function testBlockContextualLinks() {
protected function addCustomMenu() {
// Try adding a menu using a menu_name that is too long.
$label = $this->randomMachineName(16);
$menu_id = strtolower($this->randomMachineName(MENU_MAX_MENU_NAME_LENGTH_UI + 1));
$menu_id = strtolower($this->randomMachineName(MenuStorage::MAX_ID_LENGTH + 1));
$this->drupalGet('admin/structure/menu/add');
$page = $this->getSession()->getPage();
......@@ -88,7 +89,7 @@ protected function addCustomMenu() {
$page->fillField('Menu name', $menu_id);
$page->pressButton('Save');
// Check that the menu was saved with the ID truncated to the max length.
$menu = Menu::load(substr($menu_id, 0, MENU_MAX_MENU_NAME_LENGTH_UI));
$menu = Menu::load(substr($menu_id, 0, MenuStorage::MAX_ID_LENGTH));
$this->assertEquals($label, $menu->label());
// Check that the menu was added.
......
......@@ -20,7 +20,8 @@
* plural = "@count menus",
* ),
* handlers = {
* "access" = "Drupal\system\MenuAccessControlHandler"
* "access" = "Drupal\system\MenuAccessControlHandler",
* "storage" = "Drupal\system\MenuStorage",
* },
* admin_permission = "administer menu",
* entity_keys = {
......
<?php
namespace Drupal\system;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
/**
* Defines the storage class for menu configuration entities.
*/
class MenuStorage extends ConfigEntityStorage {
/**
* Menu names have a maximum length of 32.
*
* This is based on:
* - menu_tree table schema definition,
* - \Drupal\Core\Config\Entity\ConfigEntityStorage::MAX_ID_LENGTH
* - menu_name base field on the Menu Link content entity.
*
* @see \Drupal\Core\Menu\MenuTreeStorage::schemaDefinition()
* @see \Drupal\menu_link_content\Entity\MenuLinkContent::baseFieldDefinitions()
*/
const MAX_ID_LENGTH = 32;
}
<?php
namespace Drupal\Tests\system\Kernel\Menu;
use Drupal\Core\Config\Entity\Exception\ConfigEntityIdLengthException;
use Drupal\KernelTests\KernelTestBase;
use Drupal\system\Entity\Menu;
use Drupal\system\MenuStorage;
/**
* Tests MenuStorage.
*
* @group Menu
*
* @see \Drupal\system\MenuStorage
*/
class MenuStorageTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['system'];
/**
* Tests MenuStorage::MAX_ID_LENGTH is enforced.
*/
public function testMaxIdLengthException() {
$id = $this->randomMachineName(MenuStorage::MAX_ID_LENGTH + 1);
$this->expectException(ConfigEntityIdLengthException::class);
$this->expectExceptionMessage(
sprintf('Configuration entity ID %s exceeds maximum allowed length of %s characters.', $id, MenuStorage::MAX_ID_LENGTH)
);
Menu::create(['id' => $id])->save();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment