Skip to content

Commit 7355098

Browse files
committed
inject entityPath into ContentType
1 parent 375f013 commit 7355098

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

api/config/services.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ services:
8484
App\Serializer\Normalizer\CircularReferenceDetectingHalItemNormalizer:
8585
decorates: 'api_platform.hal.normalizer.item'
8686

87+
App\Serializer\Normalizer\ContentTypeNormalizer:
88+
decorates: 'api_platform.hal.normalizer.item'
89+
arguments:
90+
- '@.inner'
91+
- '@api_platform.route_name_resolver'
92+
8793
App\Serializer\Normalizer\UriTemplateNormalizer:
8894
decorates: 'api_platform.hal.normalizer.entrypoint'
8995

api/src/Entity/ContentType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ContentType extends BaseEntity {
4646
* @ORM\Column(type="text")
4747
*/
4848
#[ApiProperty(writable: false)]
49+
#[Groups(['read'])]
4950
public ?string $entityClass = null;
5051

5152
/**
@@ -56,4 +57,12 @@ class ContentType extends BaseEntity {
5657
*/
5758
#[ApiProperty(writable: false)]
5859
public ?array $jsonConfig = [];
60+
61+
/**
62+
* API endpoint path for creating new entities of type entityClass.
63+
*/
64+
#[Groups(['read'])]
65+
public function getEntityPath(): string {
66+
return ''; // empty here; actual content of this property is filled/decorated in ContentTypeNormalizer
67+
}
5968
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Serializer\Normalizer;
4+
5+
use ApiPlatform\Core\Api\OperationType;
6+
use ApiPlatform\Core\Api\UrlGeneratorInterface;
7+
use ApiPlatform\Core\Bridge\Symfony\Routing\RouteNameResolverInterface;
8+
use App\Entity\ContentType;
9+
use Symfony\Component\Routing\RouterInterface;
10+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
11+
use Symfony\Component\Serializer\SerializerAwareInterface;
12+
use Symfony\Component\Serializer\SerializerInterface;
13+
14+
/**
15+
* ...
16+
*/
17+
class ContentTypeNormalizer implements NormalizerInterface, SerializerAwareInterface {
18+
public function __construct(
19+
private NormalizerInterface $decorated,
20+
private RouteNameResolverInterface $routeNameResolver,
21+
private RouterInterface $router,
22+
) {
23+
}
24+
25+
public function supportsNormalization($data, $format = null) {
26+
return $this->decorated->supportsNormalization($data, $format);
27+
}
28+
29+
public function normalize($object, $format = null, array $context = []) {
30+
$data = $this->decorated->normalize($object, $format, $context);
31+
32+
if ($object instanceof ContentType && isset($data['entityClass'])) {
33+
$data['entityPath'] = $this->router->generate($this->routeNameResolver->getRouteName($data['entityClass'], OperationType::COLLECTION), [], UrlGeneratorInterface::ABS_PATH);
34+
}
35+
36+
return $data;
37+
}
38+
39+
public function setSerializer(SerializerInterface $serializer) {
40+
if ($this->decorated instanceof SerializerAwareInterface) {
41+
$this->decorated->setSerializer($serializer);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)