Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The uri() method is currently ugly to use because it returns an array, so you need a temporary variable everywhere.
We could add the following two functions to help with this:
function entity_url(EntityInterface $entity, $options = array())
function entity_l(EntityInterface $entity, $label = NULL, $options = array())
label would default to $entity->label() when empty.
Alternatively, these could be methods on the class, but we agreed that functions make more sense.
Comment | File | Size | Author |
---|---|---|---|
#43 | entity-l-conversion-1637342-43.patch | 13.49 KB | dcam |
#42 | entity-l-conversion-1637342-42.patch | 13.54 KB | dcam |
#39 | entity-l-conversion-1637342-39.patch | 12.49 KB | penyaskito |
#31 | entity-l-conversion-1637342-31.patch | 14.05 KB | penyaskito |
#35 | entity-l-conversion-1637342-35.patch | 13.55 KB | penyaskito |
Comments
Comment #1
fago+1 for those helpers. Those would be nice methods as well, however I fear to clutter the EntityInterface with lots of unrelated utility methods if we continue like that. That said, let's start doing more helpers/other systems that just take an entity as parameter.
Comment #2
jcisio CreditAttribution: jcisio commentedComment #3
jcisio CreditAttribution: jcisio commentedHere is a patch. I keep the $text argument name in l(). I was wondering about argument order (in l(), the first argument is $text), however, I think here as wrapper functions, the first argument should always be $entity.
Comment #4
Berdir8.x now requires the type for @param and @return, in the case of classes, this should be the fully qualified name, e.g. Drupal\entity\EntityInterface.
Also, let's add a @see for EntityInterface::uri() (again full name) and url()
Same here for the arguments and also @see for l() and EntityInterface::uri().
We might also want to add reverse references and maybe even a sentence to the documentation of EntityInterface::uri() that points you to these new helper functions.
Comment #5
jcisio CreditAttribution: jcisio commentedAddressed #5.
Comment #6
fgmWe may want to leave unexpected results behave "better" instead of failing when they could still provide usable results. That would be a design choice, though (typical PHP vs "fail early"). Otherwise good for me.
As in all occurrences in this patch, URL is normally uppercase in text.
This test does not cover the case of uri() returning some non-NULL value (although it should). Although the default implementation in Entity::uri() always returns array or NULL, inherited implementations could return something else, since the return value is not checked by type hinting. We might want to leave such a result untouched and return it instead of converting it to NULL.
As for entity_url(), we probably want to use (string) $uri if it is a non-NULL scalar anyway.
Comment #7
fagoAlso, it's used nowhere as of now. So I guess at least we should add some basic tests for it.
Comment #8
jcisio CreditAttribution: jcisio commented- Fixed capitalization of URL (anyway, I think we should we should do the same thing for 'uri' too).
- I don't fix the other two, as described in the EntityInterface, ::uri() should return an array or NULL. Also, anything other than an array is unusable.
Comment #9
jcisio CreditAttribution: jcisio commentedIt was a cross post so that the tag was removed. However I don't think tests are needed. They are basically wrapper functions, and we already have tests for Entity::uri().
Comment #10
fagoLet's use isset() here as this is faster (microoptimizations..).
It's passed to l(), so I guess it should be documented as for l().
Same here.
Yes, still we want make sure the basic wrapper works and extending the test to take care of that should be rather simple.
Comment #11
jcisio CreditAttribution: jcisio commentedFixed some errors in #10 and add tests for Entity::uri() and entity_url.
Comment #12
fgmJust one more assertion for entity_l(), maybe ?
Also, to nitpick, variables pointing to user accounts are usually named $account(something) not $user(something)
Comment #13
jcisio CreditAttribution: jcisio commented- If entity_url passes, there is no way entity_l won't pass. They are pratically the same and the behavior of url() and l() are well known.
- In tests, in most cases, $userN are used instead of $account for new created accounts.
Comment #14
fagoLet's better test the right string is returned, i.e. url(...). Just having a string doesn't make a right URL.
entity_url() doesn't invoke entity_l() so I can't follow the reasoning. Let's make sure we have complete tests for both.
Comment #15
jcisio CreditAttribution: jcisio commentedAs I commented in the test, url is managed by the entity controller, we don't know it. So I don't know how to write a better test, unless I copy the code in entity_url into the test, then compare the result with entity_url's, which, IMO, is not very useful.
Both entity_url() and entity_l() use the same logic: they use EntityInterface::uri() output and send to url() and l(). The test is basically uri() test. So if one passes, the other should pass (could you give a counterexample?).
That said, I think we could add another test: assert if entity_l() result has the format of
<a[.+?]>THE-GIVEN-TEXT</a>
. But I'm not sure it is useful. It'd better add tests for l() and url().Comment #16
fagoJust make use of url() for the test, i.e. compare url('user/X') with entity_url($user), so we verify the result is as expected.
That holds assuming that both functions are written the same. The tests are there to verify both work independent of that assumption. So, again just add a simple assertion that makes sure l($entity->label(), url("user/1")) equals the output of entity_l().
Comment #17
jcisio CreditAttribution: jcisio commentedHow can we assume that entity_url($user) would return url('user/X')? Only the user module knows that, and there is already a test for that in the user module.
I know that simpletest is not unit test, but...
I leave this issue for more suggestion.
Comment #18
BerdirExtended the tests, we don't need to know what it should return, but we do know that it needs to be the same as calling url() or l() ourself.
Comment #19
ygerasimov CreditAttribution: ygerasimov commentedLets enhance the test to check custom title and link options.
Comment #20
corvus_ch CreditAttribution: corvus_ch commentedLooking good.
Comment #21
catchHmm I'm not really sure between the procedural helpers vs. class methods but yeah it doesn't seem useful to have these in the main interface at least.
Doesn't have to happen here, but there must be somewhere we can use this on core already? Could we include one conversion in the patch then add a novice follow-up to do the rest?
Comment #22
BerdirHere we go. I think I actually converted most usages now that already use uri(), I guess we have more that are still using hardcoded links and so on. And there are quite a few that do need additional arguments.
Nicely shows that these functions are useful and help to simplify code I think. Let's see if I didn't mess anything up.
Comment #24
BerdirMissed one.
Comment #25
corvus_ch CreditAttribution: corvus_ch commentedDrupal\user\RegisterFormController::save() can profit from the new functions. For this is the only place I could find, I suggest that we fix this in this patch.
Comment #26
BerdirOk, fixed that one. As said before, I assume there are still some hardcoded links around, I suggest we check and tackle that in a follow-up.
Comment #28
BerdirFixed the other two cases that used $uri.
Comment #29
corvus_ch CreditAttribution: corvus_ch commentedThis one's looking good again.
Comment #30
fagoYep, looks great!
Comment #31
penyaskito[edited]
The conflict was because of two
@see
phpdoc tourl()
in EntityInterface that have been moved to StorableInterface.Comment #33
penyaskito#31: entity-l-conversion-1637342-31.patch queued for re-testing.
Comment #35
penyaskitoI were wrong in #31, editing comment for not confusing anyone else.
Comment #36
penyaskitoComment #37
penyaskitoThis doesn't apply anymore :/
Needs rerolling again.
Comment #38
penyaskitoRerolled again.
Comment #39
penyaskitooops!
Comment #40
penyaskitoComment #41
BerdirOne of the re-rolls seems to have lost two conversions in the node and term preprocess functions:
Comment #42
dcam CreditAttribution: dcam commentedRerolled #35 with the missing changes to the preprocess functions shown in #41.
Comment #43
dcam CreditAttribution: dcam commentedRerolled #42 due to recent entity system changes.
Comment #44
Lars Toomre CreditAttribution: Lars Toomre commentedIf this patch gets re-rolled again, can the following be added as well to clean up the docs?
Can we add @param type hint here? Also explanation should start with '(optional)' as well as in the description for $options variable.
Comment #45
Berdir#43: entity-l-conversion-1637342-43.patch queued for re-testing.
Comment #47
andypostThere's contrib module http://drupal.org/project/entity_path
Comment #48
BerdirI think we can close this one, now that we have $entity->url().