diff --git a/core/includes/menu.inc b/core/includes/menu.inc index f47b982..454f12c 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -1273,14 +1273,8 @@ function menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = ->condition('plid', $parents, 'IN') ->condition('mlid', $parents, 'NOT IN'); $result = $query->execute(); - $num_rows = FALSE; - if (!empty($result)) { - foreach ($result as $mlid) { - $parents[$mlid] = $mlid; - } - $num_rows = TRUE; - } - } while ($num_rows); + $parents += $result; + } while (!empty($result)); } $tree_parameters['expanded'] = $parents; $tree_parameters['active_trail'] = $active_trail; @@ -1367,7 +1361,6 @@ function _menu_build_tree($menu_name, array $parameters = array()) { } if (!isset($trees[$tree_cid])) { - $links = array(); $query = entity_query('menu_link'); for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) { $query->sort('p' . $i, 'ASC'); @@ -1393,6 +1386,8 @@ function _menu_build_tree($menu_name, array $parameters = array()) { } } + // Build an ordered array of links using the query result object. + $links = array(); if ($result = $query->execute()) { $links = menu_link_load_multiple($result); } @@ -2684,7 +2679,8 @@ function menu_get_router() { */ function _menu_navigation_links_rebuild($menu) { if (module_exists('menu_link')) { - $menu_link_controller = entity_get_controller('menu_link'); + $menu_link_controller = drupal_container()->get('plugin.manager.entity') + ->getStorageController('menu_link'); } else { // The Menu link module is not available at install time, so we need to @@ -2711,11 +2707,12 @@ function _menu_navigation_links_rebuild($menu) { foreach ($router_items as $key => $router_item) { // For performance reasons, do a straight query now and convert to a menu // link entity later. + // @todo revisit before release. $existing_item = db_select('menu_links') - ->fields('menu_links') - ->condition('link_path', $router_item['path']) - ->condition('module', 'system') - ->execute()->fetchAll(); + ->fields('menu_links') + ->condition('link_path', $router_item['path']) + ->condition('module', 'system') + ->execute()->fetchAll(); if ($existing_item) { $existing_item = reset($existing_item); $existing_item->options = unserialize($existing_item->options); diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 00fc0df..c6ae44d 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -5,9 +5,10 @@ * Allows users to create and organize related content in an outline. */ -use Drupal\Core\Template\Attribute; -use Drupal\entity\Plugin\Core\Entity\EntityDisplay; use Drupal\node\Plugin\Core\Entity\Node; +use Drupal\entity\Plugin\Core\Entity\EntityDisplay; +use Drupal\Core\Template\Attribute; +use Drupal\menu_link\Plugin\Core\Entity\MenuLink; use Drupal\menu_link\MenuLinkStorageController; /** @@ -549,9 +550,9 @@ function _book_update_outline(Node $node) { } } - $node->book = entity_create('menu_link', $node->book); - try { - menu_link_save($node->book); + $menu_link = entity_create('menu_link', $node->book); + if ($menu_link->save()) { + $node->book += (array) $menu_link; if ($new) { // Insert new. db_insert('book') @@ -577,10 +578,9 @@ function _book_update_outline(Node $node) { return TRUE; } - catch (Exception $e) { - // Failed to save the menu link. - return FALSE; - } + + // Failed to save the menu link. + return FALSE; } /** diff --git a/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php b/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php index 76bad18..dd94fc7 100644 --- a/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php +++ b/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php @@ -79,12 +79,12 @@ function testMenu() { $item = $this->getStandardMenuLink(); $old_title = $item['link_title']; $this->modifyMenuLink($item); - $item = menu_link_load($item['mlid']); + $item = entity_load('menu_link', $item['mlid']); // Verify that a change to the description is saved. $description = $this->randomName(16); $item['options']['attributes']['title'] = $description; menu_link_save($item); - $saved_item = menu_link_load($item['mlid']); + $saved_item = entity_load('menu_link', $item['mlid']); $this->assertEqual($description, $saved_item['options']['attributes']['title'], 'Saving an existing link updates the description (title attribute)'); $this->resetMenuLink($item, $old_title); } @@ -415,6 +415,9 @@ function modifyMenuLink(&$item) { $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save')); $this->assertResponse(200); $this->assertText('The menu link has been saved.'); + // Verify menu link. + $this->drupalGet('admin/structure/menu/manage/' . $item['menu_name']); + $this->assertText($title, 'Menu link was edited'); } /** @@ -518,7 +521,7 @@ function enableMenuLink($item) { */ function assertMenuLink($mlid, array $expected_item) { // Retrieve menu link. - $item = menu_link_load($mlid); + $item = entity_load('menu_link', $mlid); $options = $item->options; if (!empty($options['query'])) { $item['link_path'] .= '?' . drupal_http_build_query($options['query']); @@ -548,7 +551,7 @@ private function getStandardMenuLink() { $this->assertTrue($mlid > 0, 'Standard menu link id was found'); // Load menu link. // Use api function so that link is translated for rendering. - $item = menu_link_load($mlid); + $item = entity_load('menu_link', $mlid); $this->assertTrue((bool) $item, 'Standard menu link was loaded'); return $item; } diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php index 6b3eaa4..9506602 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php @@ -16,7 +16,7 @@ class MenuLinkFormController extends EntityFormController { /** - * Overrides Drupal\Core\Entity\EntityFormController::form(). + * Overrides EntityFormController::form(). */ public function form(array $form, array &$form_state, EntityInterface $menu_link) { // Since menu_link_load() no longer returns a translated and access checked @@ -112,14 +112,11 @@ public function form(array $form, array &$form_state, EntityInterface $menu_link // Get number of items in menu so the weight selector is sized appropriately. $delta = drupal_container()->get('plugin.manager.entity') ->getStorageController('menu_link')->countMenuLinks($menu_link->menu_name); - if ($delta < 50) { - // Old hardcoded value. - $delta = 50; - } $form['weight'] = array( '#type' => 'weight', '#title' => t('Weight'), - '#delta' => $delta, + // Old hardcoded value. + '#delta' => max($delta, 50), '#default_value' => $menu_link->weight, '#description' => t('Optional. In the menu, the heavier links will sink and the lighter links will be positioned nearer the top.'), ); @@ -135,7 +132,7 @@ public function form(array $form, array &$form_state, EntityInterface $menu_link } /** - * Overrides Drupal\Core\Entity\EntityFormController::actions(). + * Overrides EntityFormController::actions(). */ protected function actions(array $form, array &$form_state) { $element = parent::actions($form, $form_state); @@ -146,12 +143,12 @@ protected function actions(array $form, array &$form_state) { } /** - * Overrides Drupal\Core\Entity\EntityFormController::validate(). + * Overrides EntityFormController::validate(). */ public function validate(array $form, array &$form_state) { $menu_link = $this->buildEntity($form, $form_state); - $normal_path = drupal_container()->get('path.alias_manager')->getSystemPath($menu_link->link_path); + $normal_path = drupal_container()->get('path.alias_manager.cached')->getSystemPath($menu_link->link_path); if ($menu_link->link_path != $normal_path) { drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $menu_link->link_path, '%normal_path' => $normal_path))); $menu_link->link_path = $normal_path; @@ -184,7 +181,7 @@ public function validate(array $form, array &$form_state) { } /** - * Overrides Drupal\Core\Entity\EntityFormController::submit(). + * Overrides EntityFormController::submit(). */ public function submit(array $form, array &$form_state) { // Build the menu link object from the submitted values. @@ -202,7 +199,7 @@ public function submit(array $form, array &$form_state) { } /** - * Overrides Drupal\Core\Entity\EntityFormController::save(). + * Overrides EntityFormController::save(). */ public function save(array $form, array &$form_state) { $menu_link = $this->getEntity($form_state); @@ -220,7 +217,7 @@ public function save(array $form, array &$form_state) { } /** - * Overrides Drupal\Core\Entity\EntityFormController::delete(). + * Overrides EntityFormController::delete(). */ public function delete(array $form, array &$form_state) { $menu_link = $this->getEntity($form_state); diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php index 8ae2ecc..b29cdca 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php @@ -32,29 +32,29 @@ class MenuLinkStorageController extends DatabaseStorageController { protected static $routerItemFields = array(); /** - * Overrides Drupal\Core\Entity\DatabaseStorageController::__construct(). + * Overrides DatabaseStorageController::__construct(). */ public function __construct($entityType) { parent::__construct($entityType); - if (empty(self::$routerItemFields)) { - self::$routerItemFields = array_diff(drupal_schema_fields_sql('menu_router'), array('weight')); + if (empty(static::$routerItemFields)) { + static::$routerItemFields = array_diff(drupal_schema_fields_sql('menu_router'), array('weight')); } } /** - * Overrides Drupal\entity\DatabaseStorageController::buildQuery(). + * Overrides DatabaseStorageController::buildQuery(). */ protected function buildQuery($ids, $revision_id = FALSE) { $query = parent::buildQuery($ids, $revision_id); // Specify additional fields from the {menu_router} table. $query->leftJoin('menu_router', 'm', 'base.router_path = m.path'); - $query->fields('m', self::$routerItemFields); + $query->fields('m', static::$routerItemFields); return $query; } /** - * Overrides Drupal\entity\DatabaseStorageController::attachLoad(). + * Overrides DatabaseStorageController::attachLoad(). * * @todo Don't call parent::attachLoad() at all because we want to be able to * control the entity load hooks. @@ -71,7 +71,7 @@ protected function attachLoad(&$menu_links, $load_revision = FALSE) { } /** - * Overrides Drupal\entity\DatabaseStorageController::save(). + * Overrides DatabaseStorageController::save(). */ public function save(EntityInterface $entity) { // We return SAVED_UPDATED by default because the logic below might not @@ -136,7 +136,7 @@ public function save(EntityInterface $entity) { } /** - * Overrides Drupal\entity\DatabaseStorageController::preSave(). + * Overrides DatabaseStorageController::preSave(). */ protected function preSave(EntityInterface $entity) { // This is the easiest way to handle the unique internal path '', @@ -198,7 +198,7 @@ protected function preSave(EntityInterface $entity) { } /** - * Overrides Drupal\entity\DatabaseStorageController::postSave(). + * DatabaseStorageController::postSave(). */ function postSave(EntityInterface $entity, $update) { // Check the has_children status of the parent. @@ -224,7 +224,7 @@ public function preventReparenting($value = FALSE) { } /** - * Overrides Drupal\entity\DatabaseStorageController::preDelete(). + * Overrides DatabaseStorageController::preDelete(). */ protected function preDelete($entities) { // Nothing to do if we don't want to reparent children. @@ -245,7 +245,7 @@ protected function preDelete($entities) { } /** - * Overrides Drupal\entity\DatabaseStorageController::postDelete(). + * Overrides DatabaseStorageController::postDelete(). */ protected function postDelete($entities) { $affected_menus = array(); diff --git a/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php b/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php index d2e7c06..9d41851 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/Plugin/Core/Entity/MenuLink.php @@ -50,7 +50,7 @@ class MenuLink extends Entity implements \ArrayAccess, ContentEntityInterface { /** * The menu link ID. * - * @var integer + * @var int */ public $mlid; @@ -64,7 +64,7 @@ class MenuLink extends Entity implements \ArrayAccess, ContentEntityInterface { /** * The parent link ID. * - * @var integer + * @var int */ public $plid; @@ -99,42 +99,42 @@ class MenuLink extends Entity implements \ArrayAccess, ContentEntityInterface { public $module = 'menu'; /** - * @var integer + * @var int */ public $hidden = 0; /** - * @var integer + * @var int */ public $external; /** - * @var integer + * @var int */ public $has_children = 0; /** - * @var integer + * @var int */ public $expanded = 0; /** - * @var integer + * @var int */ public $weight = 0; /** - * @var integer + * @var int */ public $depth; /** - * @var integer + * @var int */ public $customized = 0; /** - * @var integer + * @var int * * @todo Investigate whether the p1, p2, .. pX properties can be moved to a * single array property. @@ -142,61 +142,61 @@ class MenuLink extends Entity implements \ArrayAccess, ContentEntityInterface { public $p1; /** - * @var integer + * @var int */ public $p2; /** - * @var integer + * @var int */ public $p3; /** - * @var integer + * @var int */ public $p4; /** - * @var integer + * @var int */ public $p5; /** - * @var integer + * @var int */ public $p6; /** - * @var integer + * @var int */ public $p7; /** - * @var integer + * @var int */ public $p8; /** - * @var integer + * @var int */ public $p9; /** * The menu link modification timestamp. * - * @var integer + * @var int */ public $updated = 0; /** - * Overrides Drupal\Entity\Entity::id(). + * Overrides Entity::id(). */ public function id() { return $this->mlid; } /** - * Overrides Drupal\entity\Entity::createDuplicate(). + * Overrides Entity::createDuplicate(). */ public function createDuplicate() { $duplicate = parent::createDuplicate(); diff --git a/core/modules/menu_link/menu_link.module b/core/modules/menu_link/menu_link.module index bf56799..d18598d 100644 --- a/core/modules/menu_link/menu_link.module +++ b/core/modules/menu_link/menu_link.module @@ -123,14 +123,14 @@ function menu_link_save(MenuLink $menu_link) { /** * Clones an array of menu links. * - * @param array $links + * @param array $links * An array of menu links to clone. * @param string $menu_name * (optional) The name of a menu that the links will be cloned for. If not * set, the cloned links will be in the same menu as the original set of * links that were passed in. * - * @return array + * @return array * An array of menu links with the same properties as the passed-in array, * but with the link identifiers removed so that a new link will be created * when any of them is passed into diff --git a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutStorageController.php b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutStorageController.php index a9a506f..99e8d28 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutStorageController.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutStorageController.php @@ -43,7 +43,7 @@ public function save(EntityInterface $entity) { $id = substr($entity->id(), 0, 23); $entity->set('id', $id); $entity->set('links', $default_set->links); - foreach ($entity->links as &$link) { + foreach ($entity->links as $link) { $link = $link->createDuplicate(); $link->menu_name = $id; unset($link->mlid); diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 8fbbdaf..85c818c 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -236,12 +236,12 @@ function standard_install() { ->execute(); // Create a Home link in the main menu. - $item = entity_create('menu_link', array( + $menu_link = entity_create('menu_link', array( 'link_title' => st('Home'), 'link_path' => '', 'menu_name' => 'main', )); - menu_link_save($item); + $menu_link->save(); // Enable the Contact link in the footer menu. menu_link_maintain('system', 'enable', 'contact'); @@ -250,21 +250,21 @@ function standard_install() { // Populate the default shortcut set. $shortcut_set = shortcut_set_load('default'); - $menu_item = entity_create('menu_link', array( + $menu_link = entity_create('menu_link', array( 'link_path' => 'node/add', 'link_title' => st('Add content'), 'weight' => -20, )); - $menu_item->save(); - $shortcut_set->links[$menu_item->uuid()] = $menu_item; + $menu_link->save(); + $shortcut_set->links[$menu_link->uuid()] = $menu_link; - $menu_item = entity_create('menu_link', array( + $menu_link = entity_create('menu_link', array( 'link_path' => 'admin/content', 'link_title' => st('Find content'), 'weight' => -19, )); - $menu_item->save(); - $shortcut_set->links[$menu_item->uuid()] = $menu_item; + $menu_link->save(); + $shortcut_set->links[$menu_link->uuid()] = $menu_link; $shortcut_set->save();