Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
drupal
Commits
28f1ad6a
Commit
28f1ad6a
authored
Aug 27, 2013
by
alexpott
Browse files
Issue
#2051889
by dawehner, pwolanin, tstoeckler: Add route parameters property to menu links.
parent
48ddeb48
Changes
7
Hide whitespace changes
Inline
Side-by-side
core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php
View file @
28f1ad6a
...
...
@@ -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 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
}
// 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 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $
/**
* {@inheritdoc}
*/
public
static
function
findRouteName
(
$link_path
)
{
public
static
function
findRouteName
Parameters
(
$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 @@ public static function findRouteName($link_path) {
// 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
()
;
}
}
...
...
core/modules/menu_link/lib/Drupal/menu_link/MenuLinkInterface.php
View file @
28f1ad6a
...
...
@@ -56,15 +56,16 @@ public function reset();
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
findRouteName
Parameters
(
$link_path
);
/**
* Sets the p1 through p9 properties for a menu link entity being saved.
...
...
core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php
View file @
28f1ad6a
...
...
@@ -111,6 +111,7 @@ protected function attachLoad(&$menu_links, $load_revision = FALSE) {
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
;
...
...
core/modules/menu_link/menu_link.install
View file @
28f1ad6a
...
...
@@ -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'
),
...
...
core/modules/menu_link/tests/Drupal/menu_link/Tests/Plugin/Core/Entity/MenuLinkTest.php
0 → 100644
View file @
28f1ad6a
<?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'
));
}
}
core/modules/system/lib/Drupal/system/Tests/Menu/LinksTest.php
View file @
28f1ad6a
...
...
@@ -13,6 +13,14 @@
* 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 @@ function testMenuLinkRouterReparenting() {
);
$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'
));
}
}
core/modules/system/system.install
View file @
28f1ad6a
...
...
@@ -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.
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment