diff --git a/Form/RouteChoiceType.php b/Form/RouteChoiceType.php new file mode 100644 index 0000000..d5bb432 --- /dev/null +++ b/Form/RouteChoiceType.php @@ -0,0 +1,54 @@ +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 null; + } + + return $matches[1]; + } + + 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