-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SonataExtraBundle] GenericFormHandler for both batch and entity cust…
…om form
- Loading branch information
Showing
13 changed files
with
359 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Controller\Admin; | ||
|
||
use App\Entity\User; | ||
use App\Form\AddRolesForm; | ||
use Draw\Bundle\SonataExtraBundle\ActionableAdmin\GenericFormHandler; | ||
use Draw\Bundle\SonataExtraBundle\ActionableAdmin\ObjectActionExecutioner; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class AddRolesAminAction | ||
{ | ||
public function __invoke( | ||
ObjectActionExecutioner $objectActionExecutioner, | ||
Request $request, | ||
GenericFormHandler $genericFormHandler | ||
): Response { | ||
return $genericFormHandler | ||
->execute( | ||
$objectActionExecutioner, | ||
$request, | ||
AddRolesForm::class, | ||
null, | ||
function (User $user, array $data) use ($objectActionExecutioner): void { | ||
$roles = $data['roles']; | ||
|
||
$missingRoles = array_diff($roles, $user->getRoles()); | ||
|
||
if (\count($missingRoles) > 0) { | ||
$user->setRoles(array_merge($user->getRoles(), $missingRoles)); | ||
$objectActionExecutioner->getAdmin()->update($user); | ||
} else { | ||
$objectActionExecutioner->skip('all-roles-already-set'); | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Form; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
class AddRolesForm extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add( | ||
'roles', | ||
ChoiceType::class, | ||
[ | ||
'choices' => [ | ||
'ROLE_ADMIN' => 'ROLE_ADMIN', | ||
'ROLE_USER' => 'ROLE_USER', | ||
], | ||
'multiple' => true, | ||
'expanded' => true, | ||
] | ||
) | ||
->add( | ||
'submit', | ||
SubmitType::class, | ||
['label' => 'submit'] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/sonata-extra-bundle/ActionableAdmin/AdminActionLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\SonataExtraBundle\ActionableAdmin; | ||
|
||
use Draw\Bundle\SonataExtraBundle\ActionableAdmin\Extension\ActionableAdminExtensionInterface; | ||
use Sonata\AdminBundle\Admin\AdminInterface; | ||
|
||
class AdminActionLoader | ||
{ | ||
private array $actions = []; | ||
|
||
/** | ||
* @return array<string,AdminAction> | ||
*/ | ||
public function getActions(AdminInterface $admin): array | ||
{ | ||
if (!\array_key_exists($admin->getCode(), $this->actions)) { | ||
$actions = $admin instanceof ActionableAdminInterface | ||
? $admin->getActions() | ||
: []; | ||
|
||
foreach ($admin->getExtensions() as $extension) { | ||
if (!$extension instanceof ActionableAdminExtensionInterface) { | ||
continue; | ||
} | ||
|
||
$actions = $extension->getActions($actions); | ||
} | ||
|
||
array_walk( | ||
$actions, | ||
function (AdminAction $adminAction) use ($admin): void { | ||
// Set default translation domain | ||
$adminAction->setTranslationDomain($adminAction->getTranslationDomain() ?? $admin->getTranslationDomain()); | ||
} | ||
); | ||
|
||
$this->actions[$admin->getCode()] = $actions; | ||
} | ||
|
||
return $this->actions[$admin->getCode()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.