I am seeing several use cases where it would be nice to have ability to get data (indexed results, for example) as JSON output, but in a block/page display output, which can be rendered freely afterwards via theme alterations or theme templates.
For example, I had several issues where I was sending a SINGLE request to a JSON view and then just render the same View output in various formats via theme templates - as a grid of teasers and a Map of results (in D7). In D7 there's https://www.drupal.org/project/views_datasource Views Datasource module which allows to get JSON output. At the same time, it provides an option to specify how to retrieve the output - with JSON content-type header for D8 REST-like output, or as a standard View rendering on the page (the page will render with all the regions and stuff + JSON-encoded view results).
Is it possible to add the same kind of feature to REST View Display functionality?
Comment | File | Size | Author |
---|---|---|---|
#7 | Screen Shot 2017-03-25 at 6.18.39 PM.png | 738.14 KB | veronicaSeveryn |
#7 | Screen Shot 2017-03-25 at 6.15.21 PM.png | 555.67 KB | veronicaSeveryn |
#4 | Screen Shot 2016-10-19 at 3.40.22 PM.png | 353.58 KB | veronicaSeveryn |
#4 | Screen Shot 2016-10-19 at 3.17.31 PM.png | 343.33 KB | veronicaSeveryn |
#4 | Screen Shot 2016-10-19 at 3.33.15 PM.png | 274.88 KB | veronicaSeveryn |
Comments
Comment #2
Wim LeersThanks for creating this feature request!
Can you clarify what you are requesting by providing an example of what the response you expect would contain exactly?
Comment #3
Wim LeersComment #4
veronicaSeveryn CreditAttribution: veronicaSeveryn commentedI really need this feature for D8 projects.. It might be useful for some other people, too.. I don't see a better way to be able to invoke a View execution just once and be able to output results in several different layouts... But I can't figure out how to possibly "adjust" this REST Plugin so that everyone can be happy :)
I think it's more about the presentation of the response. I'll try to explain.
Let's say I have created a View that pulls some data from DB. My final goal is to re-use the SAME VIEW RESULTS I receive from its execution (instead of creating another View or triggering the same View execution twice), but then on the rendering step present the data the way I want it: create HTML output that looks like
HTML List <UL>
of nodes, for example, and another HTML output that can be presented with a Google Map.For my request, I still need to be able to get the data in a standard JSON format. The way that I get it with REST export is just fine. The problem I have is that REST plugin renders the output of the View with "application/json" header, thus, all we see as a result is a PAGE with JSON. For my use case, I am trying to eliminate execution of the same View several times just to get the same results from DB and render them differently.
So, let's say if RETS Display mode for the view could have that Configuration Option, for example, like "Views Datasource" from D7, then:
Then I will get result like this:
At the same time, "Views Datasource" from D7 provides a template.tpl.php file for the View Display formatting, where it renders result based on the "Views API mode" option selected. And, of course, I can manipulate the JSON result from the view within template_preprocess:
Comment #6
Wim LeersI'm afraid
does not make any sense. Blocks are inherently HTML. Perhaps you want to consume the REST API in a block template, so that you don't have to deal with PHP objects/data, but you can directly interact with the REST API?If so, this very much reminds me of #2852272: Make it possible to call jsonapi from within templates.
This still needs clarifying.
Comment #7
veronicaSeveryn CreditAttribution: veronicaSeveryn commentedI guess it's not really about displaying it as a "block"..
I guess my point is that I want to be able to display JSON data output for the fields configured in a view, but at the same time render this View as a normal page - all other elements should be rendered (header, footer, etc.).
Think about it like a normal PAGE view display - we render view results with a format chosen (HTML lists, rendered entities, table, etc) + all page elements are rendered normally, the same kind of way would be nice to get JSON data, but still display it like a normal site's page presentation.
Here're a few screenshots for example:
Instead of this:
I am looking to be able to also switch to a view like this:
And that's how Drupal 7 Views Datasource module allows to do it on the same View Display, simply by switching configuration "VIEWS API MODE" on/off on a View Display Configuration Form (presented on my very first screenshot for this issue)
Comment #8
Wim LeersI think this needs to be answered by a Views maintainer.
Comment #9
dawehnerI tried to understand the usecase, but can't do yet.
For me this sounds as if this feature is useful for a small amount of users. Given that, at least for me, it feels like this is better solved in contrib space rather than implemented in core for everyone. Do you know whether implementing this in contrib would be hard?
Comment #10
Wim LeersAgreed with #9, this is a pretty rare edge case, that we should not support in Drupal core. Of course, a contrib module for this would totally make sense!