Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/development/breadcrumb.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ public function book(Author $author): Response
}
```


It's also possible to add paramters to the translation.

```yaml
breadcrumb.authors: 'Author: %name%'
```

```php
#[Route('/authors/{author}', name:'authors')]
#[Breadcrumb('breadcrumb.authors', parameters: ['%name%' => 'author.name
public function book(Author $author): Response
{
}
```

## Full example
Auteurs > J.K. Rowling > Harry Potter

Expand Down
13 changes: 13 additions & 0 deletions src/Attribute/Breadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
final class Breadcrumb
{
private string $title;
private array $parameters;
private ?Route $route;
private ?Route $parent;

public function __construct(
string $title,
?array $parameters = null,
?array $route = null,
?array $parent = null
) {
Expand All @@ -36,6 +38,12 @@ public function __construct(
} else {
$this->parent = $parent;
}

if ($parameters !== null) {
$this->parameters = $parameters;
} else {
$this->parameters = [];
}
}

public function getTitle(): ?string
Expand All @@ -58,6 +66,11 @@ public function getParent(): ?Route
return $this->parent;
}

public function getParameters(): array
{
return $this->parameters;
}

public function hasRoute(): bool
{
return $this->route !== null;
Expand Down
61 changes: 60 additions & 1 deletion src/EventListener/BreadcrumbListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpFoundation\Request;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Contracts\Translation\TranslatorInterface;

class BreadcrumbListener
{
Expand All @@ -22,17 +23,20 @@ class BreadcrumbListener
private BreadcrumbTrail $breadcrumbTrail;
private Request $request;
private EntityManagerInterface $manager;
private TranslatorInterface $translator;

public function __construct(
RouterInterface $router,
PropertyAccessorInterface $propertyAccess,
BreadcrumbTrail $breadcrumbTrail,
EntityManagerInterface $manager
EntityManagerInterface $manager,
TranslatorInterface $translator
) {
$this->router = $router;
$this->propertyAccess = $propertyAccess;
$this->breadcrumbTrail = $breadcrumbTrail;
$this->manager = $manager;
$this->translator = $translator;
}

public function onKernelController(KernelEvent $event): void
Expand Down Expand Up @@ -104,6 +108,7 @@ private function generateBreadcrumb(
\Reflectionmethod $method
): Breadcrumb {
$title = $breadcrumb->getTitle();
$parameters = $breadcrumb->getParameters();

// We're dealing with an expression, e.g. {item.name}
if ($title[0] === '{' && $title[-1] === '}') {
Expand Down Expand Up @@ -171,6 +176,60 @@ private function generateBreadcrumb(
);
}

if (count($parameters)) {
foreach ($parameters as $key => $parameterValue) {
if (str_contains($parameterValue, '.')) {
$split = explode('.', $parameterValue, 2);
$attributeName = $split[0];
$propertyPath = $split[1];
} else {
$attributeName = $parameterValue;
}

if (!$this->request->attributes->has($attributeName)) {
throw new RuntimeException(
'You tried to use {' . $attributeName . '} as a breadcrumb parameter, but there is no ' .
'parameter with that name in the route.'
);
}

$attributeId = $this->request->attributes->get($attributeName);

$name = null;
foreach ($method->getParameters() as $parameter) {
if ($parameter->name === $attributeName) {
$name = $parameter->getType()->getName();
}
}

if ($name === null) {
throw new RuntimeException(
'You tried to use {' . $attributeName . '} as a breadcrumb parameter, but there is no ' .
'parameter with that name in the route.'
);
}

$attribute = $this->manager->getRepository($name)->find($attributeId);

if (!is_object($attribute)) {
throw new RuntimeException(
'Could not resolve entity ' . $name . ' with ID ' . $attributeId
);
}

if (!isset($propertyPath)) {
throw new RuntimeException(
'When using objects in a breadcrumb, you have to specify which method to read.' .
' E.g. {object.name}'
);
}

$parameters[$key] = $this->propertyAccess->getValue($attribute, $propertyPath);
}

$title = $this->translator->trans($title, $parameters);
}

// Just a simple string
return new Breadcrumb($title);
}
Expand Down