diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityUrl.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityUrl.php index dda25c1b6..8ae6dc52b 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityUrl.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityUrl.php @@ -18,6 +18,16 @@ * consumes = { * "entity" = @ContextDefinition("entity", * label = @Translation("Entity") + * ), + * "rel" = @ContextDefinition("any", + * label = @Translation("Relationship type"), + * description = @Translation("The relationship type, e.g. canonical"), + * required = FALSE + * ), + * "options" = @ContextDefinition("any", + * label = @Translation("URL Options"), + * description = @Translation("Options to pass to the toUrl call"), + * required = FALSE * ) * } * ) @@ -28,13 +38,18 @@ class EntityUrl extends DataProducerPluginBase { * Resolver. * * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to create a canonical URL for. + * @param string|null $rel + * The link relationship type, for example: canonical or edit-form. + * @param array|null $options + * The options to provided to the URL generator. * * @return \Drupal\Core\Url * * @throws \Drupal\Core\Entity\EntityMalformedException */ - public function resolve(EntityInterface $entity) { - return $entity->toUrl(); + public function resolve(EntityInterface $entity, ?string $rel, ?array $options) { + return $entity->toUrl($rel ?? 'canonical', $options ?? []); } } diff --git a/tests/src/Kernel/DataProducer/EntityTest.php b/tests/src/Kernel/DataProducer/EntityTest.php index 7edd596f6..5d9d053ca 100644 --- a/tests/src/Kernel/DataProducer/EntityTest.php +++ b/tests/src/Kernel/DataProducer/EntityTest.php @@ -286,6 +286,26 @@ public function testResolveUrl(): void { ])); } + /** + * @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityUrl::resolve + */ + public function testResolveAbsoluteUrl(): void { + $url = $this->getMockBuilder(Url::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->entity->expects($this->once()) + ->method('toUrl') + ->with('canonical', ['absolute' => TRUE]) + ->willReturn($url); + + $this->assertEquals($url, $this->executeDataProducer('entity_url', [ + 'entity' => $this->entity, + 'rel' => 'canonical', + 'options' => ['absolute' => TRUE], + ])); + } + /** * @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityUuid::resolve */