Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR-ne pas merge] add Route choice type for page form #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Form/RouteChoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Opera\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RouteChoiceType extends AbstractType
{
private $routes;

public function __construct(RouterInterface $router)
{
foreach ($router->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;
}
}

Nsy marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* 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;
Nsy marked this conversation as resolved.
Show resolved Hide resolved
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'choices' => $this->routes,
]);
}

public function getParent()
{
return \Symfony\Component\Form\Extension\Core\Type\ChoiceType::class;
}
}
2 changes: 1 addition & 1 deletion Resources/config/easy_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
7 changes: 6 additions & 1 deletion Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
# please note that last definitions always *replace* previous ones

Opera\AdminBundle\Form\RouteChoiceType:
arguments: [ '@router' ]
tags:
- { name: 'form.type', alias: 'operaadmin_routechoice' }