Skip to content

Conversation

rollki
Copy link

@rollki rollki commented Jul 12, 2021

Fixes #1219

Copy link
Contributor

@Kingdutch Kingdutch left a comment

Choose a reason for hiding this comment

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

Big fan of this change but lets make it slightly more generic so we don't end up adding individual attributes for all the possible options. If more dynamic options are needed based on user input then composition can be used to create the options array.

The change in the test shows that this allows the same behaviour but provides us more flexibility.

Comment on lines 22 to 25
* "absolute" = @ContextDefinition("boolean",
* label = @Translation("Make absolute"),
* required = FALSE,
* default_value = FALSE
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* "absolute" = @ContextDefinition("boolean",
* label = @Translation("Make absolute"),
* required = FALSE,
* default_value = FALSE
* "absolute" = @ContextDefinition("array",
* label = @Translation("URL Options"),
* description = @Translation("Options to pass to the toUrl call"),
* required = FALSE,
* default_value = []

Copy link
Author

Choose a reason for hiding this comment

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

 *     "options" = @ContextDefinition("any",
 *       label = @Translation("URL Options"),
 *       description = @Translation("Options to pass to the toUrl call"),
 *       required = FALSE

Changed.

Comment on lines 44 to 45
public function resolve(EntityInterface $entity, bool $absolute = NULL) {
return $entity->toUrl('canonical', ['absolute' => $absolute ?? FALSE]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public function resolve(EntityInterface $entity, bool $absolute = NULL) {
return $entity->toUrl('canonical', ['absolute' => $absolute ?? FALSE]);
public function resolve(EntityInterface $entity, array $options = []) {
return $entity->toUrl('canonical', $options);

Copy link
Author

Choose a reason for hiding this comment

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

public function resolve(EntityInterface $entity, ?array $options) {
    return $entity->toUrl('canonical', $options ?? []);

Changed.

Comment on lines 36 to 38
* The entity to get URL.
* @param bool|null $absolute
* Make the URL absolute.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* The entity to get URL.
* @param bool|null $absolute
* Make the URL absolute.
* The entity to create a canonical URL for.
* @param array $options
* The options to provide to the URL generator.

Copy link
Author

Choose a reason for hiding this comment

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

Changed


$this->assertEquals($url, $this->executeDataProducer('entity_url', [
'entity' => $this->entity,
'absolute' => TRUE,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'absolute' => TRUE,
'options' => ['absolute' => TRUE],

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@rollki
Copy link
Author

rollki commented Jul 15, 2021

@Kingdutch
Many thanks for your review!
That's funny because my first sought was to add the ability to pass options instead of one option, but I chose only absolute and it was wrong, anyway I did all changes after review.
I used any type for ContextDefinition since there's no array type and allow null and change to:
return $entity->toUrl('canonical', $options ?? []);

@Kingdutch
Copy link
Contributor

Oh whelp. Too many things going on at once I suppose. Perhaps there was a good reason? I can’t find where we discussed it. 🤷‍♂️

@rollki
Copy link
Author

rollki commented Jul 19, 2021

I'm not sure we've discussed this, so this approach with options seems more flexible and logical.

@klausi klausi added the 4.x label Aug 16, 2021
Copy link
Contributor

@klausi klausi left a comment

Choose a reason for hiding this comment

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

Thanks, nice work and I think we can do this approach.

I have just one issue to change: we should expose the link relationship as well.

*/
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.

public function resolve(EntityInterface $entity) {
return $entity->toUrl();
public function resolve(EntityInterface $entity, ?array $options) {
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'.

@klausi
Copy link
Contributor

klausi commented Jul 11, 2022

Merged in #1291 . Thanks all!

@klausi klausi closed this Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[V4] Provide the ability to produce "entity_url" as an absolute URL

3 participants