Problem/Motivation
Node and comment links (Read more, 1 comment, etc) are always rendered. They can be hidden in the cases of an entity reference field or a view, but not for a normal render (e.g. the teasers on default front page). This is a problem when we want to hide links, e.g. in entity_embed.
Proposed resolution
Make the links an extra field. Thus it shows up in the view display settings. Only render if it is set to Visible.
Remaining tasks
- Write tests.
User interface changes
Links show up as an element in view display settings. It can there be set to Hidden which hides it from output.
As the theme templates still output the links aside, changing the weight of the elements in the view display settings will not have an effect on the output.
API changes
links
is removed as a setting in EntityReferenceFormatter and in Views, but added as an extra display field.
Original report by Dave Reid
I want to make an 'Embed' view mode for the entity_embed module, but I currently have to hard-code removing the links from a rendered entity by doing:
// Build the rendered entity.
$build = entity_view($entity, $view_mode, $langcode);
// Hide entity links by default.
// @todo Make this configurable via data attribute?
if (isset($build['links'])) {
$build['links']['#access'] = FALSE;
}
$output = drupal_render($build);
The entityreference field formatter also has a configurable option for links. I think this functionality would be better exposed as a 'Links' item on the 'Manage Display' tab for entities. This may be a little complex because currently node entities want to render links completely separate from the "content" items. But we lose flexibility in doing so.
Comment | File | Size | Author |
---|---|---|---|
#73 | node-comment-links-2271349.73.patch | 24.37 KB | Berdir |
#53 | node-comment-links-2271349.51.patch | 24.44 KB | larowlan |
Comments
Comment #1
Dave ReidHere's a quick stab at it that would make the links respect if they've been set to 'Hidden' or 'Visible' for nodes. Ordering the links around does not affect display though.
Comment #2
larowlanWe're hoping to move comment links to a formatter. But plus one for general approach.
Comment #3
Wim LeersIf you can also apply this to Comment's links, then I think this patch is good to go. #2 sounds great, but is further away. This is simple and at least ensures sanity, for e.g. #2274975: Display/hide links on Rendered Entity formatter very broken.
Please push this forward :)
Comment #4
Wim LeersMore accurate title.
Comment #5
BerdirBased on profiling that I did today, if you build a view of nodes without links, then we currently spend ~10% of the page request rendering/processing links that we then don't display, which is stupid.
Also, it would be a nice and clean solution for the broken entity reference links setting which has multiple issues open right now: #2275667: Reference field display links option ignored and #2274975: Display/hide links on Rendered Entity formatter very broken.
Apart from adding it for comments as well, there's problem that right now, this would allow to move the component around on the page which is a lie because the location is hardcoded right now. We might get away with a description that says "If enabled, will always be shown at the bottom" or something :p
Also, with this, the separate settings in entity reference and views would be bogus and we could possibly just throw them out here?
Comment #6
Wim LeersTo clarify: this happens only when you hide (don't print) the links in the theme layer.
Yes!
Comment #7
blueminds CreditAttribution: blueminds commentedRemoved the setting from reference field formatter, added real entity id as the array key. Testbot will not like it as I have not touched the tests yet.
Comment #9
blueminds CreditAttribution: blueminds commented- Removed the "Display links" option from views settings
- Fixed my wrong assumption from NodeViewBuilder::buildComponents()
Comment #11
Wim LeersYou still need to modify
Node::baseFieldDefinitions()
:) (AndComment::baseFieldDefinitions()
also.)Comment #12
BerdirNope, *extra* fields have nothing to do with base field definitions, they're extra.
The main problem that's remaining is that it doesn't allow you to set the weight/order. And I see two solutions for that:
- Add the description as mentioned above, like "This will only affect if the links are shown or not and not their position."
- Kill the manual override of the links in node templates, the main difference would be that it would be inside the content diff and no longer below it. We should also make sure that by default, the links stay last when you add new fields and so on.
Comment #13
Wim LeersRight! :(
Sorry for the misdirection!
Comment #14
blueminds CreditAttribution: blueminds commentedJust a theoretical question - does it matter that much to have the links at the bottom at any case? Why it cannot behave as any other display field. We display them with weight 100 by default, and let's leave it for the user to decide where to keep the links.
Comment #15
BerdirThat's exactly the question, and I can't answer it.. the problem is the extra < div > that's around everything else but not links right now. So we need some frontend people to chime in and say if they see a problem here...
Comment #16
BerdirAnother very interesting side effect that this is is that comments are now rendered as a field too, so they are *above* the links, and there is no way to change that without manually changing the template.
This issue would allow us to move the links above the comment field again. So it's actually not just "make sure it's always last"...
Comment #17
andypostThere's a 2 related issues:
1) removes "extra div" (#15) #1962846: Use field instance name for header of comment list, drop comment-wrapper template
2) general approach for out-of-bound components #1875974: Abstract 'component type' specific code out of EntityDisplay
Comment #18
slashrsm CreditAttribution: slashrsm commented+1 to make it a field. Links are strictly already rendered inside content
Comment #19
BerdirYeah, I was mostly worried about the UI problem that the links will be there but by default (in bartik) won't respect the weight only hidden or not.
But as you said, this is the same already for comments, so I don't think this is blocking anything after all :)
Re-rolled and updated default views. Still needs tests.
Comment #21
andypostwrong merge
Comment #22
blueminds CreditAttribution: blueminds commentedFixed merge, working on tests.
Comment #24
ArlaRerolled last patch to current 8.0.x.
Comment #26
BerdirApplied the same pattern to comments, updating default views, thought I did that already.
Comment #28
BerdirUps, copy & paste fail.
Comment #30
BerdirMuch fail. such stupid.
Comment #31
slashrsm CreditAttribution: slashrsm commentedLooks good to me.
Comment #32
alexpottI think we still need tests? No?
Comment #33
andypostA tests to turn on/off the components makes sense, otoh some tests form comments are going to change to phpunit in #2318251: Make comment links functionality testable and convert CommentLinkTest to PHPUnit
Comment #34
ArlaApplied template to summary.
Comment #35
jhodgdonAn excellent way to test if this works would be to update the default config for the Search-related view modes so that they do not show the comment links, and then take out the kludgy code in search-related code that was trying to hide these links... I'd hope that this would fix #1113832: [PP-1] Comment module renders "reply" and other links in search index/results, which has a "steps to reproduce" which could probably be turned into a test.
Comment #36
jhodgdonI should at least see if this patch is sufficient for the Search use case...
Comment #37
jhodgdonNever mind... berdir pinged me in IRC...
So for this to work for the Search use case, we would need to be able to specify, on a Node view mode, what view mode for Comments to use when building the Node page.
Apparently this patch doesn't allow for that. Which seems like kind of a hole in the logic... but anyway this patch won't directly help us with the Search problem. Yet.
Comment #38
ArlaAdded tests, and in the process found and removed some left-behind code around the removed views option. With help from @Berdir.
Comment #39
ArlaAlso created a draft change notice at https://www.drupal.org/node/2324707
Comment #40
Berdirtrailing whitespace here. And there should be an empty line between the closing } of the method and class.
Comment #42
jhodgdonArla: when you create a change notice, you need to reference the issue that it is for... You probably did this but I think it maybe gets lost when you preview the change node.
Comment #43
jhodgdonOh also the project on that change notice is set to "Node" and not "Drupal Core".
Comment #44
ArlaThanks @jhodgdon, corrected it! Working on tests. The CommentLinksTest passes on my system, though...
Comment #45
jhodgdonThanks Arla! It looks like 3 tests are failing... not just CommentLinksTest.
Comment #46
ArlaUpdated the tests.
Comment #47
Arla@Berdir showed me how those tests can be changed more reasonably (and cleaner!), so here's another update. In RowPluginTest, we now just don't check whether links are output, because it's no longer a feature of Views.
The interdiff is relative to #38 because that is clearer.
Comment #50
larowlanThis tests passes locally for me - retesting
Comment #53
larowlanjust a thought - probably off - but maybe something to do with testbot using a sub-dir?
Comment #54
ArlaWhen I run locally with run-tests.sh I get 6 fails. I'm not sure if I'm doing it right?
php core/scripts/run-tests.sh --class --verbose Drupal\\comment\\Tests\\CommentLinksTest
and the fails are:
Comment #55
larowlanGreen :)
I use phing or the UI to run my tests.
See build.XML in github.com/nickschuch/vd8 for the commands phing uses
Comment #56
ArlaNice.
We removed all the
links: true|false
from views defintions. Now links are showing in the default feed and the taxonomy feed (as<comments>http://d8/node/1#comments</comments>
). Do we want to make them hidden by default again? Or, since there was no test for it, is the behaviour undefined? (Should it be?)Comment #57
BerdirYes, I guess we should hide them there and provide the configuration for that, and some tests would be great.
Comment #58
andypost@Arla there's 2 issues with comments #2113323: Rename Comment::permalink() to not be ambiguous with ::uri() & #2010202: Deprecate comment_uri()
Comment #59
ArlaIt seems that the Display links option on RSS rows did not have any effect before this patch. The tests (CommentRssTest and RssTest) test only one behaviour – ironically enough that links are displayed when Display links is set to false. To test, checkout 8.0.x and visit
rss.xml
, then change the Frontpage view and check Display links, (rebuild cache,) visitrss.xml
, and note that the output is identical to before.The problem is that node\Plugin\views\row\Rss (and probably comment\Plugin\views\row\Rss) handle the links in the wrong way. Rather than fixing that separately, I guess we should just extend the tests and implement within this issue.
Comment #60
ArlaDid some exploring with @Berdir about node links in RSS. hook_node_link_alter() is implemented by comment, book and statistics. They all do a hard-coded check to make exceptions for
$view_mode == 'rss'
. Book and statistics simply don't output anything for 'rss', comment does it by adding a<comments>
element.<comments>
element is then quite far from being a node link. For example it will not be affected by the weight of the Links field. The module merely utilises the hook to decide whether to add the element or not. Should the element be output by another, more logically chosen mechanism?Comment #61
larowlan1) now that comment is a field, could we move comment rss output to a formatter and make that the formatter for the rss view mode.
We could use hook_entity_view_display_alter to force this formatter, just like node does some brute-forcing of display settings for search view mode. If this is possible, we could split that into a separate issue.
2) yeah I think we're stuck with the checks
Comment #62
larowlan2) We could split each into a distinct component links__comment links__book links__statistics etc - would require some sort of discovery mechanism of course - ie the notion of EntityLinkPlugins
Comment #63
larowlanSo this is 'in an ideal world solution' from my point of view
Then in (eg) node_entity_extra_field_info (note psuedo code, untested):
Then again in NodeViewBuilder::buildLinks() we use the plugin manager again, this time to call EntityLinkPluginInterface::buildLinks.
Then we remove hook_node_links, hook_node_links_alter etc in favour of plugins.
Comment #64
larowlanactually, we'd retain the alter hook, but for actual alters, not for 'adding'
Comment #65
Berdir1) I've thought about a formatter too, I'd suggest we create a follow-up issue for that. Then we simply don't touch that here and it is still displayed as long as you have the links extra field set to visible.
2) Yeah, maybe, but that's IMHO going much further than we want to go in this issue and it has problems on it's own.. having that IMHO doesn't make much sense without having some notiion of grouping (which would be great to have but we don't), I don't know what would happen if you decide to move them around and so on.
IMHo, on those links on RSS, it's either way not a regression compared to what we have now. If we keep the hiding, then they will simply never be displayed on the rss output, just like now. And if we remove that logic, then we can still configure it so that they don't show up by default, just like now.
Comment #66
andypostThere's issue already, added to related #1920044: Move comment field settings that relate to rendering to formatter options
The same troubles are described in #1113832: [PP-1] Comment module renders "reply" and other links in search index/results
Comment #67
andypostThe main trouble here that we can't use disabled view mode (rss, search*) to render entity #2324719-2: Node indexing - should use view mode for comments, not hook
Comment #68
jhodgdon#67/andypost: So maybe we need to fix that, so that you can use a disabled view mode to render an entity, and it will fall back? Why will it not do that anyway, and shouldn't it? It seems like something we need.
Comment #69
BerdirI don't understand the last few comments, how is that related to this issue? I don't know what you mean with disabled view modes, the only thing that exists are view modes that have no corresponding display config entity, which means that it falls back to the default. Which is perfectly OK.
IMHO, the current patch is a large step forward and can be committed like this. Changing the comment behavior can be a follow-up and I'm not completey sure that a formatter is the right step, because it could be something that's additional, what if you want to display that tag *and* the comments in an rss feed?
Same for the second point, it's not optimal that those links are still hardcoded and hidden on the rss view mode, but that's the same as it is now.
Comment #70
andypost@Berdir yep, that's out of scope, take a look at #2324719-2: Node indexing - should use view mode for comments, not hook - that means disabled entity displays for view modes
Comment #71
slashrsm CreditAttribution: slashrsm commentedRTBC then?
Comment #73
BerdirRe-roll, context conflict in EntityReferenceEntityFormatter.
Comment #76
Dries CreditAttribution: Dries commentedCommitted to 8.0.x.
I think we need to tidy up our definition of a "task". My personal opinion is that it is a feature because it actually changes the behavior and affects contributed modules. Applying a pattern to convert something from the old way to the new way can be a feature -- and isn't always a task. At what point in the release cycle do we stop applying patterns?
Comment #77
BerdirGreat!
On task/feature, agreed, I made this a (major) task for three reasons:
a) It solves known bugs in entity_reference, I just marked #2274975: Display/hide links on Rendered Entity formatter very broken as a duplicate
b) It unifies two different features into one, so now it works the same way in views and entity references, and also everywhere else where entities are displayed.
b) It's also performance relevant, if you used that checkbox in a view of nodes for example, then it still built them (including post render callbacks as they are personal), now we save this. Based on my testing, on overview pages with a lot of nodes without links, that was ~10% request or so.
Also published the change record.
Comment #79
Gábor HojtsyFix media tag.