Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 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,11 @@
* consumes = {
* "entity" = @ContextDefinition("entity",
* label = @Translation("Entity")
* ),
* "options" = @ContextDefinition("any",
* label = @Translation("URL Options"),
* description = @Translation("Options to pass to the toUrl call"),
* required = FALSE
* )
* }
* )
Expand All @@ -28,13 +33,16 @@ class EntityUrl extends DataProducerPluginBase {
* Resolver.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to create a canonical URL for.
* @param null|array $options
* The options to provide 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, ?array $options) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We discussed this in #1232 if changing the function signature of a dataproducer would be an API break in the stable version of the module.

But since this is such a simple dataproducer I think it is very unlikely that somebody extended this class, so I think we can do this.

return $entity->toUrl('canonical', $options ?? []);
Copy link
Contributor

Choose a reason for hiding this comment

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

is it a good idea to hard code 'canonical'? I already see the next pull request coming wanting to have a parameter for the link relationship as well. I think we need to make that a parameter as well and default to 'canonical'.

}

}
19 changes: 19 additions & 0 deletions tests/src/Kernel/DataProducer/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,25 @@ 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,
'options' => ['absolute' => TRUE],
]));
}

/**
* @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\EntityUuid::resolve
*/
Expand Down