Update core 8.3.0

This commit is contained in:
Rob Davies 2017-04-13 15:53:35 +01:00
parent da7a7918f8
commit cd7a898e66
6144 changed files with 132297 additions and 87747 deletions

View file

@ -22,8 +22,11 @@ use Drupal\system\Entity\Menu;
use Drupal\node\NodeInterface;
/**
* Maximum length of menu name as entered by the user. Database length is 32
* and we add a menu- prefix.
* Maximum length of menu name as entered by the user.
*
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. Use
* \Drupal\Core\Config\Entity\ConfigEntityStorage::MAX_ID_LENGTH because the
* menu name is a configuration entity ID.
*/
const MENU_MAX_MENU_NAME_LENGTH_UI = 27;
@ -35,21 +38,21 @@ function menu_ui_help($route_name, RouteMatchInterface $route_match) {
case 'help.page.menu_ui':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Menu UI module provides an interface for managing menus. A menu is a hierarchical collection of links, which can be within or external to the site, generally used for navigation. For more information, see the <a href=":menu">online documentation for the Menu UI module</a>.', array(':menu' => 'https://www.drupal.org/documentation/modules/menu/')) . '</p>';
$output .= '<p>' . t('The Menu UI module provides an interface for managing menus. A menu is a hierarchical collection of links, which can be within or external to the site, generally used for navigation. For more information, see the <a href=":menu">online documentation for the Menu UI module</a>.', [':menu' => 'https://www.drupal.org/documentation/modules/menu/']) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Managing menus') . '</dt>';
$output .= '<dd>' . t('Users with the <em>Administer menus and menu items</em> permission can add, edit, and delete custom menus on the <a href=":menu">Menus page</a>. Custom menus can be special site menus, menus of external links, or any combination of internal and external links. You may create an unlimited number of additional menus, each of which will automatically have an associated block (if you have the <a href=":block_help">Block module</a> installed). By selecting <em>Edit menu</em>, you can add, edit, or delete links for a given menu. The links listing page provides a drag-and-drop interface for controlling the order of links, and creating a hierarchy within the menu.', array(':block_help' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('help.page', array('name' => 'block')) : '#', ':menu' => \Drupal::url('entity.menu.collection'))) . '</dd>';
$output .= '<dd>' . t('Users with the <em>Administer menus and menu items</em> permission can add, edit, and delete custom menus on the <a href=":menu">Menus page</a>. Custom menus can be special site menus, menus of external links, or any combination of internal and external links. You may create an unlimited number of additional menus, each of which will automatically have an associated block (if you have the <a href=":block_help">Block module</a> installed). By selecting <em>Edit menu</em>, you can add, edit, or delete links for a given menu. The links listing page provides a drag-and-drop interface for controlling the order of links, and creating a hierarchy within the menu.', [':block_help' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('help.page', ['name' => 'block']) : '#', ':menu' => \Drupal::url('entity.menu.collection')]) . '</dd>';
$output .= '<dt>' . t('Displaying menus') . '</dt>';
$output .= '<dd>' . t('If you have the Block module enabled, then each menu that you create is rendered in a block that you enable and position on the <a href=":blocks">Block layout page</a>. In some <a href=":themes">themes</a>, the main menu and possibly the secondary menu will be output automatically; you may be able to disable this behavior on the <a href=":themes">theme\'s settings page</a>.', array(':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#', ':themes' => \Drupal::url('system.themes_page'), ':theme_settings' => \Drupal::url('system.theme_settings'))) . '</dd>';
$output .= '<dd>' . t('If you have the Block module enabled, then each menu that you create is rendered in a block that you enable and position on the <a href=":blocks">Block layout page</a>. In some <a href=":themes">themes</a>, the main menu and possibly the secondary menu will be output automatically; you may be able to disable this behavior on the <a href=":themes">theme\'s settings page</a>.', [':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#', ':themes' => \Drupal::url('system.themes_page'), ':theme_settings' => \Drupal::url('system.theme_settings')]) . '</dd>';
$output .= '</dl>';
return $output;
}
if ($route_name == 'entity.menu.add_form' && \Drupal::moduleHandler()->moduleExists('block') && \Drupal::currentUser()->hasPermission('administer blocks')) {
return '<p>' . t('You can enable the newly-created block for this menu on the <a href=":blocks">Block layout page</a>.', array(':blocks' => \Drupal::url('block.admin_display'))) . '</p>';
return '<p>' . t('You can enable the newly-created block for this menu on the <a href=":blocks">Block layout page</a>.', [':blocks' => \Drupal::url('block.admin_display')]) . '</p>';
}
elseif ($route_name == 'entity.menu.collection' && \Drupal::moduleHandler()->moduleExists('block') && \Drupal::currentUser()->hasPermission('administer blocks')) {
return '<p>' . t('Each menu has a corresponding block that is managed on the <a href=":blocks">Block layout page</a>.', array(':blocks' => \Drupal::url('block.admin_display'))) . '</p>';
return '<p>' . t('Each menu has a corresponding block that is managed on the <a href=":blocks">Block layout page</a>.', [':blocks' => \Drupal::url('block.admin_display')]) . '</p>';
}
}
@ -120,9 +123,9 @@ function menu_ui_menu_delete(Menu $menu) {
function menu_ui_block_view_system_menu_block_alter(array &$build, BlockPluginInterface $block) {
if ($block->getBaseId() == 'system_menu_block') {
$menu_name = $block->getDerivativeId();
$build['#contextual_links']['menu'] = array(
'route_parameters' => array('menu' => $menu_name),
);
$build['#contextual_links']['menu'] = [
'route_parameters' => ['menu' => $menu_name],
];
}
}
@ -149,10 +152,10 @@ function _menu_ui_node_save(NodeInterface $node, array $values) {
}
else {
// Create a new menu_link_content entity.
$entity = MenuLinkContent::create(array(
$entity = MenuLinkContent::create([
'link' => ['uri' => 'entity:node/' . $node->id()],
'langcode' => $node->language()->getId(),
));
]);
$entity->enabled->value = 1;
}
$entity->title->value = trim($values['title']);
@ -170,7 +173,7 @@ function menu_ui_node_predelete(EntityInterface $node) {
// Delete all MenuLinkContent links that point to this node.
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$result = $menu_link_manager->loadLinksByRoute('entity.node.canonical', array('node' => $node->id()));
$result = $menu_link_manager->loadLinksByRoute('entity.node.canonical', ['node' => $node->id()]);
if (!empty($result)) {
foreach ($result as $id => $instance) {
@ -199,7 +202,7 @@ function menu_ui_get_menu_link_defaults(NodeInterface $node) {
if ($node->id()) {
$id = FALSE;
// Give priority to the default menu
$type_menus = $node_type->getThirdPartySetting('menu_ui', 'available_menus', array('main'));
$type_menus = $node_type->getThirdPartySetting('menu_ui', 'available_menus', ['main']);
if (in_array($menu_name, $type_menus)) {
$query = \Drupal::entityQuery('menu_link_content')
->condition('link.uri', 'node/' . $node->id())
@ -224,7 +227,7 @@ function menu_ui_get_menu_link_defaults(NodeInterface $node) {
if ($id) {
$menu_link = MenuLinkContent::load($id);
$menu_link = \Drupal::service('entity.repository')->getTranslationFromContext($menu_link);
$defaults = array(
$defaults = [
'entity_id' => $menu_link->id(),
'id' => $menu_link->getPluginId(),
'title' => $menu_link->getTitle(),
@ -233,7 +236,7 @@ function menu_ui_get_menu_link_defaults(NodeInterface $node) {
'menu_name' => $menu_link->getMenuName(),
'parent' => $menu_link->getParentId(),
'weight' => $menu_link->getWeight(),
);
];
}
}
@ -242,7 +245,7 @@ function menu_ui_get_menu_link_defaults(NodeInterface $node) {
// definition.
$field_definitions = \Drupal::entityManager()->getBaseFieldDefinitions('menu_link_content');
$max_length = $field_definitions['title']->getSetting('max_length');
$defaults = array(
$defaults = [
'entity_id' => 0,
'id' => '',
'title' => '',
@ -251,7 +254,7 @@ function menu_ui_get_menu_link_defaults(NodeInterface $node) {
'menu_name' => $menu_name,
'parent' => '',
'weight' => 0,
);
];
}
return $defaults;
}
@ -273,8 +276,8 @@ function menu_ui_form_node_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */
$menu_parent_selector = \Drupal::service('menu.parent_form_selector');
$menu_names = menu_ui_get_menus();
$type_menus = $node_type->getThirdPartySetting('menu_ui', 'available_menus', array('main'));
$available_menus = array();
$type_menus = $node_type->getThirdPartySetting('menu_ui', 'available_menus', ['main']);
$available_menus = [];
foreach ($type_menus as $menu) {
$available_menus[$menu] = $menu_names[$menu];
}
@ -290,64 +293,64 @@ function menu_ui_form_node_form_alter(&$form, FormStateInterface $form_state) {
return;
}
$form['menu'] = array(
$form['menu'] = [
'#type' => 'details',
'#title' => t('Menu settings'),
'#access' => \Drupal::currentUser()->hasPermission('administer menu'),
'#open' => (bool) $defaults['id'],
'#group' => 'advanced',
'#attached' => array(
'library' => array('menu_ui/drupal.menu_ui'),
),
'#attached' => [
'library' => ['menu_ui/drupal.menu_ui'],
],
'#tree' => TRUE,
'#weight' => -2,
'#attributes' => array('class' => array('menu-link-form')),
);
$form['menu']['enabled'] = array(
'#attributes' => ['class' => ['menu-link-form']],
];
$form['menu']['enabled'] = [
'#type' => 'checkbox',
'#title' => t('Provide a menu link'),
'#default_value' => (int) (bool) $defaults['id'],
);
$form['menu']['link'] = array(
];
$form['menu']['link'] = [
'#type' => 'container',
'#parents' => array('menu'),
'#states' => array(
'invisible' => array(
'input[name="menu[enabled]"]' => array('checked' => FALSE),
),
),
);
'#parents' => ['menu'],
'#states' => [
'invisible' => [
'input[name="menu[enabled]"]' => ['checked' => FALSE],
],
],
];
// Populate the element with the link data.
foreach (array('id', 'entity_id') as $key) {
$form['menu']['link'][$key] = array('#type' => 'value', '#value' => $defaults[$key]);
foreach (['id', 'entity_id'] as $key) {
$form['menu']['link'][$key] = ['#type' => 'value', '#value' => $defaults[$key]];
}
$form['menu']['link']['title'] = array(
$form['menu']['link']['title'] = [
'#type' => 'textfield',
'#title' => t('Menu link title'),
'#default_value' => $defaults['title'],
'#maxlength' => $defaults['title_max_length'],
);
];
$form['menu']['link']['description'] = array(
$form['menu']['link']['description'] = [
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $defaults['description'],
'#rows' => 1,
'#description' => t('Shown when hovering over the menu link.'),
);
];
$form['menu']['link']['menu_parent'] = $parent_element;
$form['menu']['link']['menu_parent']['#title'] = t('Parent item');
$form['menu']['link']['menu_parent']['#attributes']['class'][] = 'menu-parent-select';
$form['menu']['link']['weight'] = array(
$form['menu']['link']['weight'] = [
'#type' => 'number',
'#title' => t('Weight'),
'#default_value' => $defaults['weight'],
'#description' => t('Menu links with lower weights are displayed before links with higher weights.'),
);
];
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
@ -398,21 +401,21 @@ function menu_ui_form_node_type_form_alter(&$form, FormStateInterface $form_stat
$menu_options = menu_ui_get_menus();
/** @var \Drupal\node\NodeTypeInterface $type */
$type = $form_state->getFormObject()->getEntity();
$form['menu'] = array(
$form['menu'] = [
'#type' => 'details',
'#title' => t('Menu settings'),
'#attached' => array(
'library' => array('menu_ui/drupal.menu_ui.admin'),
),
'#attached' => [
'library' => ['menu_ui/drupal.menu_ui.admin'],
],
'#group' => 'additional_settings',
);
$form['menu']['menu_options'] = array(
];
$form['menu']['menu_options'] = [
'#type' => 'checkboxes',
'#title' => t('Available menus'),
'#default_value' => $type->getThirdPartySetting('menu_ui', 'available_menus', array('main')),
'#default_value' => $type->getThirdPartySetting('menu_ui', 'available_menus', ['main']),
'#options' => $menu_options,
'#description' => t('The menus available to place links in for this content type.'),
);
];
// @todo See if we can avoid pre-loading all options by changing the form or
// using a #process callback. https://www.drupal.org/node/2310319
// To avoid an 'illegal option' error after saving the form we have to load
@ -420,14 +423,14 @@ function menu_ui_form_node_type_form_alter(&$form, FormStateInterface $form_stat
// add options to the list using ajax.
$options_cacheability = new CacheableMetadata();
$options = $menu_parent_selector->getParentSelectOptions('', NULL, $options_cacheability);
$form['menu']['menu_parent'] = array(
$form['menu']['menu_parent'] = [
'#type' => 'select',
'#title' => t('Default parent item'),
'#default_value' => $type->getThirdPartySetting('menu_ui', 'parent', 'main:'),
'#options' => $options,
'#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'),
'#attributes' => array('class' => array('menu-title-select')),
);
'#attributes' => ['class' => ['menu-title-select']],
];
$options_cacheability->applyTo($form['menu']['menu_parent']);
$form['#validate'][] = 'menu_ui_form_node_type_form_validate';

View file

@ -49,7 +49,7 @@ class MenuController extends ControllerBase {
* The available menu and menu items.
*/
public function getParentOptions(Request $request) {
$available_menus = array();
$available_menus = [];
if ($menus = $request->request->get('menus')) {
foreach ($menus as $menu) {
$available_menus[$menu] = $menu;

View file

@ -57,7 +57,7 @@ class MenuDeleteForm extends EntityDeleteForm {
$caption = '';
$num_links = $this->menuLinkManager->countMenuLinks($this->entity->id());
if ($num_links) {
$caption .= '<p>' . $this->formatPlural($num_links, '<strong>Warning:</strong> There is currently 1 menu link in %title. It will be deleted (system-defined items will be reset).', '<strong>Warning:</strong> There are currently @count menu links in %title. They will be deleted (system-defined links will be reset).', array('%title' => $this->entity->label())) . '</p>';
$caption .= '<p>' . $this->formatPlural($num_links, '<strong>Warning:</strong> There is currently 1 menu link in %title. It will be deleted (system-defined items will be reset).', '<strong>Warning:</strong> There are currently @count menu links in %title. They will be deleted (system-defined links will be reset).', ['%title' => $this->entity->label()]) . '</p>';
}
$caption .= '<p>' . t('This action cannot be undone.') . '</p>';
return $caption;
@ -67,7 +67,7 @@ class MenuDeleteForm extends EntityDeleteForm {
* {@inheritdoc}
*/
protected function logDeletionMessage() {
$this->logger('menu')->notice('Deleted custom menu %title and all its menu links.', array('%title' => $this->entity->label()));
$this->logger('menu')->notice('Deleted custom menu %title and all its menu links.', ['%title' => $this->entity->label()]);
}
/**
@ -85,7 +85,7 @@ class MenuDeleteForm extends EntityDeleteForm {
// parameter that is being removed. Also, consider moving this to
// menu_ui.module as part of a generic response to entity deletion.
// https://www.drupal.org/node/2310329
$menu_links = $this->menuLinkManager->loadLinksByRoute('entity.menu.edit_form', array('menu' => $this->entity->id()), TRUE);
$menu_links = $this->menuLinkManager->loadLinksByRoute('entity.menu.edit_form', ['menu' => $this->entity->id()], TRUE);
foreach ($menu_links as $id => $link) {
$this->menuLinkManager->removeDefinition($id);
}

View file

@ -57,22 +57,22 @@ class MenuLinkEditForm extends FormBase {
* The plugin instance to use for this form.
*/
public function buildForm(array $form, FormStateInterface $form_state, MenuLinkInterface $menu_link_plugin = NULL) {
$form['menu_link_id'] = array(
$form['menu_link_id'] = [
'#type' => 'value',
'#value' => $menu_link_plugin->getPluginId(),
);
];
$class_name = $menu_link_plugin->getFormClass();
$form['#plugin_form'] = $this->classResolver->getInstanceFromDefinition($class_name);
$form['#plugin_form']->setMenuLinkInstance($menu_link_plugin);
$form += $form['#plugin_form']->buildConfigurationForm($form, $form_state);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#button_type' => 'primary',
);
];
return $form;
}
@ -92,7 +92,7 @@ class MenuLinkEditForm extends FormBase {
drupal_set_message($this->t('The menu link has been saved.'));
$form_state->setRedirect(
'entity.menu.edit_form',
array('menu' => $link->getMenuName())
['menu' => $link->getMenuName()]
);
}

View file

@ -59,16 +59,16 @@ class MenuLinkResetForm extends ConfirmFormBase {
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to reset the link %item to its default values?', array('%item' => $this->link->getTitle()));
return $this->t('Are you sure you want to reset the link %item to its default values?', ['%item' => $this->link->getTitle()]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.menu.edit_form', array(
return new Url('entity.menu.edit_form', [
'menu' => $this->link->getMenuName(),
));
]);
}
/**

View file

@ -5,7 +5,6 @@ namespace Drupal\menu_ui;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Link;
@ -23,13 +22,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class MenuForm extends EntityForm {
/**
* The factory for entity queries.
*
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $entityQueryFactory;
/**
* The menu link manager.
*
@ -56,13 +48,11 @@ class MenuForm extends EntityForm {
*
* @var array
*/
protected $overviewTreeForm = array('#tree' => TRUE);
protected $overviewTreeForm = ['#tree' => TRUE];
/**
* Constructs a MenuForm object.
*
* @param \Drupal\Core\Entity\Query\QueryFactory $entity_query_factory
* The factory for entity queries.
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
* The menu link manager.
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
@ -70,8 +60,7 @@ class MenuForm extends EntityForm {
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
* The link generator.
*/
public function __construct(QueryFactory $entity_query_factory, MenuLinkManagerInterface $menu_link_manager, MenuLinkTreeInterface $menu_tree, LinkGeneratorInterface $link_generator) {
$this->entityQueryFactory = $entity_query_factory;
public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuLinkTreeInterface $menu_tree, LinkGeneratorInterface $link_generator) {
$this->menuLinkManager = $menu_link_manager;
$this->menuTree = $menu_tree;
$this->linkGenerator = $link_generator;
@ -82,7 +71,6 @@ class MenuForm extends EntityForm {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.query'),
$container->get('plugin.manager.menu.link'),
$container->get('menu.link_tree'),
$container->get('link_generator')
@ -96,43 +84,43 @@ class MenuForm extends EntityForm {
$menu = $this->entity;
if ($this->operation == 'edit') {
$form['#title'] = $this->t('Edit menu %label', array('%label' => $menu->label()));
$form['#title'] = $this->t('Edit menu %label', ['%label' => $menu->label()]);
}
$form['label'] = array(
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#default_value' => $menu->label(),
'#required' => TRUE,
);
$form['id'] = array(
];
$form['id'] = [
'#type' => 'machine_name',
'#title' => $this->t('Menu name'),
'#default_value' => $menu->id(),
'#maxlength' => MENU_MAX_MENU_NAME_LENGTH_UI,
'#description' => $this->t('A unique name to construct the URL for the menu. It must only contain lowercase letters, numbers and hyphens.'),
'#machine_name' => array(
'exists' => array($this, 'menuNameExists'),
'source' => array('label'),
'#machine_name' => [
'exists' => [$this, 'menuNameExists'],
'source' => ['label'],
'replace_pattern' => '[^a-z0-9-]+',
'replace' => '-',
),
],
// A menu's machine name cannot be changed.
'#disabled' => !$menu->isNew() || $menu->isLocked(),
);
$form['description'] = array(
];
$form['description'] = [
'#type' => 'textfield',
'#title' => t('Administrative summary'),
'#maxlength' => 512,
'#default_value' => $menu->getDescription(),
);
];
$form['langcode'] = array(
$form['langcode'] = [
'#type' => 'language_select',
'#title' => t('Menu language'),
'#languages' => LanguageInterface::STATE_ALL,
'#default_value' => $menu->language()->getId(),
);
];
// Add menu links administration form for existing menus.
if (!$menu->isNew() || $menu->isLocked()) {
@ -142,7 +130,7 @@ class MenuForm extends EntityForm {
// the parents of the form section.
// @see self::submitOverviewForm()
$form_state->set('menu_overview_form_parents', ['links']);
$form['links'] = array();
$form['links'] = [];
$form['links'] = $this->buildOverviewForm($form['links'], $form_state);
}
@ -160,7 +148,7 @@ class MenuForm extends EntityForm {
*/
public function menuNameExists($value) {
// Check first to see if a menu with this ID exists.
if ($this->entityQueryFactory->get('menu')->condition('id', $value)->range(0, 1)->count()->execute()) {
if ($this->entityTypeManager->getStorage('menu')->getQuery()->condition('id', $value)->range(0, 1)->count()->execute()) {
return TRUE;
}
@ -176,12 +164,12 @@ class MenuForm extends EntityForm {
$status = $menu->save();
$edit_link = $this->entity->link($this->t('Edit'));
if ($status == SAVED_UPDATED) {
drupal_set_message($this->t('Menu %label has been updated.', array('%label' => $menu->label())));
$this->logger('menu')->notice('Menu %label has been updated.', array('%label' => $menu->label(), 'link' => $edit_link));
drupal_set_message($this->t('Menu %label has been updated.', ['%label' => $menu->label()]));
$this->logger('menu')->notice('Menu %label has been updated.', ['%label' => $menu->label(), 'link' => $edit_link]);
}
else {
drupal_set_message($this->t('Menu %label has been added.', array('%label' => $menu->label())));
$this->logger('menu')->notice('Menu %label has been added.', array('%label' => $menu->label(), 'link' => $edit_link));
drupal_set_message($this->t('Menu %label has been added.', ['%label' => $menu->label()]));
$this->logger('menu')->notice('Menu %label has been added.', ['%label' => $menu->label(), 'link' => $edit_link]);
}
$form_state->setRedirectUrl($this->entity->urlInfo('edit-form'));
@ -225,10 +213,10 @@ class MenuForm extends EntityForm {
// We indicate that a menu administrator is running the menu access check.
$this->getRequest()->attributes->set('_menu_admin', TRUE);
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
$tree = $this->menuTree->transform($tree, $manipulators);
$this->getRequest()->attributes->set('_menu_admin', FALSE);
@ -241,26 +229,26 @@ class MenuForm extends EntityForm {
};
$delta = max($count($tree), 50);
$form['links'] = array(
$form['links'] = [
'#type' => 'table',
'#theme' => 'table__menu_overview',
'#header' => array(
'#header' => [
$this->t('Menu link'),
array(
[
'data' => $this->t('Enabled'),
'class' => array('checkbox'),
),
'class' => ['checkbox'],
],
$this->t('Weight'),
array(
[
'data' => $this->t('Operations'),
'colspan' => 3,
),
),
'#attributes' => array(
],
],
'#attributes' => [
'id' => 'menu-overview',
),
'#tabledrag' => array(
array(
],
'#tabledrag' => [
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'menu-parent',
@ -268,14 +256,14 @@ class MenuForm extends EntityForm {
'source' => 'menu-id',
'hidden' => TRUE,
'limit' => \Drupal::menuTree()->maxDepth() - 1,
),
array(
],
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'menu-weight',
),
),
);
],
],
];
$form['links']['#empty'] = $this->t('There are no menu links yet. <a href=":url">Add link</a>.', [
':url' => $this->url('entity.menu.add_link_form', ['menu' => $this->entity->id()], [
@ -297,19 +285,19 @@ class MenuForm extends EntityForm {
$form['links'][$id]['#weight'] = $element['#item']->link->getWeight();
// Add special classes to be used for tabledrag.js.
$element['parent']['#attributes']['class'] = array('menu-parent');
$element['weight']['#attributes']['class'] = array('menu-weight');
$element['id']['#attributes']['class'] = array('menu-id');
$element['parent']['#attributes']['class'] = ['menu-parent'];
$element['weight']['#attributes']['class'] = ['menu-weight'];
$element['id']['#attributes']['class'] = ['menu-id'];
$form['links'][$id]['title'] = array(
array(
$form['links'][$id]['title'] = [
[
'#theme' => 'indentation',
'#size' => $element['#item']->depth - 1,
),
],
$element['title'],
);
];
$form['links'][$id]['enabled'] = $element['enabled'];
$form['links'][$id]['enabled']['#wrapper_attributes']['class'] = array('checkbox', 'menu-enabled');
$form['links'][$id]['enabled']['#wrapper_attributes']['class'] = ['checkbox', 'menu-enabled'];
$form['links'][$id]['weight'] = $element['weight'];
@ -351,7 +339,7 @@ class MenuForm extends EntityForm {
if ($link) {
$id = 'menu_plugin_id:' . $link->getPluginId();
$form[$id]['#item'] = $element;
$form[$id]['#attributes'] = $link->isEnabled() ? array('class' => array('menu-enabled')) : array('class' => array('menu-disabled'));
$form[$id]['#attributes'] = $link->isEnabled() ? ['class' => ['menu-enabled']] : ['class' => ['menu-disabled']];
$form[$id]['title'] = Link::fromTextAndUrl($link->getTitle(), $link->getUrlObject())->toRenderable();
if (!$link->isEnabled()) {
$form[$id]['title']['#suffix'] = ' (' . $this->t('disabled') . ')';
@ -365,32 +353,32 @@ class MenuForm extends EntityForm {
$form[$id]['title']['#suffix'] = ' (' . $this->t('logged in users only') . ')';
}
$form[$id]['enabled'] = array(
$form[$id]['enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable @title menu link', array('@title' => $link->getTitle())),
'#title' => $this->t('Enable @title menu link', ['@title' => $link->getTitle()]),
'#title_display' => 'invisible',
'#default_value' => $link->isEnabled(),
);
$form[$id]['weight'] = array(
];
$form[$id]['weight'] = [
'#type' => 'weight',
'#delta' => $delta,
'#default_value' => $link->getWeight(),
'#title' => $this->t('Weight for @title', array('@title' => $link->getTitle())),
'#title' => $this->t('Weight for @title', ['@title' => $link->getTitle()]),
'#title_display' => 'invisible',
);
$form[$id]['id'] = array(
];
$form[$id]['id'] = [
'#type' => 'hidden',
'#value' => $link->getPluginId(),
);
$form[$id]['parent'] = array(
];
$form[$id]['parent'] = [
'#type' => 'hidden',
'#default_value' => $link->getParent(),
);
];
// Build a list of operations.
$operations = array();
$operations['edit'] = array(
$operations = [];
$operations['edit'] = [
'title' => $this->t('Edit'),
);
];
// Allow for a custom edit link per plugin.
$edit_route = $link->getEditRoute();
if ($edit_route) {
@ -400,16 +388,16 @@ class MenuForm extends EntityForm {
}
else {
// Fall back to the standard edit link.
$operations['edit'] += array(
$operations['edit'] += [
'url' => Url::fromRoute('menu_ui.link_edit', ['menu_link_plugin' => $link->getPluginId()]),
);
];
}
// Links can either be reset or deleted, not both.
if ($link->isResettable()) {
$operations['reset'] = array(
$operations['reset'] = [
'title' => $this->t('Reset'),
'url' => Url::fromRoute('menu_ui.link_reset', ['menu_link_plugin' => $link->getPluginId()]),
);
];
}
elseif ($delete_link = $link->getDeleteRoute()) {
$operations['delete']['url'] = $delete_link;
@ -417,15 +405,15 @@ class MenuForm extends EntityForm {
$operations['delete']['title'] = $this->t('Delete');
}
if ($link->isTranslatable()) {
$operations['translate'] = array(
$operations['translate'] = [
'title' => $this->t('Translate'),
'url' => $link->getTranslateRoute(),
);
];
}
$form[$id]['operations'] = array(
$form[$id]['operations'] = [
'#type' => 'operations',
'#links' => $operations,
);
];
}
if ($element->subtree) {
@ -461,16 +449,16 @@ class MenuForm extends EntityForm {
// parent. To prevent this, save items in the form in the same order they
// are sent, ensuring parents are saved first, then their children.
// See https://www.drupal.org/node/181126#comment-632270.
$order = is_array($input) ? array_flip(array_keys($input)) : array();
$order = is_array($input) ? array_flip(array_keys($input)) : [];
// Update our original form with the new order.
$form = array_intersect_key(array_merge($order, $form), $form);
$fields = array('weight', 'parent', 'enabled');
$fields = ['weight', 'parent', 'enabled'];
$form_links = $form['links'];
foreach (Element::children($form_links) as $id) {
if (isset($form_links[$id]['#item'])) {
$element = $form_links[$id];
$updated_values = array();
$updated_values = [];
// Update any fields that have changed in this menu item.
foreach ($fields as $field) {
if ($element[$field]['#value'] != $element[$field]['#default_value']) {

View file

@ -18,10 +18,10 @@ class MenuListBuilder extends ConfigEntityListBuilder {
*/
public function buildHeader() {
$header['title'] = t('Title');
$header['description'] = array(
$header['description'] = [
'data' => t('Description'),
'class' => array(RESPONSIVE_PRIORITY_MEDIUM),
);
'class' => [RESPONSIVE_PRIORITY_MEDIUM],
];
return $header + parent::buildHeader();
}
@ -29,10 +29,10 @@ class MenuListBuilder extends ConfigEntityListBuilder {
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['title'] = array(
$row['title'] = [
'data' => $entity->label(),
'class' => array('menu-label'),
);
'class' => ['menu-label'],
];
$row['description']['data'] = ['#markup' => $entity->getDescription()];
return $row + parent::buildRow($entity);
}
@ -45,11 +45,11 @@ class MenuListBuilder extends ConfigEntityListBuilder {
if (isset($operations['edit'])) {
$operations['edit']['title'] = t('Edit menu');
$operations['add'] = array(
$operations['add'] = [
'title' => t('Add link'),
'weight' => 20,
'url' => $entity->urlInfo('add-link-form'),
);
];
}
if (isset($operations['delete'])) {
$operations['delete']['title'] = t('Delete menu');

View file

@ -21,36 +21,36 @@ class MenuLanguageTest extends MenuWebTestBase {
*
* @var array
*/
public static $modules = array('language');
public static $modules = ['language'];
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser(array('access administration pages', 'administer menu')));
$this->drupalLogin($this->drupalCreateUser(['access administration pages', 'administer menu']));
// Add some custom languages.
foreach (array('aa', 'bb', 'cc', 'cs') as $language_code) {
ConfigurableLanguage::create(array(
foreach (['aa', 'bb', 'cc', 'cs'] as $language_code) {
ConfigurableLanguage::create([
'id' => $language_code,
'label' => $this->randomMachineName(),
))->save();
])->save();
}
}
/**
* Tests menu language settings and the defaults for menu link items.
*/
function testMenuLanguage() {
public function testMenuLanguage() {
// Create a test menu to test the various language-related settings.
// Machine name has to be lowercase.
$menu_name = Unicode::strtolower($this->randomMachineName(16));
$label = $this->randomString();
$edit = array(
$edit = [
'id' => $menu_name,
'description' => '',
'label' => $label,
'langcode' => 'aa',
);
];
$this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
->setDefaultLangcode('bb')
@ -65,19 +65,19 @@ class MenuLanguageTest extends MenuWebTestBase {
// Add a menu link.
$link_title = $this->randomString();
$edit = array(
$edit = [
'title[0][value]' => $link_title,
'link[0][uri]' => $link_path,
);
];
$this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
// Check the link was added with the correct menu link default language.
$menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title));
$menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]);
$menu_link = reset($menu_links);
$this->assertMenuLink($menu_link->getPluginId(), array(
$this->assertMenuLink($menu_link->getPluginId(), [
'menu_name' => $menu_name,
'route_name' => '<front>',
'langcode' => 'bb',
));
]);
// Edit menu link default, changing it to cc.
ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
@ -87,30 +87,30 @@ class MenuLanguageTest extends MenuWebTestBase {
// Add a menu link.
$link_title = $this->randomString();
$edit = array(
$edit = [
'title[0][value]' => $link_title,
'link[0][uri]' => $link_path,
);
];
$this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
// Check the link was added with the correct new menu link default language.
$menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title));
$menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]);
$menu_link = reset($menu_links);
$this->assertMenuLink($menu_link->getPluginId(), array(
$this->assertMenuLink($menu_link->getPluginId(), [
'menu_name' => $menu_name,
'route_name' => '<front>',
'langcode' => 'cc',
));
]);
// Now change the language of the new link to 'bb'.
$edit = array(
$edit = [
'langcode[0][value]' => 'bb',
);
];
$this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save'));
$this->assertMenuLink($menu_link->getPluginId(), array(
$this->assertMenuLink($menu_link->getPluginId(), [
'menu_name' => $menu_name,
'route_name' => '<front>',
'langcode' => 'bb',
));
]);
// Saving menu link items ends up on the edit menu page. To check the menu
// link has the correct language default on edit, go to the menu link edit

View file

@ -26,7 +26,7 @@ class MenuNodeTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('menu_ui', 'test_page_test', 'node', 'block', 'locale', 'language', 'content_translation');
public static $modules = ['menu_ui', 'test_page_test', 'node', 'block', 'locale', 'language', 'content_translation'];
protected function setUp() {
parent::setUp();
@ -34,9 +34,9 @@ class MenuNodeTest extends WebTestBase {
$this->drupalPlaceBlock('system_menu_block:main');
$this->drupalPlaceBlock('page_title_block');
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
$this->editor = $this->drupalCreateUser(array(
$this->editor = $this->drupalCreateUser([
'access administration pages',
'administer content types',
'administer menu',
@ -47,14 +47,14 @@ class MenuNodeTest extends WebTestBase {
'update content translations',
'delete content translations',
'translate any entity',
));
]);
$this->drupalLogin($this->editor);
}
/**
* Test creating, editing, deleting menu links via node form widget.
*/
function testMenuNodeFormWidget() {
public function testMenuNodeFormWidget() {
// Verify that cacheability metadata is bubbled from the menu link tree
// access checking that is performed when determining the "default parent
// item" options in menu_ui_form_node_type_form_alter(). The "log out" link
@ -68,9 +68,9 @@ class MenuNodeTest extends WebTestBase {
$this->assertPattern('/<input .* id="edit-menu-title" .* maxlength="' . $max_length . '" .* \/>/', 'Menu link title field has correct maxlength in node add form.');
// Disable the default main menu, so that no menus are enabled.
$edit = array(
$edit = [
'menu_options[main]' => FALSE,
);
];
$this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
// Verify that no menu settings are displayed and nodes can be created.
@ -78,47 +78,47 @@ class MenuNodeTest extends WebTestBase {
$this->assertText(t('Create Basic page'));
$this->assertNoText(t('Menu settings'));
$node_title = $this->randomMachineName();
$edit = array(
$edit = [
'title[0][value]' => $node_title,
'body[0][value]' => $this->randomString(),
);
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($node_title);
$this->assertEqual($node->getTitle(), $edit['title[0][value]']);
// Test that we cannot set a menu item from a menu that is not set as
// available.
$edit = array(
$edit = [
'menu_options[tools]' => 1,
'menu_parent' => 'main:',
);
];
$this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
$this->assertText(t('The selected menu item is not under one of the selected menus.'));
$this->assertNoRaw(t('The content type %name has been updated.', array('%name' => 'Basic page')));
$this->assertNoRaw(t('The content type %name has been updated.', ['%name' => 'Basic page']));
// Enable Tools menu as available menu.
$edit = array(
$edit = [
'menu_options[main]' => 1,
'menu_options[tools]' => 1,
'menu_parent' => 'main:',
);
];
$this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
$this->assertRaw(t('The content type %name has been updated.', array('%name' => 'Basic page')));
$this->assertRaw(t('The content type %name has been updated.', ['%name' => 'Basic page']));
// Test that we can preview a node that will create a menu item.
$edit = array(
$edit = [
'title[0][value]' => $node_title,
'menu[enabled]' => 1,
'menu[title]' => 'Test preview',
);
];
$this->drupalPostForm('node/add/page', $edit, t('Preview'));
// Create a node.
$node_title = $this->randomMachineName();
$edit = array(
$edit = [
'title[0][value]' => $node_title,
'body[0][value]' => $this->randomString(),
);
];
$this->drupalPostForm('node/add/page', $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($node_title);
// Assert that there is no link for the node.
@ -126,9 +126,9 @@ class MenuNodeTest extends WebTestBase {
$this->assertNoLink($node_title);
// Edit the node, enable the menu link setting, but skip the link title.
$edit = array(
$edit = [
'menu[enabled]' => 1,
);
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Assert that there is no link for the node.
$this->drupalGet('test-page');
@ -165,11 +165,11 @@ class MenuNodeTest extends WebTestBase {
// Log back in as normal user.
$this->drupalLogin($this->editor);
// Edit the node and create a menu link.
$edit = array(
$edit = [
'menu[enabled]' => 1,
'menu[title]' => $node_title,
'menu[weight]' => 17,
);
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Assert that the link exists.
$this->drupalGet('test-page');
@ -190,20 +190,20 @@ class MenuNodeTest extends WebTestBase {
$this->assertFalse($link->isEnabled(), 'Saving a node with a disabled menu link keeps the menu link disabled.');
// Edit the node and remove the menu link.
$edit = array(
$edit = [
'menu[enabled]' => FALSE,
);
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Assert that there is no link for the node.
$this->drupalGet('test-page');
$this->assertNoLink($node_title);
// Add a menu link to the Administration menu.
$item = MenuLinkContent::create(array(
$item = MenuLinkContent::create([
'link' => [['uri' => 'entity:node/' . $node->id()]],
'title' => $this->randomMachineName(16),
'menu_name' => 'admin',
));
]);
$item->save();
// Assert that disabled Administration menu is not shown on the
@ -219,14 +219,14 @@ class MenuNodeTest extends WebTestBase {
$item->menu_name->value = 'tools';
$item->save();
// Create a second node.
$child_node = $this->drupalCreateNode(array('type' => 'article'));
$child_node = $this->drupalCreateNode(['type' => 'article']);
// Assign a menu link to the second node, being a child of the first one.
$child_item = MenuLinkContent::create(array(
$child_item = MenuLinkContent::create([
'link' => [['uri' => 'entity:node/' . $child_node->id()]],
'title' => $this->randomMachineName(16),
'parent' => $item->getPluginId(),
'menu_name' => $item->getMenuName(),
));
]);
$child_item->save();
// Edit the first node.
$this->drupalGet('node/' . $node->id() . '/edit');
@ -240,9 +240,9 @@ class MenuNodeTest extends WebTestBase {
/**
* Testing correct loading and saving of menu links via node form widget in a multilingual environment.
*/
function testMultilingualMenuNodeFormWidget() {
public function testMultilingualMenuNodeFormWidget() {
// Setup languages.
$langcodes = array('de');
$langcodes = ['de'];
foreach ($langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)->save();
}
@ -257,7 +257,7 @@ class MenuNodeTest extends WebTestBase {
$this->rebuildContainer();
$languages = array();
$languages = [];
foreach ($langcodes as $langcode) {
$languages[$langcode] = ConfigurableLanguage::load($langcode);
}
@ -265,14 +265,14 @@ class MenuNodeTest extends WebTestBase {
// Use a UI form submission to make the node type and menu link content entity translatable.
$this->drupalLogout();
$this->drupalLogin($this->rootUser);
$edit = array(
$edit = [
'entity_types[node]' => TRUE,
'entity_types[menu_link_content]' => TRUE,
'settings[node][page][settings][language][language_alterable]' => TRUE,
'settings[node][page][translatable]' => TRUE,
'settings[node][page][fields][title]' => TRUE,
'settings[menu_link_content][menu_link_content][translatable]' => TRUE,
);
];
$this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
// Log out and back in as normal user.
@ -297,42 +297,42 @@ class MenuNodeTest extends WebTestBase {
$node->save();
// Edit the node and create a menu link.
$edit = array(
$edit = [
'menu[enabled]' => 1,
'menu[title]' => $node_title,
'menu[weight]' => 17,
);
$options = array('language' => $languages[$langcodes[0]]);
];
$options = ['language' => $languages[$langcodes[0]]];
$url = $node->toUrl('edit-form', $options);
$this->drupalPostForm($url, $edit, t('Save') . ' ' . t('(this translation)'));
// Edit the node in a different language and translate the menu link.
$edit = array(
$edit = [
'menu[enabled]' => 1,
'menu[title]' => $translated_node_title,
'menu[weight]' => 17,
);
$options = array('language' => $languages[$langcodes[1]]);
];
$options = ['language' => $languages[$langcodes[1]]];
$url = $node->toUrl('edit-form', $options);
$this->drupalPostForm($url, $edit, t('Save') . ' ' . t('(this translation)'));
// Assert that the original link exists in the frontend.
$this->drupalGet('node/' . $node->id(), array('language' => $languages[$langcodes[0]]));
$this->drupalGet('node/' . $node->id(), ['language' => $languages[$langcodes[0]]]);
$this->assertLink($node_title);
// Assert that the translated link exists in the frontend.
$this->drupalGet('node/' . $node->id(), array('language' => $languages[$langcodes[1]]));
$this->drupalGet('node/' . $node->id(), ['language' => $languages[$langcodes[1]]]);
$this->assertLink($translated_node_title);
// Revisit the edit page in original language, check the loaded menu item title and save.
$options = array('language' => $languages[$langcodes[0]]);
$options = ['language' => $languages[$langcodes[0]]];
$url = $node->toUrl('edit-form', $options);
$this->drupalGet($url);
$this->assertFieldById('edit-menu-title', $node_title);
$this->drupalPostForm(NULL, [], t('Save') . ' ' . t('(this translation)'));
// Revisit the edit page of the translation and check the loaded menu item title.
$options = array('language' => $languages[$langcodes[1]]);
$options = ['language' => $languages[$langcodes[1]]];
$url = $node->toUrl('edit-form', $options);
$this->drupalGet($url);
$this->assertFieldById('edit-menu-title', $translated_node_title);

View file

@ -10,6 +10,7 @@ use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
/**
* Add a custom menu, add menu links to the custom menu and Tools menu, check
@ -24,7 +25,7 @@ class MenuTest extends MenuWebTestBase {
*
* @var array
*/
public static $modules = array('node', 'block', 'contextual', 'help', 'path', 'test_page_test');
public static $modules = ['node', 'block', 'contextual', 'help', 'path', 'test_page_test'];
/**
* A user with administration rights.
@ -66,20 +67,20 @@ class MenuTest extends MenuWebTestBase {
$this->drupalPlaceBlock('page_title_block');
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
// Create users.
$this->adminUser = $this->drupalCreateUser(array('access administration pages', 'administer blocks', 'administer menu', 'create article content'));
$this->authenticatedUser = $this->drupalCreateUser(array());
$this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer blocks', 'administer menu', 'create article content']);
$this->authenticatedUser = $this->drupalCreateUser([]);
}
/**
* Tests menu functionality using the admin and user interfaces.
*/
function testMenu() {
public function testMenu() {
// Log in the user.
$this->drupalLogin($this->adminUser);
$this->items = array();
$this->items = [];
$this->menu = $this->addCustomMenu();
$this->doMenuTests();
@ -131,7 +132,7 @@ class MenuTest extends MenuWebTestBase {
$instance = $this->getStandardMenuLink();
$old_weight = $instance->getWeight();
// Edit the static menu link.
$edit = array();
$edit = [];
$edit['weight'] = 10;
$id = $instance->getPluginId();
$this->drupalPostForm("admin/structure/menu/link/$id/edit", $edit, t('Save'));
@ -147,16 +148,16 @@ class MenuTest extends MenuWebTestBase {
/**
* Adds a custom menu using CRUD functions.
*/
function addCustomMenuCRUD() {
public function addCustomMenuCRUD() {
// Add a new custom menu.
$menu_name = substr(hash('sha256', $this->randomMachineName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
$label = $this->randomMachineName(16);
$menu = Menu::create(array(
$menu = Menu::create([
'id' => $menu_name,
'label' => $label,
'description' => 'Description text',
));
]);
$menu->save();
// Assert the new menu.
@ -177,25 +178,25 @@ class MenuTest extends MenuWebTestBase {
* @return \Drupal\system\Entity\Menu
* The custom menu that has been created.
*/
function addCustomMenu() {
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);
$label = $this->randomMachineName(16);
$edit = array(
$edit = [
'id' => $menu_name,
'description' => '',
'label' => $label,
);
];
$this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
// Verify that using a menu_name that is too long results in a validation
// message.
$this->assertRaw(t('@name cannot be longer than %max characters but is currently %length characters long.', array(
$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,
'%length' => Unicode::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);
@ -203,13 +204,13 @@ class MenuTest extends MenuWebTestBase {
$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.', array(
$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,
'%length' => Unicode::strlen($menu_name),
)));
]));
// Verify that the confirmation message is displayed.
$this->assertRaw(t('Menu %label has been added.', array('%label' => $label)));
$this->assertRaw(t('Menu %label has been added.', ['%label' => $label]));
$this->drupalGet('admin/structure/menu');
$this->assertText($label, 'Menu created');
@ -230,18 +231,18 @@ class MenuTest extends MenuWebTestBase {
* This deletes the custom menu that is stored in $this->menu and performs
* tests on the menu delete user interface.
*/
function deleteCustomMenu() {
public function deleteCustomMenu() {
$menu_name = $this->menu->id();
$label = $this->menu->label();
// Delete custom menu.
$this->drupalPostForm("admin/structure/menu/manage/$menu_name/delete", array(), t('Delete'));
$this->drupalPostForm("admin/structure/menu/manage/$menu_name/delete", [], t('Delete'));
$this->assertResponse(200);
$this->assertRaw(t('The menu %title has been deleted.', array('%title' => $label)), 'Custom menu was deleted');
$this->assertRaw(t('The menu %title has been deleted.', ['%title' => $label]), 'Custom menu was deleted');
$this->assertNull(Menu::load($menu_name), 'Custom menu was deleted');
// Test if all menu links associated with the menu were removed from
// database.
$result = entity_load_multiple_by_properties('menu_link_content', array('menu_name' => $menu_name));
$result = entity_load_multiple_by_properties('menu_link_content', ['menu_name' => $menu_name]);
$this->assertFalse($result, 'All menu links associated with the custom menu were deleted.');
// Make sure there's no delete button on system menus.
@ -256,7 +257,7 @@ class MenuTest extends MenuWebTestBase {
/**
* Tests menu functionality.
*/
function doMenuTests() {
public function doMenuTests() {
$menu_name = $this->menu->id();
// Test the 'Add link' local action.
@ -264,32 +265,32 @@ class MenuTest extends MenuWebTestBase {
$this->clickLink(t('Add link'));
$link_title = $this->randomString();
$this->drupalPostForm(NULL, array('link[0][uri]' => '/', 'title[0][value]' => $link_title), t('Save'));
$this->drupalPostForm(NULL, ['link[0][uri]' => '/', 'title[0][value]' => $link_title], t('Save'));
$this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
// Test the 'Edit' operation.
$this->clickLink(t('Edit'));
$this->assertFieldByName('title[0][value]', $link_title);
$link_title = $this->randomString();
$this->drupalPostForm(NULL, array('title[0][value]' => $link_title), t('Save'));
$this->drupalPostForm(NULL, ['title[0][value]' => $link_title], t('Save'));
$this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
// Test the 'Delete' operation.
$this->clickLink(t('Delete'));
$this->assertRaw(t('Are you sure you want to delete the custom menu link %item?', array('%item' => $link_title)));
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertRaw(t('Are you sure you want to delete the custom menu link %item?', ['%item' => $link_title]));
$this->drupalPostForm(NULL, [], t('Delete'));
$this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
// Add nodes to use as links for menu links.
$node1 = $this->drupalCreateNode(array('type' => 'article'));
$node2 = $this->drupalCreateNode(array('type' => 'article'));
$node3 = $this->drupalCreateNode(array('type' => 'article'));
$node4 = $this->drupalCreateNode(array('type' => 'article'));
$node1 = $this->drupalCreateNode(['type' => 'article']);
$node2 = $this->drupalCreateNode(['type' => 'article']);
$node3 = $this->drupalCreateNode(['type' => 'article']);
$node4 = $this->drupalCreateNode(['type' => 'article']);
// Create a node with an alias.
$node5 = $this->drupalCreateNode(array(
$node5 = $this->drupalCreateNode([
'type' => 'article',
'path' => array(
'path' => [
'alias' => '/node5',
),
));
],
]);
// Verify add link button.
$this->drupalGet('admin/structure/menu');
@ -309,25 +310,25 @@ class MenuTest extends MenuWebTestBase {
// -- item2
// --- item3
$this->assertMenuLink($item1->getPluginId(), array(
'children' => array($item2->getPluginId(), $item3->getPluginId()),
'parents' => array($item1->getPluginId()),
$this->assertMenuLink($item1->getPluginId(), [
'children' => [$item2->getPluginId(), $item3->getPluginId()],
'parents' => [$item1->getPluginId()],
// We assert the language code here to make sure that the language
// selection element degrades gracefully without the Language module.
'langcode' => 'en',
));
$this->assertMenuLink($item2->getPluginId(), array(
'children' => array($item3->getPluginId()),
'parents' => array($item2->getPluginId(), $item1->getPluginId()),
]);
$this->assertMenuLink($item2->getPluginId(), [
'children' => [$item3->getPluginId()],
'parents' => [$item2->getPluginId(), $item1->getPluginId()],
// See above.
'langcode' => 'en',
));
$this->assertMenuLink($item3->getPluginId(), array(
'children' => array(),
'parents' => array($item3->getPluginId(), $item2->getPluginId(), $item1->getPluginId()),
]);
$this->assertMenuLink($item3->getPluginId(), [
'children' => [],
'parents' => [$item3->getPluginId(), $item2->getPluginId(), $item1->getPluginId()],
// See above.
'langcode' => 'en',
));
]);
// Verify menu links.
$this->verifyMenuLink($item1, $node1);
@ -349,26 +350,26 @@ class MenuTest extends MenuWebTestBase {
// -- item5
// -- item6
$this->assertMenuLink($item4->getPluginId(), array(
'children' => array($item5->getPluginId(), $item6->getPluginId()),
'parents' => array($item4->getPluginId()),
$this->assertMenuLink($item4->getPluginId(), [
'children' => [$item5->getPluginId(), $item6->getPluginId()],
'parents' => [$item4->getPluginId()],
// See above.
'langcode' => 'en',
));
$this->assertMenuLink($item5->getPluginId(), array(
'children' => array(),
'parents' => array($item5->getPluginId(), $item4->getPluginId()),
]);
$this->assertMenuLink($item5->getPluginId(), [
'children' => [],
'parents' => [$item5->getPluginId(), $item4->getPluginId()],
'langcode' => 'en',
));
$this->assertMenuLink($item6->getPluginId(), array(
'children' => array(),
'parents' => array($item6->getPluginId(), $item4->getPluginId()),
]);
$this->assertMenuLink($item6->getPluginId(), [
'children' => [],
'parents' => [$item6->getPluginId(), $item4->getPluginId()],
'route_name' => 'entity.node.canonical',
'route_parameters' => array('node' => $node5->id()),
'route_parameters' => ['node' => $node5->id()],
'url' => '',
// See above.
'langcode' => 'en',
));
]);
// Modify menu links.
$this->modifyMenuLink($item1);
@ -389,49 +390,49 @@ class MenuTest extends MenuWebTestBase {
// ---- item3
// -- item6
$this->assertMenuLink($item1->getPluginId(), array(
'children' => array(),
'parents' => array($item1->getPluginId()),
$this->assertMenuLink($item1->getPluginId(), [
'children' => [],
'parents' => [$item1->getPluginId()],
// See above.
'langcode' => 'en',
));
$this->assertMenuLink($item4->getPluginId(), array(
'children' => array($item5->getPluginId(), $item6->getPluginId(), $item2->getPluginId(), $item3->getPluginId()),
'parents' => array($item4->getPluginId()),
]);
$this->assertMenuLink($item4->getPluginId(), [
'children' => [$item5->getPluginId(), $item6->getPluginId(), $item2->getPluginId(), $item3->getPluginId()],
'parents' => [$item4->getPluginId()],
// See above.
'langcode' => 'en',
));
]);
$this->assertMenuLink($item5->getPluginId(), array(
'children' => array($item2->getPluginId(), $item3->getPluginId()),
'parents' => array($item5->getPluginId(), $item4->getPluginId()),
$this->assertMenuLink($item5->getPluginId(), [
'children' => [$item2->getPluginId(), $item3->getPluginId()],
'parents' => [$item5->getPluginId(), $item4->getPluginId()],
// See above.
'langcode' => 'en',
));
$this->assertMenuLink($item2->getPluginId(), array(
'children' => array($item3->getPluginId()),
'parents' => array($item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()),
]);
$this->assertMenuLink($item2->getPluginId(), [
'children' => [$item3->getPluginId()],
'parents' => [$item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()],
// See above.
'langcode' => 'en',
));
$this->assertMenuLink($item3->getPluginId(), array(
'children' => array(),
'parents' => array($item3->getPluginId(), $item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()),
]);
$this->assertMenuLink($item3->getPluginId(), [
'children' => [],
'parents' => [$item3->getPluginId(), $item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()],
// See above.
'langcode' => 'en',
));
]);
// Add 102 menu links with increasing weights, then make sure the last-added
// item's weight doesn't get changed because of the old hardcoded delta=50.
$items = array();
$items = [];
for ($i = -50; $i <= 51; $i++) {
$items[$i] = $this->addMenuLink('', '/node/' . $node1->id(), $menu_name, TRUE, strval($i));
}
$this->assertMenuLink($items[51]->getPluginId(), array('weight' => '51'));
$this->assertMenuLink($items[51]->getPluginId(), ['weight' => '51']);
// Disable a link and then re-enable the link via the overview form.
$this->disableMenuLink($item1);
$edit = array();
$edit = [];
$edit['links[menu_plugin_id:' . $item1->getPluginId() . '][enabled]'] = TRUE;
$this->drupalPostForm('admin/structure/menu/manage/' . $item1->getMenuName(), $edit, t('Save'));
@ -445,15 +446,15 @@ class MenuTest extends MenuWebTestBase {
$item5->save();
// Verify in the database.
$this->assertMenuLink($item1->getPluginId(), array('enabled' => 1));
$this->assertMenuLink($item1->getPluginId(), ['enabled' => 1]);
// Add an external link.
$item7 = $this->addMenuLink('', 'https://www.drupal.org', $menu_name);
$this->assertMenuLink($item7->getPluginId(), array('url' => 'https://www.drupal.org'));
$this->assertMenuLink($item7->getPluginId(), ['url' => 'https://www.drupal.org']);
// Add <front> menu item.
$item8 = $this->addMenuLink('', '/', $menu_name);
$this->assertMenuLink($item8->getPluginId(), array('route_name' => '<front>'));
$this->assertMenuLink($item8->getPluginId(), ['route_name' => '<front>']);
$this->drupalGet('');
$this->assertResponse(200);
// Make sure we get routed correctly.
@ -488,7 +489,7 @@ class MenuTest extends MenuWebTestBase {
/**
* Adds and removes a menu link with a query string and fragment.
*/
function testMenuQueryAndFragment() {
public function testMenuQueryAndFragment() {
$this->drupalLogin($this->adminUser);
// Make a path with query and fragment on.
@ -500,7 +501,7 @@ class MenuTest extends MenuWebTestBase {
// Now change the path to something without query and fragment.
$path = '/test-page';
$this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', array('link[0][uri]' => $path), t('Save'));
$this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', ['link[0][uri]' => $path], t('Save'));
$this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
$this->assertFieldByName('link[0][uri]', $path, 'Path no longer has query or fragment.');
@ -511,7 +512,7 @@ class MenuTest extends MenuWebTestBase {
$this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
$this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
$this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', array(), t('Save'));
$this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', [], t('Save'));
$this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
$this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
@ -520,11 +521,11 @@ class MenuTest extends MenuWebTestBase {
/**
* Tests renaming the built-in menu.
*/
function testSystemMenuRename() {
public function testSystemMenuRename() {
$this->drupalLogin($this->adminUser);
$edit = array(
$edit = [
'label' => $this->randomMachineName(16),
);
];
$this->drupalPostForm('admin/structure/menu/manage/main', $edit, t('Save'));
// Make sure menu shows up with new name in block addition.
@ -537,13 +538,13 @@ class MenuTest extends MenuWebTestBase {
/**
* Tests that menu items pointing to unpublished nodes are editable.
*/
function testUnpublishedNodeMenuItem() {
$this->drupalLogin($this->drupalCreateUser(array('access administration pages', 'administer blocks', 'administer menu', 'create article content', 'bypass node access')));
public function testUnpublishedNodeMenuItem() {
$this->drupalLogin($this->drupalCreateUser(['access administration pages', 'administer blocks', 'administer menu', 'create article content', 'bypass node access']));
// Create an unpublished node.
$node = $this->drupalCreateNode(array(
$node = $this->drupalCreateNode([
'type' => 'article',
'status' => NODE_NOT_PUBLISHED,
));
'status' => NodeInterface::NOT_PUBLISHED,
]);
$item = $this->addMenuLink('', '/node/' . $node->id());
$this->modifyMenuLink($item);
@ -563,20 +564,20 @@ class MenuTest extends MenuWebTestBase {
* Tests the contextual links on a menu block.
*/
public function testBlockContextualLinks() {
$this->drupalLogin($this->drupalCreateUser(array('administer menu', 'access contextual links', 'administer blocks')));
$this->drupalLogin($this->drupalCreateUser(['administer menu', 'access contextual links', 'administer blocks']));
$custom_menu = $this->addCustomMenu();
$this->addMenuLink('', '/', $custom_menu->id());
$block = $this->drupalPlaceBlock('system_menu_block:' . $custom_menu->id(), array('label' => 'Custom menu', 'provider' => 'system'));
$block = $this->drupalPlaceBlock('system_menu_block:' . $custom_menu->id(), ['label' => 'Custom menu', 'provider' => 'system']);
$this->drupalGet('test-page');
$id = 'block:block=' . $block->id() . ':langcode=en|menu:menu=' . $custom_menu->id() . ':langcode=en';
// @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder()
$this->assertRaw('<div data-contextual-id="' . $id . '"></div>', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id)));
$this->assertRaw('<div data-contextual-id="' . $id . '"></div>', format_string('Contextual link placeholder with id @id exists.', ['@id' => $id]));
// Get server-rendered contextual links.
// @see \Drupal\contextual\Tests\ContextualDynamicContextTest:renderContextualLinks()
$post = array('ids[0]' => $id);
$response = $this->drupalPost('contextual/render', 'application/json', $post, array('query' => array('destination' => 'test-page')));
$post = ['ids[0]' => $id];
$response = $this->drupalPost('contextual/render', 'application/json', $post, ['query' => ['destination' => 'test-page']]);
$this->assertResponse(200);
$json = Json::decode($response);
$this->assertIdentical($json[$id], '<ul class="contextual-links"><li class="block-configure"><a href="' . base_path() . 'admin/structure/block/manage/' . $block->id() . '">Configure block</a></li><li class="entitymenuedit-form"><a href="' . base_path() . 'admin/structure/menu/manage/' . $custom_menu->id() . '">Edit menu</a></li></ul>');
@ -601,13 +602,13 @@ class MenuTest extends MenuWebTestBase {
* @return \Drupal\menu_link_content\Entity\MenuLinkContent
* A menu link entity.
*/
function addMenuLink($parent = '', $path = '/', $menu_name = 'tools', $expanded = FALSE, $weight = '0') {
public function addMenuLink($parent = '', $path = '/', $menu_name = 'tools', $expanded = FALSE, $weight = '0') {
// View add menu link page.
$this->drupalGet("admin/structure/menu/manage/$menu_name/add");
$this->assertResponse(200);
$title = '!link_' . $this->randomMachineName(16);
$edit = array(
$edit = [
'link[0][uri]' => $path,
'title[0][value]' => $title,
'description[0][value]' => '',
@ -615,18 +616,18 @@ class MenuTest extends MenuWebTestBase {
'expanded[value]' => $expanded,
'menu_parent' => $menu_name . ':' . $parent,
'weight[0][value]' => $weight,
);
];
// Add menu link.
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertResponse(200);
$this->assertText('The menu link has been saved.');
$menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $title));
$menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $title]);
$menu_link = reset($menu_links);
$this->assertTrue($menu_link, 'Menu link was found in database.');
$this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'children' => array(), 'parent' => $parent));
$this->assertMenuLink($menu_link->getPluginId(), ['menu_name' => $menu_name, 'children' => [], 'parent' => $parent]);
return $menu_link;
}
@ -634,23 +635,23 @@ class MenuTest extends MenuWebTestBase {
/**
* Attempts to add menu link with invalid path or no access permission.
*/
function addInvalidMenuLink() {
foreach (array('access' => '/admin/people/permissions') as $type => $link_path) {
$edit = array(
public function addInvalidMenuLink() {
foreach (['access' => '/admin/people/permissions'] as $type => $link_path) {
$edit = [
'link[0][uri]' => $link_path,
'title[0][value]' => 'title',
);
];
$this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));
$this->assertRaw(t("The path '@link_path' is inaccessible.", array('@link_path' => $link_path)), 'Menu link was not created');
$this->assertRaw(t("The path '@link_path' is inaccessible.", ['@link_path' => $link_path]), 'Menu link was not created');
}
}
/**
* Tests that parent options are limited by depth when adding menu links.
*/
function checkInvalidParentMenuLinks() {
public function checkInvalidParentMenuLinks() {
$last_link = NULL;
$created_links = array();
$created_links = [];
// Get the max depth of the tree.
$menu_link_tree = \Drupal::service('menu.link_tree');
@ -660,7 +661,7 @@ class MenuTest extends MenuWebTestBase {
for ($i = 0; $i <= $max_depth - 1; $i++) {
$parent = $last_link ? 'tools:' . $last_link->getPluginId() : 'tools:';
$title = 'title' . $i;
$edit = array(
$edit = [
'link[0][uri]' => '/',
'title[0][value]' => $title,
'menu_parent' => $parent,
@ -668,9 +669,9 @@ class MenuTest extends MenuWebTestBase {
'enabled[value]' => 1,
'expanded[value]' => FALSE,
'weight[0][value]' => '0',
);
];
$this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));
$menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $title));
$menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $title]);
$last_link = reset($menu_links);
$created_links[] = 'tools:' . $last_link->getPluginId();
}
@ -699,7 +700,7 @@ class MenuTest extends MenuWebTestBase {
* @param object $parent_node
* Parent menu link content node.
*/
function verifyMenuLink(MenuLinkContent $item, $item_node, MenuLinkContent $parent = NULL, $parent_node = NULL) {
public function verifyMenuLink(MenuLinkContent $item, $item_node, MenuLinkContent $parent = NULL, $parent_node = NULL) {
// View home page.
$this->drupalGet('');
$this->assertResponse(200);
@ -713,7 +714,7 @@ class MenuTest extends MenuWebTestBase {
// Verify menu link link.
$this->clickLink($title);
$title = $parent_node->label();
$this->assertTitle(t("@title | Drupal", array('@title' => $title)), 'Parent menu link link target was correct');
$this->assertTitle(t("@title | Drupal", ['@title' => $title]), 'Parent menu link link target was correct');
}
// Verify menu link.
@ -723,7 +724,7 @@ class MenuTest extends MenuWebTestBase {
// Verify menu link link.
$this->clickLink($title);
$title = $item_node->label();
$this->assertTitle(t("@title | Drupal", array('@title' => $title)), 'Menu link link target was correct');
$this->assertTitle(t("@title | Drupal", ['@title' => $title]), 'Menu link link target was correct');
}
/**
@ -736,12 +737,12 @@ class MenuTest extends MenuWebTestBase {
* @param string $menu_name
* The menu the menu link will be moved to.
*/
function moveMenuLink(MenuLinkContent $item, $parent, $menu_name) {
public function moveMenuLink(MenuLinkContent $item, $parent, $menu_name) {
$mlid = $item->id();
$edit = array(
$edit = [
'menu_parent' => $menu_name . ':' . $parent,
);
];
$this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
$this->assertResponse(200);
}
@ -752,14 +753,14 @@ class MenuTest extends MenuWebTestBase {
* @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
* Menu link entity.
*/
function modifyMenuLink(MenuLinkContent $item) {
public function modifyMenuLink(MenuLinkContent $item) {
$item->title->value = $this->randomMachineName(16);
$mlid = $item->id();
$title = $item->getTitle();
// Edit menu link.
$edit = array();
$edit = [];
$edit['title[0][value]'] = $title;
$this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
$this->assertResponse(200);
@ -777,9 +778,9 @@ class MenuTest extends MenuWebTestBase {
* @param int $old_weight
* Original title for menu link.
*/
function resetMenuLink(MenuLinkInterface $menu_link, $old_weight) {
public function resetMenuLink(MenuLinkInterface $menu_link, $old_weight) {
// Reset menu link.
$this->drupalPostForm("admin/structure/menu/link/{$menu_link->getPluginId()}/reset", array(), t('Reset'));
$this->drupalPostForm("admin/structure/menu/link/{$menu_link->getPluginId()}/reset", [], t('Reset'));
$this->assertResponse(200);
$this->assertRaw(t('The menu link was reset to its default settings.'), 'Menu link was reset');
@ -794,14 +795,14 @@ class MenuTest extends MenuWebTestBase {
* @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
* Menu link.
*/
function deleteMenuLink(MenuLinkContent $item) {
public function deleteMenuLink(MenuLinkContent $item) {
$mlid = $item->id();
$title = $item->getTitle();
// Delete menu link.
$this->drupalPostForm("admin/structure/menu/item/$mlid/delete", array(), t('Delete'));
$this->drupalPostForm("admin/structure/menu/item/$mlid/delete", [], t('Delete'));
$this->assertResponse(200);
$this->assertRaw(t('The menu link %title has been deleted.', array('%title' => $title)), 'Menu link was deleted');
$this->assertRaw(t('The menu link %title has been deleted.', ['%title' => $title]), 'Menu link was deleted');
// Verify deletion.
$this->drupalGet('');
@ -814,7 +815,7 @@ class MenuTest extends MenuWebTestBase {
* @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
* Menu link.
*/
function toggleMenuLink(MenuLinkContent $item) {
public function toggleMenuLink(MenuLinkContent $item) {
$this->disableMenuLink($item);
// Verify menu link is absent.
@ -833,14 +834,14 @@ class MenuTest extends MenuWebTestBase {
* @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
* Menu link.
*/
function disableMenuLink(MenuLinkContent $item) {
public function disableMenuLink(MenuLinkContent $item) {
$mlid = $item->id();
$edit['enabled[value]'] = FALSE;
$this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
// Unlike most other modules, there is no confirmation message displayed.
// Verify in the database.
$this->assertMenuLink($item->getPluginId(), array('enabled' => 0));
$this->assertMenuLink($item->getPluginId(), ['enabled' => 0]);
}
/**
@ -849,13 +850,13 @@ class MenuTest extends MenuWebTestBase {
* @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
* Menu link.
*/
function enableMenuLink(MenuLinkContent $item) {
public function enableMenuLink(MenuLinkContent $item) {
$mlid = $item->id();
$edit['enabled[value]'] = TRUE;
$this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
// Verify in the database.
$this->assertMenuLink($item->getPluginId(), array('enabled' => 1));
$this->assertMenuLink($item->getPluginId(), ['enabled' => 1]);
}
/**
@ -863,7 +864,7 @@ class MenuTest extends MenuWebTestBase {
* AJAX callback.
*/
public function testMenuParentsJsAccess() {
$admin = $this->drupalCreateUser(array('administer menu'));
$admin = $this->drupalCreateUser(['administer menu']);
$this->drupalLogin($admin);
// Just check access to the callback overall, the POST data is irrelevant.
$this->drupalGetAjax('admin/structure/menu/parents');

View file

@ -14,7 +14,7 @@ abstract class MenuWebTestBase extends WebTestBase {
*
* @var array
*/
public static $modules = array('menu_ui', 'menu_link_content');
public static $modules = ['menu_ui', 'menu_link_content'];
/**
* Fetches the menu item from the database and compares it to expected item.
@ -24,7 +24,7 @@ abstract class MenuWebTestBase extends WebTestBase {
* @param array $expected_item
* Array containing properties to verify.
*/
function assertMenuLink($menu_plugin_id, array $expected_item) {
public function assertMenuLink($menu_plugin_id, array $expected_item) {
// Retrieve menu link.
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');

View file

@ -1,6 +1,6 @@
<?php
namespace Drupal\menu_ui\Tests;
namespace Drupal\Tests\menu_ui\Functional;
use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
@ -17,7 +17,7 @@ class MenuCacheTagsTest extends PageCacheTagsTestBase {
/**
* {@inheritdoc}
*/
public static $modules = array('menu_ui', 'block', 'test_page_test');
public static $modules = ['menu_ui', 'block', 'test_page_test'];
/**
* Tests cache tags presence and invalidation of the Menu entity.
@ -29,23 +29,24 @@ class MenuCacheTagsTest extends PageCacheTagsTestBase {
$url = Url::fromRoute('test_page_test.test_page');
// Create a Llama menu, add a link to it and place the corresponding block.
$menu = Menu::create(array(
$menu = Menu::create([
'id' => 'llama',
'label' => 'Llama',
'description' => 'Description text',
));
]);
$menu->save();
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
// Move a link into the new menu.
$menu_link = $menu_link_manager->updateDefinition('test_page_test.test_page', array('menu_name' => 'llama', 'parent' => ''));
$block = $this->drupalPlaceBlock('system_menu_block:llama', array('label' => 'Llama', 'provider' => 'system', 'region' => 'footer'));
$menu_link = $menu_link_manager->updateDefinition('test_page_test.test_page', ['menu_name' => 'llama', 'parent' => '']);
$block = $this->drupalPlaceBlock('system_menu_block:llama', ['label' => 'Llama', 'provider' => 'system', 'region' => 'footer']);
// Prime the page cache.
$this->verifyPageCache($url, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$expected_tags = array(
$expected_tags = [
'http_response',
'rendered',
'block_view',
'config:block_list',
@ -54,7 +55,7 @@ class MenuCacheTagsTest extends PageCacheTagsTestBase {
// The cache contexts associated with the (in)accessible menu links are
// bubbled.
'config:user.role.anonymous',
);
];
$this->verifyPageCache($url, 'HIT', $expected_tags);
// Verify that after modifying the menu, there is a cache miss.
@ -67,7 +68,7 @@ class MenuCacheTagsTest extends PageCacheTagsTestBase {
$this->verifyPageCache($url, 'HIT');
// Verify that after modifying the menu link weight, there is a cache miss.
$menu_link_manager->updateDefinition('test_page_test.test_page', array('weight' => -10));
$menu_link_manager->updateDefinition('test_page_test.test_page', ['weight' => -10]);
$this->pass('Test modification of menu link.', 'Debug');
$this->verifyPageCache($url, 'MISS');
@ -76,7 +77,7 @@ class MenuCacheTagsTest extends PageCacheTagsTestBase {
// Verify that after adding a menu link, there is a cache miss.
$this->pass('Test addition of menu link.', 'Debug');
$menu_link_2 = MenuLinkContent::create(array(
$menu_link_2 = MenuLinkContent::create([
'id' => '',
'parent' => '',
'title' => 'Alpaca',
@ -85,7 +86,7 @@ class MenuCacheTagsTest extends PageCacheTagsTestBase {
'uri' => 'internal:/',
]],
'bundle' => 'menu_name',
));
]);
$menu_link_2->save();
$this->verifyPageCache($url, 'MISS');
@ -107,7 +108,7 @@ class MenuCacheTagsTest extends PageCacheTagsTestBase {
$this->verifyPageCache($url, 'MISS');
// Verify a cache hit.
$this->verifyPageCache($url, 'HIT', ['config:block_list', 'config:user.role.anonymous', 'rendered']);
$this->verifyPageCache($url, 'HIT', ['config:block_list', 'config:user.role.anonymous', 'http_response', 'rendered']);
}
}

View file

@ -1,15 +1,15 @@
<?php
namespace Drupal\menu_ui\Tests;
namespace Drupal\Tests\menu_ui\Functional;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
* Reorder menu items.
*
* @group menu_ui
*/
class MenuLinkReorderTest extends WebTestBase {
class MenuLinkReorderTest extends BrowserTestBase {
/**
* An administrator user.
@ -23,12 +23,12 @@ class MenuLinkReorderTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('menu_ui', 'test_page_test', 'node', 'block');
public static $modules = ['menu_ui', 'test_page_test', 'node', 'block'];
/**
* Test creating, editing, deleting menu links via node form widget.
*/
function testDefaultMenuLinkReorder() {
public function testDefaultMenuLinkReorder() {
// Add the main menu block.
$this->drupalPlaceBlock('system_menu_block:main');
@ -38,17 +38,17 @@ class MenuLinkReorderTest extends WebTestBase {
$this->assertLink('Home');
// The administrator user that can re-order menu links.
$this->administrator = $this->drupalCreateUser(array(
$this->administrator = $this->drupalCreateUser([
'administer site configuration',
'access administration pages',
'administer menu',
));
]);
$this->drupalLogin($this->administrator);
// Change the weight of the link to a non default value.
$edit = array(
$edit = [
'links[menu_plugin_id:test_page_test.front_page][weight]' => -10,
);
];
$this->drupalPostForm('admin/structure/menu/manage/main', $edit, t('Save'));
// The link is still there.

View file

@ -1,8 +1,8 @@
<?php
namespace Drupal\menu_ui\Tests;
namespace Drupal\Tests\menu_ui\Functional;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
use Drupal\system\Entity\Menu;
/**
@ -10,22 +10,22 @@ use Drupal\system\Entity\Menu;
*
* @group menu_ui
*/
class MenuUninstallTest extends WebTestBase {
class MenuUninstallTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('menu_ui');
public static $modules = ['menu_ui'];
/**
* Tests Menu uninstall.
*/
public function testMenuUninstall() {
\Drupal::service('module_installer')->uninstall(array('menu_ui'));
\Drupal::service('module_installer')->uninstall(['menu_ui']);
\Drupal::entityManager()->getStorage('menu')->resetCache(array('admin'));
\Drupal::entityManager()->getStorage('menu')->resetCache(['admin']);
$this->assertTrue(Menu::load('admin'), 'The \'admin\' menu still exists after uninstalling Menu UI module.');
}