1414use  Symfony \Component \HttpFoundation \Request ;
1515use  InvalidArgumentException ;
1616use  RuntimeException ;
17+ use  Symfony \Contracts \Translation \TranslatorInterface ;
1718
1819class  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 $ eventvoid 
@@ -104,6 +108,7 @@ private function generateBreadcrumb(
104108        \Reflectionmethod   $ method
105109    ): Breadcrumb 
106110        $ title$ breadcrumbgetTitle ();
111+         $ parameters$ breadcrumbgetParameters ();
107112
108113        // We're dealing with an expression, e.g. {item.name} 
109114        if  ($ title0 ] === '{ '  && $ title1 ] === '} ' ) {
@@ -171,6 +176,60 @@ private function generateBreadcrumb(
171176            );
172177        }
173178
179+         if  (count ($ parameters
180+             foreach  ($ parametersas  $ key$ parameterValue
181+                 if  (str_contains ($ parameterValue'. ' )) {
182+                     $ splitexplode ('. ' , $ parameterValue2 );
183+                     $ attributeName$ split0 ];
184+                     $ propertyPath$ split1 ];
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+                 $ namenull ;
199+                 foreach  ($ methodgetParameters () as  $ parameter
200+                     if  ($ parametername  === $ attributeName
201+                         $ name$ parametergetType ()->getName ();
202+                     }
203+                 }
204+ 
205+                 if  ($ namenull ) {
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 ($ namefind ($ 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