PHP code example of alshenetsky / easyadmin-breadcrumbs
1. Go to this page and download the library: Download alshenetsky/easyadmin-breadcrumbs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alshenetsky / easyadmin-breadcrumbs example snippets
namespace App\Controller\Admin\Breadcrumb;
use Alshenetsky\EasyAdminBreadcrumbs\Breadcrumb\AbstractBreadcrumb;
use Alshenetsky\EasyAdminBreadcrumbs\Breadcrumb\BreadcrumbType;
use App\Entity\User;
class UserIndexBreadcrumb extends AbstractBreadcrumb
{
public function getType(): BreadcrumbType
{
return BreadcrumbType::INDEX;
}
public function getEntityFqdn(): string
{
return User::class;
}
public function getName(): string
{
return 'Users';
}
}
public function supports(AdminContext $context): bool
{
return isset($context->getRequest()->get('filters')['parent']['value']);
}
namespace App\Controller\Admin\Breadcrumb;
use Alshenetsky\EasyAdminBreadcrumbs\Breadcrumb\AbstractBreadcrumb;
use Alshenetsky\EasyAdminBreadcrumbs\Breadcrumb\BreadcrumbData;
use Alshenetsky\EasyAdminBreadcrumbs\Breadcrumb\BreadcrumbType;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
class UserEditBreadcrumb extends AbstractBreadcrumb
{
public function getType(): BreadcrumbType
{
return BreadcrumbType::EDIT;
}
public function getEntityFqdn(): string
{
return User::class;
}
public function getParent(): ?string
{
return UserIndexBreadcrumb::class;
}
public function gather(AdminContext $context): BreadcrumbData
{
return parent::gather($context)
->set('userId', $context->getEntity()->getPrimaryKeyValue())
;
}
public function configure(BreadcrumbData $gatheredData): void
{
/** @var User $user */
$user = $this->getEntityManager()
->getReference(
User::class,
$gatheredData->get('userId')
)
;
$this
->setName(sprintf('Editing user %s', $user->getName()))
->setEntityId($user->getId())
;
}
}
->setName(sprintf('Editing user %s', $user->getName()))
->setUrl(
$this
->getDefaultUrl() // returns EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator instance with controller and action already provided
->setEntityId($user->getId())
->set('foo', 'bar')
)
class OrdersIndexBreadcrumb extends AbstractBreadcrumb
{
public function getType(): BreadcrumbType
{
return BreadcrumbType::INDEX;
}
public function getEntityFqdn(): string
{
return Order::class;
}
public function getParent(): ?string
{
return UserEditBreadcrumb::class;
}
public function gather(AdminContext $context): BreadcrumbData
{
// gather userId from request filters:
return parent::gather($context)
->set('userId', $context->getRequest()->get('filters')['userId']['value'])
;
}
public function provide(BreadcrumbData $gatheredData): BreadcrumbData
{
// provide UserEditBreadcrumb with userId we gathered:
return parent::provide($gatheredData)
->set('userId', $gatheredData->get('userId'))
;
}
public function configure(BreadcrumbData $gatheredData): void
{
$this
->setName('User orders')
->setFilters(['userId' => ['comparison' => '=', 'value' => $gatheredData->get('userId')]])
;
}
}
use \Alshenetsky\EasyAdminBreadcrumbs\Exception\BreadcrumbNotApplicableException;
public function gather(AdminContext $context): BreadcrumbData
{
return parent::gather($context)
->set(
'userId',
$context->getRequest()->get('filters')['userId']['value'] ?? throw new BreadcrumbNotApplicableException() // the absence of a filter value means that the list of ALL orders is now displayed, not just the user's orders. In this case, breadcrumbs are not applicable.
)
;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.