-
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] AdminAction improvment (#285)
[SonataExtraBundle] New ObjectActionExecutioner [SonataExtraBundle] ObjectActionExecutioner ExecutionErrorEvent [SonataExtraBundle] ObjectActionExecution rely on options/listener [SonataExtraBundle] GenericFormHandler for both batch and entity custom form
- Loading branch information
Showing
27 changed files
with
1,032 additions
and
324 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
20 changes: 20 additions & 0 deletions
20
packages/sonata-extra-bundle/ActionableAdmin/ActionableAdminInterface.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,20 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\SonataExtraBundle\ActionableAdmin; | ||
|
||
use Draw\Bundle\SonataExtraBundle\ActionableAdmin\Extension\ActionableAdminExtension; | ||
use Sonata\AdminBundle\Admin\AdminInterface; | ||
|
||
interface ActionableAdminInterface extends AdminInterface | ||
{ | ||
/** | ||
* List of AdminAction available for this admin. | ||
* | ||
* Key is the action name. | ||
* | ||
* @see ActionableAdminExtension | ||
* | ||
* @return array<string,AdminAction> | ||
*/ | ||
public function getActions(): array; | ||
} |
13 changes: 0 additions & 13 deletions
13
packages/sonata-extra-bundle/ActionableAdmin/ActionableInterface.php
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.