1010
1111use eZ \Publish \API \Repository \LocationService ;
1212use eZ \Publish \API \Repository \URLAliasService ;
13+ use eZ \Publish \Core \MVC \ConfigResolverInterface ;
1314use Symfony \Component \Form \FormEvent ;
1415
1516class BuildPathFromRootListener
@@ -20,14 +21,17 @@ class BuildPathFromRootListener
2021 /** @var \eZ\Publish\API\Repository\URLAliasService */
2122 private $ urlAliasService ;
2223
23- /**
24- * @param \eZ\Publish\API\Repository\LocationService $locationService
25- * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
26- */
27- public function __construct (LocationService $ locationService , URLAliasService $ urlAliasService )
28- {
24+ /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
25+ private $ configResolver ;
26+
27+ public function __construct (
28+ LocationService $ locationService ,
29+ URLAliasService $ urlAliasService ,
30+ ConfigResolverInterface $ configResolver
31+ ) {
2932 $ this ->locationService = $ locationService ;
3033 $ this ->urlAliasService = $ urlAliasService ;
34+ $ this ->configResolver = $ configResolver ;
3135 }
3236
3337 /**
@@ -44,10 +48,25 @@ public function onPreSubmitData(FormEvent $event)
4448 if (1 >= $ location ->depth ) {
4549 return ;
4650 }
47- $ parentLocation = $ this ->locationService ->loadLocation ($ location ->parentLocationId );
48- $ urlAlias = $ this ->urlAliasService ->reverseLookup ($ parentLocation );
49- $ data ['path ' ] = $ urlAlias ->path . '/ ' . $ data ['path ' ];
51+ $ data ['path ' ] = $ this ->createPathBasedOnParentLocation ($ location ->parentLocationId , $ data ['path ' ]);
52+ $ event ->setData ($ data );
53+ } elseif (isset ($ data ['site_root ' ]) && true === (bool )$ data ['site_root ' ] && !empty ($ data ['site_access ' ])) {
54+ $ parameterName = 'content.tree_root.location_id ' ;
55+ $ defaultTreeRootLocationId = $ this ->configResolver ->getParameter ($ parameterName );
56+ $ treeRootLocationId = $ this ->configResolver ->hasParameter ($ parameterName , null , $ data ['site_access ' ])
57+ ? $ this ->configResolver ->getParameter ($ parameterName , null , $ data ['site_access ' ])
58+ : $ defaultTreeRootLocationId ;
59+
60+ $ data ['path ' ] = $ this ->createPathBasedOnParentLocation ((int )$ treeRootLocationId , $ data ['path ' ]);
5061 $ event ->setData ($ data );
5162 }
5263 }
64+
65+ private function createPathBasedOnParentLocation (int $ locationId , string $ path ): string
66+ {
67+ $ parentLocation = $ this ->locationService ->loadLocation ($ locationId );
68+ $ urlAlias = $ this ->urlAliasService ->reverseLookup ($ parentLocation );
69+
70+ return $ urlAlias ->path . '/ ' . $ path ;
71+ }
5372}
0 commit comments