Skip to content

Commit

Permalink
Add a configurePersistentParameters method
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Feb 16, 2021
1 parent 0a107a3 commit fb33ec2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/reference/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,14 @@ Persistent parameters
In some cases, the interface might be required to pass the same parameters
across the different ``Admin``'s actions. Instead of setting them in the
template or doing other weird hacks, you can define a ``getPersistentParameters``
template or doing other weird hacks, you can define a ``configurePersistentParameters``
method. This method will be used when a link is being generated::
// src/Admin/MediaAdmin.php
final class MediaAdmin extends AbstractAdmin
{
public function getPersistentParameters()
public function configurePersistentParameters()
{
if (!$this->getRequest()) {
return [];
Expand Down
37 changes: 26 additions & 11 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2128,22 +2128,29 @@ public function getClassnameLabel()
return $this->classnameLabel;
}

/**
* @final since sonata-project/admin-bundle 3.x
*/
public function getPersistentParameters()
{
$parameters = [];

$parameters = $this->configurePersistentParameters();
foreach ($this->getExtensions() as $extension) {
$params = $extension->getPersistentParameters($this);
// NEXT_MAJOR: Remove the check and the else part.
if (\method_exists($extension, 'configurePersistentParameters')) {
$parameters = $extension->configurePersistentParameters($this, $parameters);
} else {
$params = $extension->getPersistentParameters($this);

// NEXT_MAJOR: Remove this check, since return typehint is added
if (!\is_array($params)) {
throw new \RuntimeException(sprintf(
'The %s::getPersistentParameters must return an array',
\get_class($extension)
));
}
// NEXT_MAJOR: Remove this check, since return typehint is added
if (!\is_array($params)) {
throw new \RuntimeException(sprintf(
'The %s::getPersistentParameters must return an array',
\get_class($extension)
));
}

$parameters = array_merge($parameters, $params);
$parameters = array_merge($parameters, $params);
}
}

return $parameters;
Expand Down Expand Up @@ -2894,6 +2901,14 @@ protected function alterObject(object $object): void
{
}

/**
* @return array<string, mixed>
*/
protected function configurePersistentParameters(): array
{
return [];
}

/**
* @return string[]
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Admin/AdminExtensionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @method void configureDefaultFilterValues(AdminInterface $admin, array &$filterValues)
* @method void configureDefaultSortValues(AdminInterface $admin, array &$sortValues)
* @method void configureFormOptions(AdminInterface $admin, array &$formOptions)
* @method array configurePersistentParameters(AdminInterface $admin, array $parameters)
*
* @phpstan-template T of object
*/
Expand Down Expand Up @@ -150,6 +151,10 @@ public function alterNewInstance(AdminInterface $admin, $object);
public function alterObject(AdminInterface $admin, $object);

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x, use configurePersistentParameters() instead.
*
* Get a chance to add persistent parameters.
*
* @return array<string, mixed>
Expand All @@ -158,6 +163,17 @@ public function alterObject(AdminInterface $admin, $object);
*/
public function getPersistentParameters(AdminInterface $admin);

/**
* Get a chance to add persistent parameters.
*
* @param array<string, mixed> $parameters
*
* @return array<string, mixed>
*
* @phpstan-param AdminInterface<T> $admin
*/
// public function configurePersistentParameters(AdminInterface $admin, array $parameters);

/**
* Return the controller access mapping.
*
Expand Down

0 comments on commit fb33ec2

Please sign in to comment.