Skip to content

Commit c2f46b3

Browse files
Merge pull request #4 from basilicom/feature/additional-relation-type-support
Feature/additional relation type support
2 parents c67f4c7 + 8fa4082 commit c2f46b3

File tree

3 files changed

+72
-41
lines changed

3 files changed

+72
-41
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ if (class_exists('\\Basilicom\\PathFormatterBundle\\BasilicomPathFormatterBundle
6161
6262
6363
## Advanced configuration
64+
### Contextual pattern overwrites
6465
It is possible to configure a context-based pattern, so that a dataObject in a relation-field of a specific class will be formatted differently.
6566
6667
**Example:**
67-
```
68+
```yaml
6869
# app/config/config.yml
6970
basilicom_path_formatter:
7071
pattern:
@@ -82,6 +83,19 @@ basilicom_path_formatter:
8283

8384
While the product will be formatted like ``Sneakers 19.99EUR`` in all relation-fields, the ProductList-Class will show them like ``#13 - Sneakers`` or ``#13 - Sneakers (premium-only!)``, based on the product class.
8485

86+
### Formatting documents and assets
87+
```yaml
88+
basilicom_path_formatter:
89+
pattern:
90+
Pimcore\Model\Asset: "{id} {key}"
91+
Pimcore\Model\Document: "{id} {key}"
92+
93+
Pimcore\Model\DataObject\Car::files:
94+
patternOverwrites:
95+
Pimcore\Model\Asset: "{key}"
96+
Pimcore\Model\Document: "{key}"
97+
```
98+
8599
## Additional features
86100
87101
### Showing images

src/DependencyInjection/BasilicomPathFormatter.php

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Basilicom\PathFormatterBundle\DependencyInjection;
44

55
use Pimcore\Model\Asset;
6-
use Pimcore\Model\DataObject\Concrete;
6+
use Pimcore\Model\Element\AbstractElement;
77
use Pimcore\Model\Element\ElementInterface;
88
use 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

src/DependencyInjection/PimcoreAdapter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Basilicom\PathFormatterBundle\DependencyInjection;
44

5+
use Pimcore\Model\Asset;
6+
use Pimcore\Model\Document;
57
use Pimcore\Model\DataObject\Concrete;
68

79
class PimcoreAdapter
@@ -10,4 +12,14 @@ public function getConcreteById(int $id): ?Concrete
1012
{
1113
return Concrete::getById($id);
1214
}
15+
16+
public function getAssetById(int $id): ?Asset
17+
{
18+
return Asset::getById($id);
19+
}
20+
21+
public function getDocumentById(int $id): ?Document
22+
{
23+
return Document::getById($id);
24+
}
1325
}

0 commit comments

Comments
 (0)