Skip to content

Commit 1134c1d

Browse files
authored
feat(EntityUrl): Extend entity url dataproducter with absolute option (#1291)
1 parent 122e29c commit 1134c1d

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/Plugin/GraphQL/DataProducer/Entity/EntityUrl.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
* consumes = {
1919
* "entity" = @ContextDefinition("entity",
2020
* label = @Translation("Entity")
21+
* ),
22+
* "rel" = @ContextDefinition("any",
23+
* label = @Translation("Relationship type"),
24+
* description = @Translation("The relationship type, e.g. canonical"),
25+
* required = FALSE
26+
* ),
27+
* "options" = @ContextDefinition("any",
28+
* label = @Translation("URL Options"),
29+
* description = @Translation("Options to pass to the toUrl call"),
30+
* required = FALSE
2131
* )
2232
* }
2333
* )
@@ -28,13 +38,18 @@ class EntityUrl extends DataProducerPluginBase {
2838
* Resolver.
2939
*
3040
* @param \Drupal\Core\Entity\EntityInterface $entity
41+
* The entity to create a canonical URL for.
42+
* @param string|null $rel
43+
* The link relationship type, for example: canonical or edit-form.
44+
* @param array|null $options
45+
* The options to provided to the URL generator.
3146
*
3247
* @return \Drupal\Core\Url
3348
*
3449
* @throws \Drupal\Core\Entity\EntityMalformedException
3550
*/
36-
public function resolve(EntityInterface $entity) {
37-
return $entity->toUrl();
51+
public function resolve(EntityInterface $entity, ?string $rel, ?array $options) {
52+
return $entity->toUrl($rel ?? 'canonical', $options ?? []);
3853
}
3954

4055
}

tests/src/Kernel/DataProducer/EntityTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ public function testResolveUrl(): void {
286286
]));
287287
}
288288

289+
/**
290+
* @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityUrl::resolve
291+
*/
292+
public function testResolveAbsoluteUrl(): void {
293+
$url = $this->getMockBuilder(Url::class)
294+
->disableOriginalConstructor()
295+
->getMock();
296+
297+
$this->entity->expects($this->once())
298+
->method('toUrl')
299+
->with('canonical', ['absolute' => TRUE])
300+
->willReturn($url);
301+
302+
$this->assertEquals($url, $this->executeDataProducer('entity_url', [
303+
'entity' => $this->entity,
304+
'rel' => 'canonical',
305+
'options' => ['absolute' => TRUE],
306+
]));
307+
}
308+
289309
/**
290310
* @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityUuid::resolve
291311
*/

0 commit comments

Comments
 (0)