33namespace Basilicom \PathFormatterBundle \DependencyInjection ;
44
55use Pimcore \Model \Asset ;
6- use Pimcore \Model \DataObject \ Concrete ;
6+ use Pimcore \Model \Element \ AbstractElement ;
77use Pimcore \Model \Element \ElementInterface ;
88use Pimcore \Model \DataObject \ClassDefinition \PathFormatterInterface ;
99
@@ -23,12 +23,10 @@ public function __construct(PimcoreAdapter $pimcoreAdapter, bool $enableAssetPre
2323 }
2424
2525 /**
26- * @param array $result containing the nice path info. Modify it or leave it as it is. Pass it out
27- * afterwards!
28- * @param ElementInterface $source the source object
29- * @param array $targets list of nodes describing the target elements
30- * @param array $params optional parameters. may contain additional context information in the future.
31- * to be defined.
26+ * @param array $result containing the nice path info. Modify it or leave it as it is. Pass it out afterwards!
27+ * @param ElementInterface $source the source object
28+ * @param array $targets list of nodes describing the target elements
29+ * @param array $params optional parameters. may contain additional context information in the future. to be defined.
3230 *
3331 * @return array list of display names.
3432 */
@@ -37,29 +35,39 @@ public function formatPath(array $result, ElementInterface $source, array $targe
3735 if (!empty ($ this ->patternConfiguration )) {
3836 foreach ($ targets as $ key => $ item ) {
3937 if ($ item ['type ' ] === 'object ' ) {
40- $ targetObject = $ this ->pimcoreAdapter ->getConcreteById ($ item ['id ' ]);
41-
42- foreach (array_reverse ($ this ->patternConfiguration ) as $ patternKey => $ patternConfig ) {
43- if (strrpos ($ patternKey , ':: ' ) !== false ) {
44- $ formattedPath = $ this ->getFormattedPathWithContext (
45- $ patternKey ,
46- $ patternConfig [ConfigDefinition::PATTERN_OVERWRITES ],
47- $ params ['context ' ],
48- $ source ,
49- $ targetObject
50- );
51- } else {
52- $ formattedPath = $ this ->getFormattedPath (
53- $ patternKey ,
54- $ patternConfig [ConfigDefinition::PATTERN ],
55- $ targetObject
56- );
57- }
58-
59- if (!empty ($ formattedPath )) {
60- $ result [$ key ] = $ formattedPath ;
61- break ;
62- }
38+ $ targetElement = $ this ->pimcoreAdapter ->getConcreteById ($ item ['id ' ]);
39+ } elseif ($ item ['type ' ] === 'asset ' ) {
40+ $ targetElement = $ this ->pimcoreAdapter ->getAssetById ($ item ['id ' ]);
41+ } elseif ($ item ['type ' ] === 'document ' ) {
42+ $ targetElement = $ this ->pimcoreAdapter ->getDocumentById ($ item ['id ' ]);
43+ } else {
44+ continue ;
45+ }
46+
47+ if (!$ targetElement ) {
48+ continue ;
49+ }
50+
51+ foreach (array_reverse ($ this ->patternConfiguration ) as $ patternKey => $ patternConfig ) {
52+ if (strrpos ($ patternKey , ':: ' ) !== false ) {
53+ $ formattedPath = $ this ->getFormattedPathWithContext (
54+ $ patternKey ,
55+ $ patternConfig [ConfigDefinition::PATTERN_OVERWRITES ],
56+ $ params ['context ' ],
57+ $ source ,
58+ $ targetElement
59+ );
60+ } else {
61+ $ formattedPath = $ this ->getFormattedPath (
62+ $ patternKey ,
63+ $ patternConfig [ConfigDefinition::PATTERN ],
64+ $ targetElement
65+ );
66+ }
67+
68+ if (!empty ($ formattedPath )) {
69+ $ result [$ key ] = $ formattedPath ;
70+ break ;
6371 }
6472 }
6573 }
@@ -73,34 +81,31 @@ private function getFormattedPathWithContext(
7381 array $ patternOverwrites ,
7482 array $ context ,
7583 ElementInterface $ source ,
76- ?Concrete $ targetObject
84+ ?AbstractElement $ targetElement
7785 ): string {
7886 $ formattedPath = '' ;
7987
8088 $ contextClassName = substr ($ patternKey , 0 , strpos ($ patternKey , ':: ' ));
8189 $ contextFieldName = substr ($ patternKey , strpos ($ patternKey , ':: ' ) + 2 );
8290
83- $ pattern = '' ;
8491 if (class_exists ($ contextClassName )
8592 && $ source instanceof $ contextClassName
8693 && $ context ['fieldname ' ] === $ contextFieldName
8794 ) {
8895 foreach ($ patternOverwrites as $ className => $ pattern ) {
89- if (class_exists ($ className ) && $ targetObject instanceof $ className ) {
90- $ formattedPath = $ this ->getFormattedPath ($ className , $ pattern , $ targetObject );
96+ if (class_exists ($ className ) && $ targetElement instanceof $ className ) {
97+ $ formattedPath = $ this ->getFormattedPath ($ className , $ pattern , $ targetElement );
9198 break ;
9299 }
93100 }
94-
95101 }
96102
97103 return $ formattedPath ;
98104 }
99105
100-
101- private function getFormattedPath (string $ className , string $ pattern , ?Concrete $ targetObject ): string
106+ private function getFormattedPath (string $ className , string $ pattern , ?AbstractElement $ targetElement ): string
102107 {
103- if (empty ($ pattern ) || !class_exists ($ className ) || !($ targetObject instanceof $ className )) {
108+ if (empty ($ pattern ) || !class_exists ($ className ) || !($ targetElement instanceof $ className )) {
104109 return '' ;
105110 }
106111
@@ -109,8 +114,8 @@ private function getFormattedPath(string $className, string $pattern, ?Concrete
109114 $ formattedPath = $ pattern ;
110115 foreach ($ propertyList as $ property ) {
111116 $ propertyGetter = 'get ' . ucfirst (trim ($ property ));
112- if (method_exists ($ targetObject , $ propertyGetter )) {
113- $ propertyValue = call_user_func ([$ targetObject , $ propertyGetter ]);
117+ if (method_exists ($ targetElement , $ propertyGetter )) {
118+ $ propertyValue = call_user_func ([$ targetElement , $ propertyGetter ]);
114119 if ($ propertyValue instanceof Asset \Image) {
115120 $ imagePath = $ propertyValue ->getFullPath ();
116121 $ replacement = $ this ->enableAssetPreview
0 commit comments