Issue #2051889 by dawehner, pwolanin, tstoeckler: Add route parameters property to menu links.
parent
48ddeb48db
commit
28f1ad6ad0
|
@ -256,6 +256,13 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface {
|
|||
*/
|
||||
public $route_name;
|
||||
|
||||
/**
|
||||
* The parameters of the route associated with this menu link, if any.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $route_parameters;
|
||||
|
||||
/**
|
||||
* The route object associated with this menu link, if any.
|
||||
*
|
||||
|
@ -485,7 +492,13 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface {
|
|||
}
|
||||
// Find the route_name.
|
||||
if (!isset($this->route_name)) {
|
||||
$this->route_name = $this::findRouteName($this->link_path);
|
||||
if ($result = static::findRouteNameParameters($this->link_path)) {
|
||||
list($this->route_name, $this->route_parameters) = $result;
|
||||
}
|
||||
else {
|
||||
$this->route_name = '';
|
||||
$this->route_parameters = array();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -508,7 +521,7 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function findRouteName($link_path) {
|
||||
public static function findRouteNameParameters($link_path) {
|
||||
// Look up the route_name used for the given path.
|
||||
$request = Request::create('/' . $link_path);
|
||||
$request->attributes->set('_system_path', $link_path);
|
||||
|
@ -517,10 +530,13 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface {
|
|||
// legacy router which will call hook_menu() and you will get back to
|
||||
// this method.
|
||||
$result = \Drupal::service('router.dynamic')->matchRequest($request);
|
||||
return isset($result['_route']) ? $result['_route'] : '';
|
||||
$return = array();
|
||||
$return[] = isset($result['_route']) ? $result['_route'] : '';
|
||||
$return[] = $result['_raw_variables']->all();
|
||||
return $return;
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
return '';
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -56,15 +56,16 @@ interface MenuLinkInterface extends ContentEntityInterface {
|
|||
public static function buildFromRouterItem(array $item);
|
||||
|
||||
/**
|
||||
* Returns the route_name matching a URL.
|
||||
* Returns the route_name and route parameters matching a system path.
|
||||
*
|
||||
* @param string $link_path
|
||||
* The link path to find a route name for.
|
||||
*
|
||||
* @return string
|
||||
* The route name.
|
||||
* @return array
|
||||
* Returns an array with both the route name and parameters, or an empty
|
||||
* array if no route was matched.
|
||||
*/
|
||||
public static function findRouteName($link_path);
|
||||
public static function findRouteNameParameters($link_path);
|
||||
|
||||
/**
|
||||
* Sets the p1 through p9 properties for a menu link entity being saved.
|
||||
|
|
|
@ -111,6 +111,7 @@ class MenuLinkStorageController extends DatabaseStorageController implements Men
|
|||
|
||||
foreach ($menu_links as &$menu_link) {
|
||||
$menu_link->options = unserialize($menu_link->options);
|
||||
$menu_link->route_parameters = unserialize($menu_link->route_parameters);
|
||||
|
||||
// Use the weight property from the menu link.
|
||||
$menu_link->router_item['weight'] = $menu_link->weight;
|
||||
|
|
|
@ -202,6 +202,13 @@ function menu_link_schema() {
|
|||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
),
|
||||
'route_parameters' => array(
|
||||
'description' => 'Serialized array of route parameters of this menu link.',
|
||||
'type' => 'blob',
|
||||
'size' => 'big',
|
||||
'not null' => FALSE,
|
||||
'serialize' => TRUE,
|
||||
),
|
||||
),
|
||||
'indexes' => array(
|
||||
'path_menu' => array(array('link_path', 128), 'menu_name'),
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\menu_link\Tests\Plugin\Core\Entity\MenuLinkTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\menu_link\Tests\Plugin\Core\Entity;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\menu_link\Entity\MenuLink;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
||||
|
||||
/**
|
||||
* Tests the menu link entity class.
|
||||
*
|
||||
* @group Drupal
|
||||
* @group Drupal_menu
|
||||
*
|
||||
* @see \Drupal\menu_link\Plugin\Core\Entity\MenuLink
|
||||
*/
|
||||
class MenuLinkTest extends UnitTestCase {
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Menu link entity',
|
||||
'description' => 'Tests the menu link entity class.',
|
||||
'group' => 'Menu',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the findRouteNameParameters method.
|
||||
*
|
||||
* @see \Drupal\menu_link\Plugin\Core\Entity\MenuLink::findRouteNameParameters()
|
||||
*/
|
||||
public function testFindRouteNameParameters() {
|
||||
$router = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
|
||||
$container = new ContainerBuilder();
|
||||
$container->set('router.dynamic', $router);
|
||||
\Drupal::setContainer($container);
|
||||
|
||||
$router->expects($this->at(0))
|
||||
->method('matchRequest')
|
||||
->will($this->returnValue(array(
|
||||
RouteObjectInterface::ROUTE_NAME => 'view.frontpage.page_1',
|
||||
'_raw_variables' => new ParameterBag(),
|
||||
)));
|
||||
$router->expects($this->at(1))
|
||||
->method('matchRequest')
|
||||
->will($this->returnValue(array(
|
||||
RouteObjectInterface::ROUTE_NAME => 'node_view',
|
||||
'_raw_variables' => new ParameterBag(array('node' => '1')),
|
||||
)));
|
||||
$router->expects($this->at(2))
|
||||
->method('matchRequest')
|
||||
->will($this->returnValue(array(
|
||||
RouteObjectInterface::ROUTE_NAME => 'node_edit',
|
||||
'_raw_variables' => new ParameterBag(array('node' => '2')),
|
||||
)));
|
||||
$router->expects($this->at(3))
|
||||
->method('matchRequest')
|
||||
->will($this->throwException(new ResourceNotFoundException()));
|
||||
|
||||
$this->assertEquals(array('view.frontpage.page_1', array()), MenuLink::findRouteNameParameters('node'));
|
||||
$this->assertEquals(array('node_view', array('node' => '1')), MenuLink::findRouteNameParameters('node/1'));
|
||||
$this->assertEquals(array('node_edit', array('node' => '2')), MenuLink::findRouteNameParameters('node/2/edit'));
|
||||
|
||||
$this->assertEquals(array(), MenuLink::findRouteNameParameters('non-existing'));
|
||||
}
|
||||
|
||||
}
|
|
@ -13,6 +13,14 @@ use Drupal\simpletest\WebTestBase;
|
|||
* Tests for menu links.
|
||||
*/
|
||||
class LinksTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('router_test');
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Menu links',
|
||||
|
@ -229,4 +237,28 @@ class LinksTest extends WebTestBase {
|
|||
);
|
||||
$this->assertMenuLinkParents($links, $expected_hierarchy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the router system integration (route_name and route_parameters).
|
||||
*/
|
||||
public function testRouterIntegration() {
|
||||
$menu_link = entity_create('menu_link', array(
|
||||
'link_path' => 'router_test/test1',
|
||||
));
|
||||
$menu_link->save();
|
||||
$this->assertEqual($menu_link->route_name, 'router_test_1');
|
||||
$this->assertEqual($menu_link->route_parameters, array());
|
||||
|
||||
$menu_link = entity_create('menu_link', array(
|
||||
'link_path' => 'router_test/test3/test',
|
||||
));
|
||||
$menu_link->save();
|
||||
$this->assertEqual($menu_link->route_name, 'router_test_3');
|
||||
$this->assertEqual($menu_link->route_parameters, array('value' => 'test'));
|
||||
|
||||
$menu_link = entity_load('menu_link', $menu_link->id());
|
||||
$this->assertEqual($menu_link->route_name, 'router_test_3');
|
||||
$this->assertEqual($menu_link->route_parameters, array('value' => 'test'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2251,6 +2251,21 @@ function system_upgrade_8060() {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add route_parameters column to the menu_links table.
|
||||
*/
|
||||
function system_update_8060() {
|
||||
$spec = array(
|
||||
'description' => 'Serialized array of route parameters of this menu link.',
|
||||
'type' => 'blob',
|
||||
'size' => 'big',
|
||||
'not null' => FALSE,
|
||||
'serialize' => TRUE,
|
||||
);
|
||||
|
||||
db_add_field('menu_links', 'route_parameters', $spec);
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "defgroup updates-7.x-to-8.x".
|
||||
* The next series of updates should start at 9000.
|
||||
|
|
Loading…
Reference in New Issue