EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.04k stars 1.02k forks source link

[3.0] Add custom page for custom action #3330

Closed hason closed 1 year ago

hason commented 4 years ago

I would like to implement global action trash which show list of deleted entity (custom query builder for softdeleteable entity).

final class ProjectCrudController extends AdminCrudController
{
    public function configureActions(Actions $actions): Actions
    {
        return $actions
            ->add(Crud::PAGE_INDEX, Action::new('trash')->linkToCrudAction('trash')->createAsGlobalAction())
        ;
    }

    public function trash(AdminContext $context)
    {
        return $this->index($context);
    }

   public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
    {
        /* @var $context AdminContext */
        $context = $searchDto->getRequest()->attributes->get('easyadmin_context');
        $action = $context->getCrud()->getCurrentAction();

        $qb = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);

        if ($action === 'trash') {
            $qb->andWhere('entity.deletedAt IS NOT NULL');
            $qb->getEntityManager()->getFilters()->disable('softdeleteable');
        }

        return $qb;
    }
}

Unfortunately, an exception is thrown:

Call to a member function all() on array

Error
in vendor/easycorp/easyadmin-bundle/src/Factory/ActionFactory.php (line 44)
in vendor/easycorp/easyadmin-bundle/src/Factory/EntityFactory.php -> processEntityActions (line 56)
in vendor/easycorp/easyadmin-bundle/src/Factory/EntityFactory.php -> processActions (line 62)
in vendor/easycorp/easyadmin-bundle/src/Controller/AbstractCrudController.php -> processActionsForAll (line 120)

The problem is that I can't define the page that belongs to the action. For all custom actions is pageName set to null (https://github.com/EasyCorp/EasyAdminBundle/blob/master/src/Factory/AdminContextFactory.php#L50-L52).

It would be nice to be able to define CRUD page for custom action, eg. for different lists (Index, Trash, List with predefined filters, etc.), various editing edit pages with different forms…

Note: How to properly get action name in createIndexQueryBuilder method without hack (see example above)?

quentint commented 4 years ago

Same here, apparently actions added in configureActions are not know by $context->getCrud()->getActionsConfig() in AbstractCrudController.

ondraondra81 commented 3 years ago

+1

Feynman1986 commented 3 years ago

+1

saifgo commented 3 years ago

will i get the solution by creating a twig file extending

{% extends '@EasyAdmin/page/content.html.twig' %}
{% block page_content %}
        <h1 class="font-weight-bold text-danger .display1 ">
        {{user_name}}                                                   </h1>
{% endblock %}

and configuring my Action to render that page

public function configureActions(Actions $actions): Actions
    {
        $viewCadeaux = Action::new('viewCadeaux', 'Cadeaux', 'fa fa-file-invoice')
            ->linkToCrudAction('cadeaux');

            return $actions
                ->add(Crud::PAGE_INDEX, $viewCadeaux);
    }

    public function cadeaux(AdminContext $context)
    {
        $ganante = $context->getEntity()->getInstance();
        return $this->render('winner.html.twig', [
            'user_name' => $ganante->getPlayer()->getName(),
        ]);
    }

Hope this can help 😄

tamert commented 3 years ago

return $actions->add("customPage", Action::index);

unfortunately I haven't found a solution yet

guillermofekete commented 3 years ago

Hi! I'm trying to do something similar... Is it possible to to create (custom) pages besides the built-in ones?

AdminContextFactory->create(...) and crud->setPageTitle(...) are checking that pageName are the four base ones

jazithedev commented 2 years ago

I confirm. I cannot create a custom action even though the docs are telling otherwise ☹️. I think it is a BUG and not a FEATURE.

I've created:

$operatorList = Action::new('operatorList', 'Operator list')
    ->linkToCrudAction('operatorList');

$actions->add(Crud::PAGE_DETAIL, $operatorList);

and getting:

Call to a member function all() on array

There is something wrong in ActionFactory, as $actionsDto->getActions() tells that can return an array also, which is invalid when executing all() method from its output.