EasyCorp / EasyAdminBundle

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

[FEATURE] Breadcrumb #5789

Open ksn135 opened 1 year ago

ksn135 commented 1 year ago

Short description of what this feature will allow to do:

A breadcrumb or breadcrumb trail is a graphical control element used as a navigational aid in user interfaces and on web pages. It allows users to keep track and maintain awareness of their locations within programs, documents, or websites. The term is a reference to the trail of bread crumbs left by Hansel and Gretel in the German fairy tale of the same name.[1]

Example of how to use this feature

To enable breadcrumbs at once and in all modules, you only need to add literally only one function call, the rest will happen completely automatically:

// src/Controller/DashboardController.php
class DashboardController extends AbstractDashboardController
{
   public function configureDashboard(): Dashboard
    {
        return Dashboard::new()
            ->enableBreadcrumb() // <== The Magic Is HERE
            // or change 'Home' to something else:
            // ->enableBreadcrumb('Admin')
            // or for i18n set translation key
            // ->enableBreadcrumb('admin_label.breadcrumb.home')

           ...
       ;
    }

}
Screenshot 2023-06-11 at 14 07 51 Screenshot 2023-06-11 at 14 08 03 Screenshot 2023-06-11 at 14 08 12

Sometimes it is necessary to change the algorithm for generating breadcrumbs. For example, there is a child controller, which is responsible for displaying the details of the object associated with the main object. In this case it is necessary for breadcrumb to be built from the main object. To do this, there is a special way to specify a custom callback function that allows you to change the logic of breadcrumb generation. In the example below, the counterparty has a list of FTS checks, each of which can be viewed in detail on a separate page using the child controller ContractorFnsInfoCrudController.

// src/Controller/ContractorFnsInfoCrudController.php
class ContractorFnsInfoCrudController extends AbstractCrudController
{
    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setBreadcrumbHierarchyCallback(function (null|string|BreadcrumbItem $action): ?BreadcrumbItem {
                return match (true) {
                    $action instanceof BreadcrumbItem && Action::DETAIL === $action->action => new BreadcrumbItem(Action::INDEX, 'admin_label.contractor.plural', null, ContractorCrudController::class),
                    Action::DETAIL === $action => new BreadcrumbItem(
                        Action::DETAIL,
                        fn (ContractorFnsinfo $fnsInfo) => (string) $fnsInfo->getContractor(),
                        null,
                        ContractorCrudController::class,
                        fn (ContractorFnsinfo $fnsInfo) => $fnsInfo->getContractor()?->getId()
                    ),
                    default => null,
                };
            })
           ...
       ;
    }

}
Screenshot 2023-06-11 at 14 37 59 Screenshot 2023-06-11 at 14 39 34

Occasionally you need to prepend some contextual link to the current breadcrumb. For example, in the list of payments you want to add a link to a very frequently used report for easy navigation of users. Or, on the other hand, you want to add a backlink to the list of payments in this report. You can achieve this functionality by using custom PHP attribute BreadcrumbItem on the target controller. Attributes have the highest priority when generating breadcrumbs. The example below shows just such a case.

// src/Controller/PaymentCrudController.php
class PaymentCrudController extends AbstractCrudController
{

    /**
     * The Index
     */
    #[BreadcrumbItem(action: 'stat', label: 'admin_label.payment.page.stat')] 
    public function index(AdminContext $context): KeyValueStore|Response
    {
        return parent::index($context);
    }

    /**
     * The Report
     */
    #[BreadcrumbItem(label: 'admin_label.menu.item.reports', route: 'app_report')]
    #[BreadcrumbItem(action: Action::INDEX)]
    public function stat(): Response
    {
        if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::INDEX, 'entity' => null])) {
            throw $this->createAccessDeniedException();
        }
        /** @var ProjectRepository $repo */
        $repo = $this->em->getRepository(Project::class);

        return $this->render('@Ksn135Company/payment-stat.html.twig', [
            'ea_field_assets' => new AssetsDto(),
            'action_page_title' => 'admin_label.payment.page.stat',
            'rows' => $repo->getRowsForStatPage(),
        ]);
    }

}
Screenshot 2023-06-11 at 14 54 38 Screenshot 2023-06-11 at 14 55 13

I will be glad to hear the opinion of the community

alshenetsky commented 7 months ago

Try this: https://github.com/alshenetsky/easyadmin-breadcrumbs