Skip to content

Commit

Permalink
Replace create action with new to avoid Sonata standard links
Browse files Browse the repository at this point in the history
  • Loading branch information
yann-eugone committed Oct 10, 2023
1 parent 09ec32a commit 33c5711
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 28 deletions.
23 changes: 6 additions & 17 deletions src/Admin/SavedFiltersAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterfa

protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->clearExcept(['list', 'create']);
$collection->clearExcept(['list']);

$collection->add(
name: 'new',
pattern: 'new',
methods: [Request::METHOD_POST],
);
$collection->add(
name: 'protect',
pattern: "{$this->getRouterIdParameter()}/protect",
Expand Down Expand Up @@ -89,22 +94,6 @@ protected function configureRoutes(RouteCollectionInterface $collection): void
);
}

protected function configureDashboardActions(array $actions): array
{
$actions = parent::configureDashboardActions($actions);
unset($actions['create']);

return $actions;
}

protected function configureActionButtons(array $buttonList, string $action, ?object $object = null): array
{
$actions = parent::configureActionButtons($buttonList, $action, $object);
unset($actions['create']);

return $actions;
}

protected function configureListFields(ListMapper $list): void
{
$list
Expand Down
5 changes: 2 additions & 3 deletions src/Controller/SavedFiltersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@ final class SavedFiltersController extends CRUDController
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly SavedFiltersOwnerAccessorInterface $filterSetHolderAccessor,
private readonly ValidatorInterface $validator,
) {
}

public function createAction(Request $request): JsonResponse
public function newAction(Request $request, ValidatorInterface $validator): JsonResponse
{
$filtersSet = new SavedFilters();
$filtersSet->setName($request->request->get('name'));
$filtersSet->setAdminClass($request->request->get('adminClass'));
$filtersSet->setFilters($request->request->get('filters'));
$filtersSet->grantOwner($this->owner());

$violations = $this->validator->validate($filtersSet);
$violations = $validator->validate($filtersSet);
if (count($violations) > 0) {
return $this->json($violations, Response::HTTP_BAD_REQUEST);
}
Expand Down
2 changes: 1 addition & 1 deletion templates/saved_filters_action.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<i class="fa fa-plus-circle" aria-hidden="true"></i>
{{ 'saved_filters.action.create'|trans }}
</a>
<form action="{{ path('presta_sonatasavedfilters_savedfilters_create') }}" class="hidden">
<form action="{{ path('presta_sonatasavedfilters_savedfilters_new') }}" class="hidden">
<div class="form-group col-sm-12">
<label class="sr-only" for="saved_filters_name">{{ 'saved_filters.field.name'|trans }}</label>
<input type="text" id="saved_filters_name" name="saved_filters[name]" placeholder="{{ 'saved_filters.field.name'|trans }}" class="form-control input-sm" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Presta\SonataSavedFiltersBundle\Tests\App\User;
use Symfony\Component\HttpFoundation\Response;

final class SavedFiltersAdminCreateTest extends AdminTestCase
final class SavedFiltersAdminNewTest extends AdminTestCase
{
public function testCreate(): void
public function testNew(): void
{
// Given
self::$doctrine->persist($admin = new User('admin'));
Expand All @@ -19,7 +19,7 @@ public function testCreate(): void
self::assertSame(0, self::$doctrine->getRepository(SavedFilters::class)->count([]));

// When
self::$client->request('POST', '/presta/sonata-saved-filters/saved-filters/create', [
self::$client->request('POST', '/presta/sonata-saved-filters/saved-filters/new', [
'name' => 'My precious filter',
'adminClass' => User::class,
'filters' => 'filter%5Busername%5D%5Bvalue%5D=john',
Expand All @@ -31,7 +31,7 @@ public function testCreate(): void
self::assertSame(1, self::$doctrine->getRepository(SavedFilters::class)->count([]));
}

public function testCreateInvalid(): void
public function testNewInvalid(): void
{
// Given
self::$doctrine->persist($admin = new User('admin'));
Expand All @@ -40,7 +40,7 @@ public function testCreateInvalid(): void
self::assertSame(0, self::$doctrine->getRepository(SavedFilters::class)->count([]));

// When
self::$client->request('POST', '/presta/sonata-saved-filters/saved-filters/create', [
self::$client->request('POST', '/presta/sonata-saved-filters/saved-filters/new', [
'name' => null,
'adminClass' => null,
'filters' => null,
Expand All @@ -52,13 +52,13 @@ public function testCreateInvalid(): void
self::assertSame(0, self::$doctrine->getRepository(SavedFilters::class)->count([]));
}

public function testCreateNotAuthenticated(): void
public function testNewNotAuthenticated(): void
{
// Given
self::assertSame(0, self::$doctrine->getRepository(SavedFilters::class)->count([]));

// When
self::$client->request('POST', '/presta/sonata-saved-filters/saved-filters/create');
self::$client->request('POST', '/presta/sonata-saved-filters/saved-filters/new');

// Then
self::assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
Expand Down

0 comments on commit 33c5711

Please sign in to comment.