From 284f2b11a48b893cbd13d5429ad68d207488e4e6 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Wed, 13 Oct 2010 13:43:21 +0000 Subject: - Patch #902644 by sun, tobiasb: machine names are too hard to implement. Date types and menu names are not validated. This patch fixes a bug, but is also a last minute clean-up that will help with better distribution support. We discussed this in http://drupal.org/node/933846. --- modules/menu/menu.admin.inc | 114 ++++++++++++++++++-------------------------- modules/menu/menu.test | 12 ++++- 2 files changed, 57 insertions(+), 69 deletions(-) (limited to 'modules/menu') diff --git a/modules/menu/menu.admin.inc b/modules/menu/menu.admin.inc index 622df38ab..e4dc02951 100644 --- a/modules/menu/menu.admin.inc +++ b/modules/menu/menu.admin.inc @@ -419,57 +419,45 @@ function menu_edit_item_submit($form, &$form_state) { */ function menu_edit_menu($form, &$form_state, $type, $menu = array()) { $system_menus = menu_list_system_menus(); - $menu += array('menu_name' => '', 'old_name' => '', 'title' => '', 'description' => ''); - if (!empty($menu['menu_name'])) { - $menu['old_name'] = $menu['menu_name']; - } - $form['old_name'] = array('#type' => 'value', '#value' => $menu['old_name']); + $menu += array( + 'menu_name' => '', + 'old_name' => !empty($menu['menu_name']) ? $menu['menu_name'] : '', + 'title' => '', + 'description' => '', + ); + // Allow menu_edit_menu_submit() and other form submit handlers to determine + // whether the menu already exists. + $form['#insert'] = empty($menu['old_name']); + $form['old_name'] = array( + '#type' => 'value', + '#value' => $menu['old_name'], + ); - // The title of a system menu cannot be altered. - if (isset($system_menus[$menu['menu_name']])) { - $form['title'] = array('#type' => 'value', '#value' => $menu['title']); - } - else { - $form['title'] = array( - '#type' => 'textfield', - '#title' => t('Title'), - '#default_value' => $menu['title'], - '#required' => TRUE, - '#field_suffix' => '  ', - ); - } + $form['title'] = array( + '#type' => 'textfield', + '#title' => t('Title'), + '#default_value' => $menu['title'], + '#required' => TRUE, + // The title of a system menu cannot be altered. + '#access' => !isset($system_menus[$menu['menu_name']]), + ); - // The internal menu name can only be defined during initial menu creation. - if (!empty($menu['old_name'])) { - $form['#insert'] = FALSE; - $form['menu_name'] = array('#type' => 'value', '#value' => $menu['menu_name']); - } - else { - $form['#insert'] = TRUE; - $js_settings = array( - 'type' => 'setting', - 'data' => array( - 'machineReadableValue' => array( - 'title' => array( - 'text' => t('URL path'), - 'target' => 'menu-name', - 'searchPattern' => '[^a-z0-9]+', - 'replaceToken' => '-', - ), - ), - ), - ); - $form['menu_name'] = array( - '#type' => 'textfield', - '#title' => t('Menu name'), - '#maxsize' => MENU_MAX_MENU_NAME_LENGTH_UI, - '#description' => t('This text will be used to construct the URL for the menu. The name must contain only lowercase letters, numbers and hyphens, and must be unique.'), - '#required' => TRUE, - '#attached' => array( - 'js' => array(drupal_get_path('module', 'system') . '/system.js', $js_settings), - ), - ); - } + $form['menu_name'] = array( + '#type' => 'machine_name', + '#title' => t('Menu name'), + '#default_value' => $menu['menu_name'], + '#maxlength' => MENU_MAX_MENU_NAME_LENGTH_UI, + '#description' => t('A unique name to construct the URL for the menu. It must only contain lowercase letters, numbers and hyphens.'), + '#machine_name' => array( + 'exists' => 'menu_edit_menu_name_exists', + 'source' => array('title'), + 'label' => t('URL path'), + 'replace_pattern' => '[^a-z0-9-]+', + 'replace' => '-', + ), + // A menu's machine name cannot be changed. + '#disabled' => !empty($menu['old_name']) || isset($system_menus[$menu['menu_name']]), + ); $form['description'] = array( '#type' => 'textarea', @@ -560,26 +548,18 @@ function menu_delete_menu_confirm_submit($form, &$form_state) { } /** - * Validates the human and machine-readable names when adding or editing a menu. + * Returns whether a menu name already exists. + * + * @see menu_edit_menu() + * @see form_validate_machine_name() */ -function menu_edit_menu_validate($form, &$form_state) { - $item = $form_state['values']; - if (preg_match('/[^a-z0-9-]/', $item['menu_name'])) { - form_set_error('menu_name', t('The menu name may only consist of lowercase letters, numbers, and hyphens.')); - } - if ($form['#insert']) { - if (strlen($item['menu_name']) > MENU_MAX_MENU_NAME_LENGTH_UI) { - form_set_error('menu_name', format_plural(MENU_MAX_MENU_NAME_LENGTH_UI, "The menu name can't be longer than 1 character.", "The menu name can't be longer than @count characters.")); - } +function menu_edit_menu_name_exists($value) { + // 'menu-' is added to the menu name to avoid name-space conflicts. + $value = 'menu-' . $value; + $custom_exists = db_query_range('SELECT 1 FROM {menu_custom} WHERE menu_name = :menu', 0, 1, array(':menu' => $value))->fetchField(); + $link_exists = db_query_range("SELECT 1 FROM {menu_links} WHERE menu_name = :menu", 0, 1, array(':menu' => $value))->fetchField(); - // We will add 'menu-' to the menu name to help avoid name-space conflicts. - $item['menu_name'] = 'menu-' . $item['menu_name']; - $custom_exists = db_query_range('SELECT 1 FROM {menu_custom} WHERE menu_name = :menu', 0, 1, array(':menu' => $item['menu_name']))->fetchField(); - $link_exists = db_query_range("SELECT 1 FROM {menu_links} WHERE menu_name = :menu", 0, 1, array(':menu' => $item['menu_name']))->fetchField(); - if ($custom_exists || $link_exists) { - form_set_error('menu_name', t('The menu already exists.')); - } - } + return $custom_exists || $link_exists; } /** diff --git a/modules/menu/menu.test b/modules/menu/menu.test index 2c5900181..a90fbfb28 100644 --- a/modules/menu/menu.test +++ b/modules/menu/menu.test @@ -140,7 +140,11 @@ class MenuTestCase extends DrupalWebTestCase { $this->drupalPost('admin/structure/menu/add', $edit, t('Save')); // Verify that using a menu_name that is too long results in a validation message. - $this->assertText(format_plural(MENU_MAX_MENU_NAME_LENGTH_UI, "The menu name can't be longer than 1 character.", "The menu name can't be longer than @count characters."), t('Validation failed when menu name is too long.')); + $this->assertRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array( + '!name' => t('Menu name'), + '%max' => MENU_MAX_MENU_NAME_LENGTH_UI, + '%length' => drupal_strlen($menu_name), + ))); // Change the menu_name so it no longer exceeds the maximum length. $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI); @@ -148,7 +152,11 @@ class MenuTestCase extends DrupalWebTestCase { $this->drupalPost('admin/structure/menu/add', $edit, t('Save')); // Verify that no validation error is given for menu_name length. - $this->assertNoText(format_plural(MENU_MAX_MENU_NAME_LENGTH_UI, "The menu name can't be longer than 1 character.", "The menu name can't be longer than @count characters."), t('Validation failed when menu name is too long.')); + $this->assertNoRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array( + '!name' => t('Menu name'), + '%max' => MENU_MAX_MENU_NAME_LENGTH_UI, + '%length' => drupal_strlen($menu_name), + ))); // Unlike most other modules, there is no confirmation message displayed. $this->drupalGet('admin/structure/menu'); -- cgit v1.2.3