Skip to content
19 changes: 17 additions & 2 deletions src/Plugin/GraphQL/DataProducer/Entity/EntityUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
* consumes = {
* "entity" = @ContextDefinition("entity",
* label = @Translation("Entity")
* ),
* "rel" = @ContextDefinition("any",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data type should be string

* 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
* )
* }
* )
Expand All @@ -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 ?? []);
}

}
20 changes: 20 additions & 0 deletions tests/src/Kernel/DataProducer/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down