From 741e038410e764c0d8e71d51f073f3220c55b987 Mon Sep 17 00:00:00 2001 From: Nsy Date: Thu, 17 Jan 2019 13:05:15 +0100 Subject: [PATCH 1/3] add Route choice type for page form --- Form/RouteChoiceType.php | 55 ++++++++++++++++++++++++++++++++ Resources/config/easy_admin.yaml | 2 +- Resources/config/services.yaml | 7 +++- 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 Form/RouteChoiceType.php diff --git a/Form/RouteChoiceType.php b/Form/RouteChoiceType.php new file mode 100644 index 0000000..12dbc74 --- /dev/null +++ b/Form/RouteChoiceType.php @@ -0,0 +1,55 @@ +getRouteCollection()->all() as $routeName => $route) { + if ($route->getDefault("_controller")) { + $controllerName = $this->getControllerName($route->getDefault("_controller")); + if ($controllerName) { + $this->routes[$controllerName][$routeName] = $routeName; + } else { + $this->routes["Other"][$routeName] = $routeName; + } + } + + } + } + + /** + * ex: + * input -> "Opera\AdminBundle\Controller\AdminPagesController::layout" + * output -> "AdminPagesController" + */ + private function getControllerName(string $controllerRoutePath) + { + preg_match('/.*\\\\(.*)::.*/', $controllerRoutePath, $matches); + + if (isset($matches[1])) { + return $matches[1]; + } + + return null; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'choices' => $this->routes, + ]); + } + + public function getParent() + { + return \Symfony\Component\Form\Extension\Core\Type\ChoiceType::class; + } +} diff --git a/Resources/config/easy_admin.yaml b/Resources/config/easy_admin.yaml index fea8d97..0d7e5d9 100644 --- a/Resources/config/easy_admin.yaml +++ b/Resources/config/easy_admin.yaml @@ -14,7 +14,7 @@ easy_admin: - title - { property: 'slug', type_options: { required: false } } - { property: 'status', type: 'choice', type_options: { choices: { 'published': 'published', 'draft': 'draft' } } } - - route + - { property: 'route', type: 'Opera\AdminBundle\Form\RouteChoiceType' } - { property: 'configuration', type: 'Opera\AdminBundle\Form\JsonType' } - { property: 'requirements', type: 'Opera\AdminBundle\Form\JsonType' } - { property: 'is_regexp', help: 'If is not a route and want dynamics params' } diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 2024c40..fa920d4 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -22,4 +22,9 @@ services: tags: ['controller.service_arguments'] # add more service definitions when explicit configuration is needed - # please note that last definitions always *replace* previous ones \ No newline at end of file + # please note that last definitions always *replace* previous ones + + Opera\AdminBundle\Form\RouteChoiceType: + arguments: [ '@router' ] + tags: + - { name: 'form.type', alias: 'operaadmin_routechoice' } \ No newline at end of file From 0c8103ec4860113f7363b9ce8701bdfae675939a Mon Sep 17 00:00:00 2001 From: Nsy Date: Wed, 11 Dec 2019 18:21:48 +0100 Subject: [PATCH 2/3] return early --- Form/RouteChoiceType.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Form/RouteChoiceType.php b/Form/RouteChoiceType.php index 12dbc74..7315ada 100644 --- a/Form/RouteChoiceType.php +++ b/Form/RouteChoiceType.php @@ -34,11 +34,11 @@ private function getControllerName(string $controllerRoutePath) { preg_match('/.*\\\\(.*)::.*/', $controllerRoutePath, $matches); - if (isset($matches[1])) { - return $matches[1]; + if (!isset($matches[1])) { + return null; } - return null; + return $matches[1]; } public function configureOptions(OptionsResolver $resolver) From 2540126e07b8d18976f5a164c0239351ad4bed40 Mon Sep 17 00:00:00 2001 From: Nsy Date: Wed, 11 Dec 2019 18:22:36 +0100 Subject: [PATCH 3/3] codestyle --- Form/RouteChoiceType.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Form/RouteChoiceType.php b/Form/RouteChoiceType.php index 7315ada..d5bb432 100644 --- a/Form/RouteChoiceType.php +++ b/Form/RouteChoiceType.php @@ -21,7 +21,6 @@ public function __construct(RouterInterface $router) $this->routes["Other"][$routeName] = $routeName; } } - } }