Skip to content

Commit 2710cdc

Browse files
author
Sander De la Marche
committed
Merge branch 'master' of github.com:sumocoders/FrameworkCoreBundle
2 parents 0462823 + 0adf0bb commit 2710cdc

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

docs/development/breadcrumb.md

+15
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ public function book(Author $author): Response
138138
}
139139
```
140140

141+
142+
It's also possible to add paramters to the translation.
143+
144+
```yaml
145+
breadcrumb.authors: 'Author: %name%'
146+
```
147+
148+
```php
149+
#[Route('/authors/{author}', name:'authors')]
150+
#[Breadcrumb('breadcrumb.authors', parameters: ['%name%' => 'author.name
151+
public function book(Author $author): Response
152+
{
153+
}
154+
```
155+
141156
## Full example
142157
Auteurs > J.K. Rowling > Harry Potter
143158

src/Attribute/Breadcrumb.php

+13
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
final class Breadcrumb
1010
{
1111
private string $title;
12+
private array $parameters;
1213
private ?Route $route;
1314
private ?Route $parent;
1415

1516
public function __construct(
1617
string $title,
18+
?array $parameters = null,
1719
?array $route = null,
1820
?array $parent = null
1921
) {
@@ -36,6 +38,12 @@ public function __construct(
3638
} else {
3739
$this->parent = $parent;
3840
}
41+
42+
if ($parameters !== null) {
43+
$this->parameters = $parameters;
44+
} else {
45+
$this->parameters = [];
46+
}
3947
}
4048

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

69+
public function getParameters(): array
70+
{
71+
return $this->parameters;
72+
}
73+
6174
public function hasRoute(): bool
6275
{
6376
return $this->route !== null;

src/EventListener/BreadcrumbListener.php

+60-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpFoundation\Request;
1515
use InvalidArgumentException;
1616
use RuntimeException;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
1718

1819
class BreadcrumbListener
1920
{
@@ -22,17 +23,20 @@ class BreadcrumbListener
2223
private BreadcrumbTrail $breadcrumbTrail;
2324
private Request $request;
2425
private EntityManagerInterface $manager;
26+
private TranslatorInterface $translator;
2527

2628
public function __construct(
2729
RouterInterface $router,
2830
PropertyAccessorInterface $propertyAccess,
2931
BreadcrumbTrail $breadcrumbTrail,
30-
EntityManagerInterface $manager
32+
EntityManagerInterface $manager,
33+
TranslatorInterface $translator
3134
) {
3235
$this->router = $router;
3336
$this->propertyAccess = $propertyAccess;
3437
$this->breadcrumbTrail = $breadcrumbTrail;
3538
$this->manager = $manager;
39+
$this->translator = $translator;
3640
}
3741

3842
public function onKernelController(KernelEvent $event): void
@@ -104,6 +108,7 @@ private function generateBreadcrumb(
104108
\Reflectionmethod $method
105109
): Breadcrumb {
106110
$title = $breadcrumb->getTitle();
111+
$parameters = $breadcrumb->getParameters();
107112

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

179+
if (count($parameters)) {
180+
foreach ($parameters as $key => $parameterValue) {
181+
if (str_contains($parameterValue, '.')) {
182+
$split = explode('.', $parameterValue, 2);
183+
$attributeName = $split[0];
184+
$propertyPath = $split[1];
185+
} else {
186+
$attributeName = $parameterValue;
187+
}
188+
189+
if (!$this->request->attributes->has($attributeName)) {
190+
throw new RuntimeException(
191+
'You tried to use {' . $attributeName . '} as a breadcrumb parameter, but there is no ' .
192+
'parameter with that name in the route.'
193+
);
194+
}
195+
196+
$attributeId = $this->request->attributes->get($attributeName);
197+
198+
$name = null;
199+
foreach ($method->getParameters() as $parameter) {
200+
if ($parameter->name === $attributeName) {
201+
$name = $parameter->getType()->getName();
202+
}
203+
}
204+
205+
if ($name === null) {
206+
throw new RuntimeException(
207+
'You tried to use {' . $attributeName . '} as a breadcrumb parameter, but there is no ' .
208+
'parameter with that name in the route.'
209+
);
210+
}
211+
212+
$attribute = $this->manager->getRepository($name)->find($attributeId);
213+
214+
if (!is_object($attribute)) {
215+
throw new RuntimeException(
216+
'Could not resolve entity ' . $name . ' with ID ' . $attributeId
217+
);
218+
}
219+
220+
if (!isset($propertyPath)) {
221+
throw new RuntimeException(
222+
'When using objects in a breadcrumb, you have to specify which method to read.' .
223+
' E.g. {object.name}'
224+
);
225+
}
226+
227+
$parameters[$key] = $this->propertyAccess->getValue($attribute, $propertyPath);
228+
}
229+
230+
$title = $this->translator->trans($title, $parameters);
231+
}
232+
174233
// Just a simple string
175234
return new Breadcrumb($title);
176235
}

0 commit comments

Comments
 (0)