diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 4394542aac..25e867ad8f 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -32,6 +32,7 @@ use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface; // NEXT_MAJOR: Uncomment next line. // use Sonata\AdminBundle\Util\Instantiator; +use Sonata\AdminBundle\Util\ParametersManipulator; use Sonata\Form\Validator\Constraints\InlineConstraint; use Sonata\Form\Validator\ErrorElement; use Symfony\Component\Form\Extension\Core\Type\HiddenType; @@ -711,7 +712,7 @@ public function getFilterParameters() } } - $parameters = array_replace_recursive($parameters, $filters); + $parameters = ParametersManipulator::merge($parameters, $filters); // always force the parent value if ($this->isChild() && $this->getParentAssociationMapping()) { diff --git a/src/Util/ParametersManipulator.php b/src/Util/ParametersManipulator.php new file mode 100644 index 0000000000..06712712cb --- /dev/null +++ b/src/Util/ParametersManipulator.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Util; + +/** + * @author Willem Verspyck + */ +final class ParametersManipulator +{ + /** + * Merge parameters, but replace them when it's a subarray. + */ + public static function merge(array $parameters, array $newParameters): array + { + foreach (array_intersect_key($parameters, $newParameters) as $key => $parameter) { + if (\is_array($parameter)) { + $parameters[$key] = array_replace($parameter, $newParameters[$key]); + } else { + $parameters[$key] = $newParameters[$key]; + } + } + + return array_merge($parameters, array_diff_key($newParameters, $parameters)); + } +} diff --git a/tests/Util/ParametersManipulatorTest.php b/tests/Util/ParametersManipulatorTest.php new file mode 100644 index 0000000000..d42dbb51b3 --- /dev/null +++ b/tests/Util/ParametersManipulatorTest.php @@ -0,0 +1,152 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\Util; + +use PHPUnit\Framework\TestCase; +use Sonata\AdminBundle\Util\ParametersManipulator; + +/** + * @author Willem Verspyck + */ +class ParametersManipulatorTest extends TestCase +{ + public function provideMergeParameters(): iterable + { + return [ + [ + [ + '_sort_order' => 'DESC', + '_sort_by' => 'id', + 'status' => [ + 'type' => '1', + 'value' => 'foo', + ], + ], + [ + 'status' => [ + 'type' => '2', + 'value' => 'foo', + ], + ], + [ + '_sort_order' => 'DESC', + '_sort_by' => 'id', + 'status' => [ + 'type' => '2', + 'value' => 'foo', + ], + ], + ], + [ + [ + 'status' => [ + 'type' => '1', + ], + ], + [ + 'status' => [ + 'value' => 'foo', + ], + ], + [ + 'status' => [ + 'type' => '1', + 'value' => 'foo', + ], + ], + ], + [ + [ + 'status' => [ + 'type' => '1', + 'value' => 'foo', + ], + ], + [ + 'status' => [ + 'type' => '2', + ], + '_page' => 2, + '_per_page' => 25, + ], + [ + 'status' => [ + 'type' => '2', + 'value' => 'foo', + ], + '_page' => 2, + '_per_page' => 25, + ], + ], + [ + [ + 'status' => [ + 'type' => '1', + 'value' => [ + 'foo', + 'bar', + ], + ], + ], + [ + 'status' => [ + 'value' => [ + 'foo', + ], + ], + ], + [ + 'status' => [ + 'type' => '1', + 'value' => [ + 'foo', + ], + ], + ], + ], + [ + [ + 'status' => [ + 'value' => [ + 'foo', + 'bar', + ], + ], + ], + [ + 'status' => [ + 'value' => [ + 'baz', + ], + ], + ], + [ + 'status' => [ + 'value' => [ + 'baz', + ], + ], + ], + ], + ]; + } + + /** + * @dataProvider provideMergeParameters + */ + public function testMergeParameters(array $parameters, array $newParameters, array $result): void + { + $this->assertSame($result, ParametersManipulator::merge($parameters, $newParameters)); + } +}