Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
entity
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
entity
Merge requests
!8
Add an overridden add page with destination support
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Add an overridden add page with destination support
issue/entity-3181860:3181860-add-page-destination
into
8.x-1.x
Overview
0
Commits
4
Pipelines
0
Changes
2
Open
Tobias Zimmermann
requested to merge
issue/entity-3181860:3181860-add-page-destination
into
8.x-1.x
3 years ago
Overview
0
Commits
4
Pipelines
0
Changes
2
Expand
Closes
#3181860
0
0
Merge request reports
Compare
8.x-1.x
version 3
ebaafc0d
3 years ago
version 2
614433f4
3 years ago
version 1
b482af20
3 years ago
8.x-1.x (HEAD)
and
latest version
latest version
a3defef4
4 commits,
3 years ago
version 3
ebaafc0d
3 commits,
3 years ago
version 2
614433f4
1 commit,
3 years ago
version 1
b482af20
1 commit,
3 years ago
2 files
+
91
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
src/Controller/EntityController.php
0 → 100644
+
79
−
0
Options
<?php
namespace
Drupal\entity\Controller
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\Entity\Controller\EntityController
as
BaseEntityController
;
use
Drupal\Core\Link
;
use
Symfony\Component\HttpFoundation\RedirectResponse
;
use
Symfony\Component\HttpFoundation\Request
;
/**
* Provides an add-page with destination support for entities.
*/
class
EntityController
extends
BaseEntityController
{
/**
* Displays add links for the available bundles.
*
* Redirects to the add form if there's only one bundle available.
*
* @param string $entity_type_id
* The entity type ID.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object. If this contains a destination query parameter, it is
* "forwarded" to the add links (or the redirect response).
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|array
* If there's only one available bundle, a redirect response.
* Otherwise, a render array with the add links for each bundle.
*/
public
function
addPage
(
$entity_type_id
,
Request
$request
=
NULL
)
{
$response
=
parent
::
addPage
(
$entity_type_id
);
if
(
$request
&&
$request
->
query
->
has
(
'destination'
))
{
// If the request includes a destination, fetch it to add it to the links
// below, but remove it from the request so that it does not override the
// redirect that happens in case there is only a single bundle.
$destination
=
$request
->
query
->
get
(
'destination'
);
$request
->
query
->
remove
(
'destination'
);
if
(
$response
instanceof
RedirectResponse
)
{
// Override the redirect to include the destination query parameter.
$form_route_name
=
'entity.'
.
$entity_type_id
.
'.add_form'
;
$entity_type
=
$this
->
entityTypeManager
->
getDefinition
(
$entity_type_id
);
$bundle_argument
=
$entity_type
->
getBundleEntityType
()
?:
$entity_type
->
getKey
(
'bundle'
);
$bundle_names
=
array_keys
(
$this
->
entityTypeBundleInfo
->
getBundleInfo
(
$entity_type_id
));
$bundle_name
=
reset
(
$bundle_names
);
$response
=
$this
->
redirect
(
$form_route_name
,
[
$bundle_argument
=>
$bundle_name
],
[
'query'
=>
[
'destination'
=>
$destination
]]
);
}
else
{
assert
(
is_array
(
$response
));
// Make sure that different destination query parameters create separate
// cache entries.
CacheableMetadata
::
createFromRenderArray
(
$response
)
->
addCacheContexts
([
'url.query_args:destination'
])
->
applyTo
(
$response
);
assert
(
isset
(
$response
[
'#bundles'
])
&&
is_array
(
$response
[
'#bundles'
]));
foreach
(
$response
[
'#bundles'
]
as
$bundle
)
{
assert
(
isset
(
$bundle
[
'add_link'
])
&&
(
$bundle
[
'add_link'
]
instanceof
Link
));
// Add the destination to each add link.
$bundle
[
'add_link'
]
->
getUrl
()
->
mergeOptions
([
'query'
=>
[
'destination'
=>
$destination
,
],
]);
}
}
}
return
$response
;
}
}
Loading