Closed (duplicate)
Project:
Drupal core
Version:
8.0.x-dev
Component:
entity system
Priority:
Normal
Category:
Feature request
Assigned:
Issue tags:
Reporter:
Created:
15 Jun 2012 at 11:39 UTC
Updated:
29 Jul 2014 at 20:47 UTC
Jump to comment: Most recent, Most recent file
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 commentedComment #3
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 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 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 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 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 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 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 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 commentedLets enhance the test to check custom title and link options.
Comment #20
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 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 commentedThis one's looking good again.
Comment #30
fagoYep, looks great!
Comment #31
penyaskito[edited]
The conflict was because of two
@seephpdoc 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 commentedRerolled #35 with the missing changes to the preprocess functions shown in #41.
Comment #43
dcam commentedRerolled #42 due to recent entity system changes.
Comment #44
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().