Problem/Motivation
Drupal 8 wants to provide a great REST experience out of the box.
Yet /node/1?_format=json only gives you:
{
"status": "2",
"cid": "8",
"last_comment_timestamp": "1454237193",
"last_comment_name": "",
"last_comment_uid": "1",
"comment_count": "6"
}
… that's only one comment ID (cid) there. And it's the one for the last comment. How do you get the five other comments?
Presumably this is because it's not the Node entities that point to Comment entities, it's Comment entities that point to Node entities. Hence it makes sense that a Node entity doesn't list all the Comment entities' IDs. But that makes it not less frustrating for front-end developers, who not necessarily have deep Drupal knowledge.
The answer?
Create a "REST export" view.
That's not an API-first Drupal. This makes it so that you cannot even get such basic content without having deep Drupal knowledge!
Proposed resolution
Provide a "REST export" view at /node/{node}/comments that returns all published comments for that node. Ship this view by default.
Alternative solutions include:
- allow referenced entity collections to be embedded in HAL+JSON responses: #2100637: REST views: add special handling for collections
- native http://jsonapi.org/support
- native http://graphql.org/ support
2 and 3 are not practical. 1 we should work on, but because nodes don't reference comments, it seems implausible that we'd be able to do this, unless we perhaps provide a custom comment field normalizer.
Remaining tasks
- Agree on solution.
- Build solution.
- Review solution.
- Commit solution.
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | REST_node_comments_view-2662010-7.patch | 6.74 KB | wim leers |
Comments
Comment #2
dawehnerMaybe comment should ship with a view by default?
Comment #3
wim leersFrom the IS:
So… +1 :)
Comment #4
dawehnerOh yeah I was more referring to your choice of using rest module as component.
Comment #5
wim leersI'd say this would ship as optional configuration with REST module, no? i.e.
core/modules/rest/config/optional/views.view.node_comments_rest.ymlComment #6
wim leersIn fact, let's just create a patch.
Comment #7
wim leersOh hm, I see your point. Never mind me.
Comment #10
dawehnerI guess we want to disable it by default so sites don't accidentally leak data?
Nice!
What kind of access logic do we actually expect? People with access to the parent entity should have access to the individual comments?
Any reason to use a full pager, not a mini one?
Really fields? I would just expose the entire comment.
If you use the entire comment you also don't have issues with not displaying the data for the parent entity. It would be just included all the time.
Comment #11
Crell commentedComments are not unique to nodes anymore, so this should be done in a not-node-specific way. Also, to be properly RESTy we should have a link from the entity to its comment resource. Which means auto-generating the route information using a route provider.
How that fits in with using a View for the data itself... I'm not actually sure off hand. :-/
Comment #12
larowlanComment #13
larowlanThere is the possibility of multiple comment threads too, so lets not limit ourself to a single field either
Comment #14
wim leers#10
rest.settings.yml… but that ship has sailed anyway, so +1#11: Agreed on all counts. Except I don't know how to do any of that.
#13: True again, but also no idea how to do that either.
Help very much welcomed!
Comment #15
wim leers@Crell: would #2100637: REST views: add special handling for collections also be able to solve this for us?
Comment #16
larowlanAh, so if this runs off Comment entities - if we change the uri to
comments/{entity_type_id}/{entity_id}and add an argument that filters using the entity_type_id column/field on Comments, it should be generic enough to support all entity-types. Then we'd just need a new link-template in the commented entity-type » something like 'comment-thread'. This could be done in comment_entity_type_alter by looping over comment-types and collecting all of the target entity types - Something like this:We could also accept an optional field-name to make it extra useful
comments/{entity_type_id}/{entity_id}/{field_name}?Then when we generate the hal representation of a node with commenting enabled, we could add a link to the 'comment-thread' using the link template.
Thoughts?
Comment #17
wim leersSounds great! Just one problem: what if there are multiple comment types on the same entity?
Comment #18
larowlanThat's where the optional field name will come in, but could easily be the comment-type too - both are base fields on Comment entity, so should be available as arguments.
Comment #19
wim leersOops. You already covered that, I missed it in my first reading. Alright, let's do it? :)
Comment #22
wim leersDo we still want this?
Comment #24
wim leersSince this is obviously not going to work as simply as I had originally imagined (I was too naïve), and for the reasons given in #2100637-156: REST views: add special handling for collections, I'm going to close this issue.