diff --git a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php index c50f7b431a..f8c9613c49 100644 --- a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php +++ b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php @@ -296,7 +296,6 @@ protected function doSave(array $link) { if ($original) { $link['mlid'] = $original['mlid']; $link['has_children'] = $original['has_children']; - $affected_menus[$original['menu_name']] = $original['menu_name']; $fields = $this->preSave($link, $original); // If $link matches the $original data then exit early as there are no // changes to make. Use array_diff_assoc() to check if they match because: @@ -308,6 +307,7 @@ protected function doSave(array $link) { if (array_diff_assoc($fields, $original) == [] && array_diff_assoc($original, $fields) == ['mlid' => $link['mlid']]) { return $affected_menus; } + $affected_menus[$original['menu_name']] = $original['menu_name']; } $transaction = $this->connection->startTransaction(); diff --git a/core/lib/Drupal/Core/Menu/MenuTreeStorageInterface.php b/core/lib/Drupal/Core/Menu/MenuTreeStorageInterface.php index 0e36e13c40..f0c61d0ee2 100644 --- a/core/lib/Drupal/Core/Menu/MenuTreeStorageInterface.php +++ b/core/lib/Drupal/Core/Menu/MenuTreeStorageInterface.php @@ -91,10 +91,13 @@ public function loadByRoute($route_name, array $route_parameters = [], $menu_nam /** * Saves a plugin definition to the storage. * + * This function can provide optimizations to avoid saves, when there have + * been no changes. This should be reflected in the return value. + * * @param array $definition * A definition for a \Drupal\Core\Menu\MenuLinkInterface plugin. * - * @return array + * @return string[] * The menu names affected by the save operation. This will be one menu * name if the link is saved to the sane menu, or two if it is saved to a * new menu. diff --git a/core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php b/core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php index ad3f04d0ab..b52f646329 100644 --- a/core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php +++ b/core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php @@ -365,7 +365,21 @@ public function testLoadByProperties() { } /** + * Tests saving without any changes. + */ + public function testResaving() { + $affected_menus = $this->addMenuLink('root'); + $this->assertEquals(['tools' => 'tools'], $affected_menus); + + $menu_link = $this->treeStorage->load('root'); + $this->assertEquals([], $this->treeStorage->save($menu_link)); + } + + /** * Adds a link with the given ID and supply defaults. + * + * @return string[] + * The affected menus. */ protected function addMenuLink($id, $parent = '', $route_name = 'test', $route_parameters = [], $menu_name = 'tools', $extra = []) { $link = [ @@ -378,7 +392,7 @@ protected function addMenuLink($id, $parent = '', $route_name = 'test', $route_p 'options' => [], 'metadata' => [], ] + $extra; - $this->treeStorage->save($link); + return $this->treeStorage->save($link); } /**