Skip to content

Commit 649eb85

Browse files
authored
Fix url resolution and test compatibility after Sulu 3.0.6 upgrade (#159)
1 parent 6806e3d commit 649eb85

8 files changed

Lines changed: 369 additions & 12 deletions

File tree

.php-cs-fixer.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
'multi_line_extends_each_single_line' => true,
2727
] ,
2828
'linebreak_after_opening_tag' => true,
29-
// 'declare_strict_types' => true,
29+
'declare_strict_types' => true,
3030
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
3131
'native_constant_invocation' => true,
3232
'native_function_casing' => true,

Content/StructureResolver.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313

1414
namespace Sulu\Bundle\HeadlessBundle\Content;
1515

16+
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FieldMetadata;
1617
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata;
1718
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\TypedFormMetadata;
1819
use Sulu\Bundle\AdminBundle\Metadata\MetadataProviderInterface;
1920
use Sulu\Bundle\HeadlessBundle\Content\ExtensionResolver\ExtensionResolverProvider;
2021
use Sulu\Bundle\HttpCacheBundle\ReferenceStore\ReferenceStoreInterface;
2122
use Sulu\Component\Persistence\Model\AuditableInterface;
23+
use Sulu\Content\Application\ContentDataMapper\DataMapper\TemplateDataMapper;
2224
use Sulu\Content\Domain\Model\AuthorInterface;
2325
use Sulu\Content\Domain\Model\DimensionContentInterface;
2426
use Sulu\Content\Domain\Model\ExcerptInterface;
2527
use Sulu\Content\Domain\Model\LinkInterface;
28+
use Sulu\Content\Domain\Model\RoutableInterface;
2629
use Sulu\Content\Domain\Model\SeoInterface;
2730
use Sulu\Content\Domain\Model\ShadowInterface;
2831
use Sulu\Content\Domain\Model\TaxonomyInterface;
@@ -149,6 +152,7 @@ private function resolveTemplateContent(
149152

150153
$fieldMetadataList = $formMetadata->getFlatFieldMetadata();
151154
$templateData = $dimensionContent->getTemplateData();
155+
$templateData = $this->fillRouteFieldsFromRoute($dimensionContent, $templateData, $fieldMetadataList);
152156

153157
if (null !== $properties) {
154158
$filteredFieldMetadata = [];
@@ -216,6 +220,57 @@ private function resolveExtensionProperties(
216220
}
217221
}
218222

223+
/**
224+
* @param array<string, mixed> $templateData
225+
* @param array<string, FieldMetadata> $fieldMetadataList
226+
*
227+
* @return array<string, mixed>
228+
*/
229+
private function fillRouteFieldsFromRoute(
230+
TemplateInterface $dimensionContent,
231+
array $templateData,
232+
array $fieldMetadataList,
233+
): array {
234+
if (!$dimensionContent instanceof RoutableInterface) {
235+
return $templateData;
236+
}
237+
238+
$route = $dimensionContent->getRoute();
239+
if (null === $route) {
240+
return $templateData;
241+
}
242+
243+
foreach ($fieldMetadataList as $name => $field) {
244+
if (!$field->hasTag(TemplateDataMapper::SKIP_TAG)) {
245+
continue;
246+
}
247+
248+
$type = $field->getType();
249+
if ('route' === $type) {
250+
$templateData[$name] = $route->getSlug();
251+
} elseif ('page_tree_route' === $type) {
252+
$parentRoute = $route->getParentRoute();
253+
if (null === $parentRoute) {
254+
continue;
255+
}
256+
$parentSlug = $parentRoute->getSlug();
257+
$slug = $route->getSlug();
258+
$suffix = \str_starts_with($slug, $parentSlug)
259+
? \substr($slug, \strlen($parentSlug))
260+
: '';
261+
$templateData[$name] = [
262+
'page' => [
263+
'uuid' => $parentRoute->getResourceId(),
264+
'path' => $parentSlug,
265+
],
266+
'suffix' => $suffix,
267+
];
268+
}
269+
}
270+
271+
return $templateData;
272+
}
273+
219274
/**
220275
* @return array<string, mixed>
221276
*/

Tests/Functional/Controller/responses/navigation__get.json

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,58 @@
2424
"creator": null,
2525
"created": "@string@.isDateTime()",
2626
"order": "@integer@",
27-
"children": []
27+
"children": [
28+
{
29+
"id": "@uuid@",
30+
"uuid": "@uuid@",
31+
"linkType": null,
32+
"publishedState": true,
33+
"published": "@string@.isDateTime()",
34+
"title": "Test 1A",
35+
"locale": "de",
36+
"webspaceKey": "sulu_io",
37+
"template": "default",
38+
"parent": "@null@||@uuid@",
39+
"url": "/test-1a",
40+
"urls": {
41+
"de": "/test-1a"
42+
},
43+
"lastModified": null,
44+
"author": null,
45+
"authored": "@string@.isDateTime()",
46+
"changer": null,
47+
"changed": "@string@.isDateTime()",
48+
"creator": null,
49+
"created": "@string@.isDateTime()",
50+
"order": "@integer@",
51+
"children": []
52+
},
53+
{
54+
"id": "@uuid@",
55+
"uuid": "@uuid@",
56+
"linkType": null,
57+
"publishedState": true,
58+
"published": "@string@.isDateTime()",
59+
"title": "Test 1B",
60+
"locale": "de",
61+
"webspaceKey": "sulu_io",
62+
"template": "default",
63+
"parent": "@null@||@uuid@",
64+
"url": "/test-1b",
65+
"urls": {
66+
"de": "/test-1b"
67+
},
68+
"lastModified": null,
69+
"author": null,
70+
"authored": "@string@.isDateTime()",
71+
"changer": null,
72+
"changed": "@string@.isDateTime()",
73+
"creator": null,
74+
"created": "@string@.isDateTime()",
75+
"order": "@integer@",
76+
"children": []
77+
}
78+
]
2879
},
2980
{
3081
"id": "@uuid@",

Tests/Functional/Controller/responses/navigation__get_excerpt.json

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,76 @@
3333
"icon": null,
3434
"image": null
3535
},
36-
"children": []
36+
"children": [
37+
{
38+
"id": "@uuid@",
39+
"uuid": "@uuid@",
40+
"linkType": null,
41+
"publishedState": true,
42+
"published": "@string@.isDateTime()",
43+
"title": "Test 1A",
44+
"locale": "de",
45+
"webspaceKey": "sulu_io",
46+
"template": "default",
47+
"parent": "@null@||@uuid@",
48+
"url": "/test-1a",
49+
"urls": {
50+
"de": "/test-1a"
51+
},
52+
"lastModified": null,
53+
"author": null,
54+
"authored": "@string@.isDateTime()",
55+
"changer": null,
56+
"changed": "@string@.isDateTime()",
57+
"creator": null,
58+
"created": "@string@.isDateTime()",
59+
"order": "@integer@",
60+
"excerpt": {
61+
"title": "",
62+
"more": "",
63+
"description": "",
64+
"categories": [],
65+
"tags": [],
66+
"icon": null,
67+
"image": null
68+
},
69+
"children": []
70+
},
71+
{
72+
"id": "@uuid@",
73+
"uuid": "@uuid@",
74+
"linkType": null,
75+
"publishedState": true,
76+
"published": "@string@.isDateTime()",
77+
"title": "Test 1B",
78+
"locale": "de",
79+
"webspaceKey": "sulu_io",
80+
"template": "default",
81+
"parent": "@null@||@uuid@",
82+
"url": "/test-1b",
83+
"urls": {
84+
"de": "/test-1b"
85+
},
86+
"lastModified": null,
87+
"author": null,
88+
"authored": "@string@.isDateTime()",
89+
"changer": null,
90+
"changed": "@string@.isDateTime()",
91+
"creator": null,
92+
"created": "@string@.isDateTime()",
93+
"order": "@integer@",
94+
"excerpt": {
95+
"title": "",
96+
"more": "",
97+
"description": "",
98+
"categories": [],
99+
"tags": [],
100+
"icon": null,
101+
"image": null
102+
},
103+
"children": []
104+
}
105+
]
37106
},
38107
{
39108
"id": "@uuid@",

Tests/Traits/CreateSnippetTrait.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Sulu\Bundle\HeadlessBundle\Tests\Traits;
1515

1616
use Doctrine\ORM\EntityManagerInterface;
17+
use Sulu\Bundle\TagBundle\Tag\TagManagerInterface;
1718
use Sulu\Content\Domain\Model\WorkflowInterface;
1819
use Sulu\Messenger\Infrastructure\Symfony\Messenger\FlushMiddleware\EnableFlushStamp;
1920
use Sulu\Snippet\Application\Message\ApplyWorkflowTransitionSnippetMessage;
@@ -60,7 +61,13 @@ protected static function createSnippet(
6061
if (isset($data['excerpt']) && \is_array($data['excerpt'])) {
6162
$excerptData = $data['excerpt'];
6263
if (isset($excerptData['tags']) && \is_array($excerptData['tags'])) {
63-
$snippetData['excerptTags'] = $excerptData['tags'];
64+
$tagManager = static::getContainer()->get(TagManagerInterface::class);
65+
$tagIds = [];
66+
foreach ($excerptData['tags'] as $tagName) {
67+
$tag = $tagManager->findByName($tagName) ?? $tagManager->save(['name' => $tagName]);
68+
$tagIds[] = $tag->getId();
69+
}
70+
$snippetData['excerptTags'] = $tagIds;
6471
}
6572
if (isset($excerptData['categories']) && \is_array($excerptData['categories'])) {
6673
$snippetData['excerptCategories'] = $excerptData['categories'];

0 commit comments

Comments
 (0)