Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
221
Merge Requests
221
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
3f56402e
Commit
3f56402e
authored
Jul 25, 2016
by
catch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2767853
by tstoeckler: Provide a route for collections of entities
parent
57c7fdd5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
0 deletions
+81
-0
core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php
...b/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php
+32
-0
core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php
...ests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php
+49
-0
No files found.
core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php
View file @
3f56402e
...
...
@@ -23,6 +23,7 @@
* - add-form
* - edit-form
* - delete-form
* - collection
*
* @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider.
*
...
...
@@ -95,6 +96,10 @@ public function getRoutes(EntityTypeInterface $entity_type) {
$collection
->
add
(
"entity.
{
$entity_type_id
}
.delete_form"
,
$delete_route
);
}
if
(
$collection_route
=
$this
->
getCollectionRoute
(
$entity_type
))
{
$collection
->
add
(
"entity.
{
$entity_type_id
}
.collection"
,
$collection_route
);
}
return
$collection
;
}
...
...
@@ -298,6 +303,33 @@ protected function getDeleteFormRoute(EntityTypeInterface $entity_type) {
}
}
/**
* Gets the collection route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected
function
getCollectionRoute
(
EntityTypeInterface
$entity_type
)
{
// If the entity type does not provide an admin permission, there is no way
// to control access, so we cannot provide a route in a sensible way.
if
(
$entity_type
->
hasLinkTemplate
(
'collection'
)
&&
$entity_type
->
hasListBuilderClass
()
&&
(
$admin_permission
=
$entity_type
->
getAdminPermission
()))
{
$route
=
new
Route
(
$entity_type
->
getLinkTemplate
(
'collection'
));
$route
->
addDefaults
([
'_entity_list'
=>
$entity_type
->
id
(),
// @todo Improve this in https://www.drupal.org/node/2767025
'_title'
=>
'@label entities'
,
'_title_arguments'
=>
[
'@label'
=>
$entity_type
->
getLabel
()],
])
->
setRequirement
(
'_permission'
,
$admin_permission
);
return
$route
;
}
}
/**
* Gets the type of the ID key for a given entity type.
*
...
...
core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php
View file @
3f56402e
...
...
@@ -252,6 +252,52 @@ public function providerTestGetCanonicalRoute() {
return
$data
;
}
/**
* @covers ::getCanonicalRoute
* @dataProvider providerTestGetCollectionRoute
*/
public
function
testGetCollectionRoute
(
Route
$expected
=
NULL
,
EntityTypeInterface
$entity_type
)
{
$route
=
$this
->
routeProvider
->
getCollectionRoute
(
$entity_type
);
$this
->
assertEquals
(
$expected
,
$route
);
}
public
function
providerTestGetCollectionRoute
()
{
$data
=
[];
$entity_type1
=
$this
->
getEntityType
();
$entity_type1
->
hasLinkTemplate
(
'collection'
)
->
willReturn
(
FALSE
);
$data
[
'no_collection_link_template'
]
=
[
NULL
,
$entity_type1
->
reveal
()];
$entity_type2
=
$this
->
getEntityType
();
$entity_type2
->
hasLinkTemplate
(
'collection'
)
->
willReturn
(
TRUE
);
$entity_type2
->
hasListBuilderClass
()
->
willReturn
(
FALSE
);
$data
[
'no_list_builder'
]
=
[
NULL
,
$entity_type2
->
reveal
()];
$entity_type3
=
$this
->
getEntityType
(
$entity_type2
);
$entity_type3
->
hasListBuilderClass
()
->
willReturn
(
TRUE
);
$entity_type3
->
getAdminPermission
()
->
willReturn
(
FALSE
);
$data
[
'no_admin_permission'
]
=
[
NULL
,
$entity_type3
->
reveal
()];
$entity_type4
=
$this
->
getEntityType
(
$entity_type3
);
$entity_type4
->
getAdminPermission
()
->
willReturn
(
'administer the entity type'
);
$entity_type4
->
id
()
->
willReturn
(
'the_entity_type_id'
);
$entity_type4
->
getLabel
()
->
willReturn
(
'The entity type'
);
$entity_type4
->
getLinkTemplate
(
'collection'
)
->
willReturn
(
'/the/collection/link/template'
);
$entity_type4
->
isSubclassOf
(
FieldableEntityInterface
::
class
)
->
willReturn
(
FALSE
);
$route
=
(
new
Route
(
'/the/collection/link/template'
))
->
setDefaults
([
'_entity_list'
=>
'the_entity_type_id'
,
'_title'
=>
'@label entities'
,
'_title_arguments'
=>
[
'@label'
=>
'The entity type'
],
])
->
setRequirements
([
'_permission'
=>
'administer the entity type'
,
]);
$data
[
'collection_route'
]
=
[
clone
$route
,
$entity_type4
->
reveal
()];
return
$data
;
}
/**
* @covers ::getEntityTypeIdKeyType
*/
...
...
@@ -313,5 +359,8 @@ public function getAddFormRoute(EntityTypeInterface $entity_type) {
public
function
getCanonicalRoute
(
EntityTypeInterface
$entity_type
)
{
return
parent
::
getCanonicalRoute
(
$entity_type
);
}
public
function
getCollectionRoute
(
EntityTypeInterface
$entity_type
)
{
return
parent
::
getCollectionRoute
(
$entity_type
);
}
}
Write
Preview
Markdown
is supported
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