EasyCorp / EasyAdminBundle

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

Ideas for the Association Field #5365

Open ADSiga opened 2 years ago

ADSiga commented 2 years ago

Short description of what this feature will allow to do: That would allow a quicker way to filter results in an index page that has lots of results.

Example of how to use this feature Instead of clicking on the Filter button and setting different filters, you can simply click on value from an Association Field and that value would become an active filter value.

In some cases, it might not make sense to click on an Association Field to see the values that are contained in that entity. And to make list (sub-)pages with different lists (in that entity's page) may also not be always a good idea.

So what I did, that can for sure be improved is the following:

1) I created a new Association Field Configurator where I added the following code in the configureToManyAssociation function:

` if (is_object($field->getValue())) $className = (new \ReflectionClass($field->getValue()->getOwner()))->getShortName();

    $fieldClass = $field->getFormTypeOption('class');
    $crudController = str_replace('App\\Entity\\','', $fieldClass);
    $crudController = 'App\Controller\Admin\\'.$crudController.'CrudController';

    $crudLink = $this->adminUrlGenerator
        ->setController($crudController)
        ->setAction(Action::INDEX)
        ->unset(EA::MENU_INDEX)
        ->unset(EA::SUBMENU_INDEX)
        ->includeReferrer()
        ->generateUrl();

    if (is_object($field->getValue())) $crudLink .= '&filters['.$className.'][comparison]==&filters['.$className.'][value]='.$field->getValue()->getOwner()->getId();`

1a) set the priority of that configurator in services.yaml file: ` App\EasyAdmin\NewAssociationConfigurator: tags:

2) I created a new associatíon field template where I set the outcome of the formated value as raw: {% if 'toMany' == field.customOptions.get('associationType') %} <span class="badge badge-secondary">{{ field.formattedValue**|raw** }}</span> {% else %}

I still have to find a way to remove the irrelevant filters from the URL.

ADSiga commented 2 years ago

Something i forgot: `private function generateLinkToAssociatedEntity(?string $crudController, EntityDto $entityDto): ?string { if (null === $crudController) { return null; }

    if (str_contains($crudController,'Rechnung') !== false) {
        return $this->adminUrlGenerator
            ->setController($crudController)
            ->setAction(Action::DETAIL)
            ->setEntityId($entityDto->getPrimaryKeyValue())
            ->unset(EA::MENU_INDEX)
            ->unset(EA::SUBMENU_INDEX)
            ->includeReferrer()
            ->generateUrl();
    }

    $parameters = explode('&', $this->adminUrlGenerator
        ->setController($crudController)
        ->setAction(Action::DETAIL)
        ->setEntityId($entityDto->getPrimaryKeyValue())
        ->unset(EA::MENU_INDEX)
        ->unset(EA::SUBMENU_INDEX)
        ->includeReferrer()
        ->generateUrl());

    $entityCrudController = '';

    foreach ($parameters as $parameter) {
        $items = explode('=', $parameter);
        if ($items[0] == 'referrer') {
            $urlItems = explode('&', urldecode($items[1]));
            foreach ($urlItems as $urlItem) {
                $items = explode('=', $urlItem);
                if ($items[0] === 'crudControllerFqcn') $entityCrudController = urldecode($items[1]);
            }
        };
    }

    $array = explode('\\', $crudController);
    $object = end($array);
    $field = str_replace('CrudController','', $object);

    // TODO: check if user has permission to see the related entity
    $url = $this->adminUrlGenerator
        ->setController($entityCrudController)
        ->setAction(Action::INDEX)
        ->unset(EA::MENU_INDEX)
        ->unset(EA::SUBMENU_INDEX)
        ->includeReferrer()
        ->generateUrl();

    return $url.'&filters['.$field.'][comparison]==&filters['.$field.'][value]='.$entityDto->getPrimaryKeyValue();
}`