diff --git a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php index 6741d3540aba13d12673c045c63cc142c5a70025..c1c137f2ded62c50425b1a23b069987c25ed0600 100644 --- a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php +++ b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php @@ -635,11 +635,13 @@ protected function prepareLink(array $link, $intersect = FALSE) { * {@inheritdoc} */ public function loadByProperties(array $properties) { - // @todo Only allow loading by plugin definition properties. - https://www.drupal.org/node/2302165 $query = $this->connection->select($this->table, $this->options); $query->fields($this->table, $this->definitionFields()); foreach ($properties as $name => $value) { + if (!in_array($name, $this->definitionFields())) { + $fields = implode(', ', $this->definitionFields()); + throw new \InvalidArgumentException(String::format('An invalid property name, @name was specified. Allowed property names are: @fields.', array('@name' => $name, '@fields' => $fields))); + } $query->condition($name, $value); } $loaded = $this->safeExecuteSelect($query)->fetchAllAssoc('id', \PDO::FETCH_ASSOC); diff --git a/core/lib/Drupal/Core/Menu/MenuTreeStorageInterface.php b/core/lib/Drupal/Core/Menu/MenuTreeStorageInterface.php index a5d512d4ee5c391cfa68b9381461aee509e79b2b..78028e146f255cb7d0e22fd2f6b121200bd4f515 100644 --- a/core/lib/Drupal/Core/Menu/MenuTreeStorageInterface.php +++ b/core/lib/Drupal/Core/Menu/MenuTreeStorageInterface.php @@ -71,6 +71,9 @@ public function loadMultiple(array $ids); * @param array $properties * The properties to filter by. * + * @throws \InvalidArgumentException + * Thrown if an invalid property name is specified in $properties. + * * @return array * An array of menu link definition arrays. */ diff --git a/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php b/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php index 75b4f1e225fc1a0417f7a9f87aa0da2164c0420b..7f39b377f8e1e37331453cb286e6a19041371bae 100644 --- a/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php +++ b/core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php @@ -299,6 +299,29 @@ public function testSubtreeHeight() { $this->assertEqual($this->treeStorage->getSubtreeHeight($child4->getPluginId()), 1); } + /** + * Tests MenuTreeStorage::loadByProperties(). + */ + public function testLoadByProperties() { + $properties = array('foo' => 'bar'); + $msg = 'An invalid property name throws an exception.'; + try { + $this->treeStorage->loadByProperties($properties); + $this->fail($msg); + } + catch (\InvalidArgumentException $e) { + $this->assertEqual( + 'An invalid property name, foo was specified. Allowed property names are: menu_name, route_name, route_parameters, url, title, title_arguments, title_context, description, parent, weight, options, expanded, hidden, provider, metadata, class, form_class, id.', + $e->getMessage() + ); + $this->pass($msg); + } + $this->addMenuLink(1); + $properties = array(array('menu_name' => 'tools')); + $links = $this->treeStorage->loadByProperties($properties); + $this->assertEqual('tools', $links[1]['menu_name']); + } + /** * Adds a link with the given ID and supply defaults. */