Problem/Motivation
Drupal 8 shipped with a bunch of functions that were immediately marked as deprecated. I can see the use for some leftovers such as drupal_render() and entity_load_multiple() but I wonder why there were new methods introduced that are also deprecated already.
For instance, if you actively avoid using deprecated functions, you have to write this to generate an entity link:
\Drupal::service('renderer')->render($entity->toLink()->toRenderable()); instead of $entity->link();
Is this really the direction we want to go in? Why can't we keep the link() method as a shorthand for the rendered Link object for example? I'm worried that we will scare off less experienced developers this way or have completely illegible code as a result of having to be so verbose.
Alternatively: is it okay to use deprecated functions to avoid the above verbosity or do you risk having your code broken somewhere throughout the D8 cycle?
Proposed resolution
- Un-deprecate toString() and explain the situations in which it may be used.
- The following is the non-deprecated way of doing it then:
$entity->toLink()->toString()
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | drupal-undeprecate_link_to_string-2628754-20.patch | 1.32 KB | kristiaanvandeneynde |
| #10 | link_tostring_should-2628754-10.patch | 855 bytes | cilefen |
| #10 | interdiff.txt | 923 bytes | cilefen |
| #8 | 2628754-8.patch | 785 bytes | dawehner |
Comments
Comment #2
dawehnerIf you really need to render it to a link, which ideally you would not have to, you could also just do:
$entity->toLink()->toString()Comment #3
kristiaanvandeneyndeI wanted to do that, but toString() is deprecated as well :D
Comment #4
berdirI don't understand why toString() is deprecated, but at that point, it's a link/url system problem, not in entity system :)
Comment #5
bojanz commentedtoString() being deprecated makes no sense, since there's no alternative.
Comment #6
dawehnerRight, it should be just decouraged to be used, as well, on render API level you should just use render arrays.
Comment #7
kristiaanvandeneyndeI know I can and should just use render arrays. I'm highlighting this issue as it's a clean example of how complex some simple tasks have become. If someone asks you to return a link to an entity as a string -regardless the reason- the code you have to write is all but pretty.
Also, I can't tag this as "link system" since it doesn't exist and link.module isn't correct here.
Comment #8
dawehnerSo what about the following statement?
Comment #9
berdirWorks for me. Maybe a native speaker can review and improve the wording?
Updating component and issue title. Also needs an issue summary update.
Comment #10
cilefen commentedI did the native-speaker check. I also moved it to the full function description because appearing after the @return annotation (because it replaced a @deprecated), it appears wrongly to be part of the return documentation.
Comment #11
berdirThanks, just needs an issue summary update now I think. I'd keep the current text in problem description and add a proposed solution for un-deprecating toString().
Comment #12
cilefen commentedComment #13
berdirComment #14
kristiaanvandeneyndeLooks good to me, thanks!
Comment #15
berdirChange makes sense to me. We shouldn't deprecated methods that have no real replacement/acceptable alternative way of doing things.
Comment #16
kristiaanvandeneyndeAgreed, I thought I was losing my mind here :) Generating textual links should not be such a pain.
Comment #17
dawehnerIt would be nice if we have a semantic tag for, this is not wrong, but its suggested to do something else, but for now, there is nothing else.
Comment #18
alexpottDon't we know what that something is? i.e a Link object. Also I think the paragraph could be more clearly written as...
Also an @see to ::toRenderable() should be added.
Comment #19
alexpottComment #20
kristiaanvandeneyndeDone #18
Comment #21
claudiu.cristeaAll points were addressed.
Comment #22
alexpottI removed a superfluous comma I added to the text.
Committed 5bc305c and pushed to 8.0.x and 8.1.x. Thanks!
Comment #26
SlayJay commentedIs there any place setup to discuss the bigger issue?
\Drupal::service('renderer')->render($entity->toLink()->toRenderable());
vs.
$entity->toLink()->toString()
is happening all over the place in core, and I really thing this is a disastrous direction to head in.