Add option to handle json wrapped in array

Created on 9 September 2018, almost 6 years ago
Updated 3 June 2024, 22 days ago

According to the external_entities documentation, views cannot be used

However we have included a patch in this issue so that in fact yes, views CAN be used.

There are some questions remaining regarding page and limit_per_page:
see 📌 indexing external entities - tracking locks up Needs review

The patch below is required in order to follow the design pattern that was demonstrated at Drupal Europe 5 years ago. Described by these two docuements:
https://www.drupaleurope.org/session/how-cope-external-entities.html
https://www.drupaleurope.org/sites/default/files/slides/2018-09/How%20to...
https://youtu.be/tHGc6AdLzs4

Prior to this patch:

unable to process external entity lookup using the described configuration
(this is maybe unrelated?)

With patch 8 applied, an array option is added


With option "Response for single item is wrapped in array" enabled, external_entities rest endpoint can now be a json generated by "views" from Drupal core as described in the links above.
1) Fixes external entities processing
Maybe unrelated?

Original background information

The response from a Drupal 8 Views REST Export display for a single item is wrapped in an array. The new support for mapped data does not help because the mapping is different for a single item than multiple items.

One solution would be to create a Drupal Views REST storage client but it would be very similar to the basic REST client, and presumably would need to be in another module. Another approach would be to extend the decoders but that seems like a lots of new code for a small issue.

The patch here adds a checkbox to the REST storage client form to indicate that the array wrapper will be removed from the response.

Feature request
Status

Needs review

Component

Code

Created by

🇬🇧United Kingdom polynya

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 9 months ago
    Patch Failed to Apply
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 9 months ago
    1 pass, 2 fail
  • 🇨🇦Canada joseph.olstad

    Straight reroll just to remove fuzz.

  • The last submitted patch, 6: views_rest_support-2988391-06.patch, failed testing. View results
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 9 months ago
    1 pass, 2 fail
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 8.0 & MySQL 5.7
    last update 9 months ago
    1 pass, 2 fail
  • Status changed to Needs work 9 months ago
  • 🇨🇦Canada joseph.olstad

    New patch fixes error.

  • Status changed to Needs review 9 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 9 months ago
    2 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 8.0 & MySQL 5.7
    last update 9 months ago
    2 pass
  • Status changed to RTBC 9 months ago
  • Assigned to joseph.olstad
  • Status changed to Active 9 months ago
  • 🇨🇦Canada joseph.olstad

    Actually, this change seem to resolve the tracking lockup bug.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 9 months ago
    1 pass, 2 fail
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 9 months ago
    1 pass, 2 fail
  • 🇨🇦Canada joseph.olstad

    OOps, here's the new patch.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 9 months ago
    2 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 9 months ago
    2 pass
  • 🇨🇦Canada joseph.olstad

    patch 11 is incorrect.

  • 🇨🇦Canada joseph.olstad

    I was able to get the example working as mentioned in the issue summary, despite the tracking issue

  • Status changed to Needs review 9 months ago
  • Issue was unassigned.
  • 🇨🇦Canada joseph.olstad

    This is working, I had to go through the steps a couple times closely, see the pdf linked in the issue summary. (and also the youtube video is helpful)

  • Status changed to RTBC 6 months ago
  • 🇩🇪Germany a.dmitriiev

    Confirming that patch #8 works.

  • First commit to issue fork.
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 8
    last update 5 months ago
    1 pass, 2 fail
  • Status changed to Fixed 4 months ago
  • 🇳🇱Netherlands pefferen

    With the beta1 version of External Entities I can configure an endpoint of a Drupal 10 exposing nodes as a Rest Views display without patching it.

    I can set up the one as explained in the video with fields, configure A rest endpoint and map the fields via simple mapping.

    I also tried configuring the rendering of the full entity with a Views Rest display (so all fields) then I need to map via the Xpath mapper since the JSON has more layers. Also in this case I get a working external entity overview and detail pages.

    I think that the fix to loadMultiple() in the Rest storage client in #3368304 has made this possible.

    Closing this issue, please reopen if I am wrong :)

  • Assigned to joseph.olstad
  • Status changed to Active 4 months ago
  • 🇨🇦Canada joseph.olstad

    I'll try to review this soon.

  • 🇨🇦Canada joseph.olstad

    Ok reviewed a bit more, the rebuild tracking is timing out.

    I set up a pager , the page variable is:
    page
    the offset variable is:
    offset
    the number of items per page is:
    items_per_page

    I set the view in drupal to 50 items per page
    exposed the option to change it

    enabled full pager

    I'll review this again soon

    📌 indexing external entities - tracking locks up Needs review

    external data source is Drupal 8
    consumer is Drupal 10 external_entities module.

  • 🇨🇦Canada joseph.olstad

    @pefferen,
    please update the documentation relating to "External Entities I can configure an endpoint of a Drupal 10 exposing nodes as a Rest Views display without patching it."

    https://www.drupal.org/docs/8/modules/external-entities/views

  • 🇳🇱Netherlands pefferen

    As far as I see it, external entities cannot use views directly, only by using Search API. The usage of Views as a rest endpoint goes beyond the scope of this module.

    However, having documentation on how to set it up could be very useful.

  • 🇫🇷France guignonv

    As far as I see it, external entities cannot use views directly, only by using Search API. The usage of Views as a rest endpoint goes beyond the scope of this module.

    Well, I have to disagree... :) I've written the xnttviews plugin and it lets me use all my external entities with views. I am using mostly external database sources or files but I have it also working on external entities coming from a REST service. It's not a big module and it is inspired by what search API does. It currently supports filters, contextual filters, sorting and pagination (I even added a "Fuzzy search" filter for my personal needs which works fine if the client supports a 'FUZZY' operator). It has still issues for sure but they can be fixed. Last week I had an issue with pagination when it was set to "display all" but I fixed it. I also noticed a problem with table columns that are not sortable but I'm quite sure I can investigate and fix it as well...

    I think it should be integrated into the External Entity module as part of its core once xnttviews is more mature (needs automated tests, code cleaning/refactoring).

    If you give it a try, please use the dev version as I did not create a new beta release yet with the latest fixes. You definitely should give it a try.

  • 🇨🇦Canada joseph.olstad

    @pefferen , I think you've misunderstood the use case.

    I have another Drupal site, in my case, it's Drupal 8. I've used the above mentioned patch and on my remote Drupal 8 website I am exposing the nodes as json data from a Drupal 'view'. The Drupal 8 site does NOT have the external_entities module installed.

    I have configured this external endpoint as an external entity mapping in my Drupal 10 website that has the external_entities module installed.

    My question to you, what is your documentation for doing this without the above mentioned patch?

    How should this be set up so that paging the external source works and so that I avoid the search_api tracking timeouts?

    Shouldn't there be documentation be made available for this?

  • 🇫🇷France guignonv

    @joseph.olstad, I had a look to your patch which looks fine. However, I would like to propose or more generic approach than the one proposed here. The only use case it currently solves is the results in an array (at first level) but there are other similar uses cases.

    What if the array of objects is stored in a more complex structure? Then, a new patch will be needed...

    For instance, consider these 2 REST endpoints where data objects are stored in the sub arrays:
    in "artObjects" for https://www.rijksmuseum.nl/api/en/collection?format=json&key=fpGQTuED and in "results.data" for https://www.musabase.org/brapi/v1/germplasm/?page=0&pageSize=10

    Proposed modification: instead of a checkbox, provide a text field labeled "JONPath to dataset (use when objects are not returned in a simple JSON list but rather in a more complex structure)" and then grab the data using JSONPath as the library is included with External Entities. Then, for the first example, the path would be "$.artObjects.*" and for the second "$.result.data.*". It would handle many more use cases without too much complexity. Maybe an example could be added as field description (for instance: "Ex.: if the REST service returns objects wrapped in a structure {..., "data": [{object1},{object2},..]}, use the JSON path "$.data.*".").

    Something like that (for the query() part):

    use JsonPath\JsonObject;
    
    ...
    
    // $results would contain the parsed JSON structure.
    if (!empty($this->configuration['data_jsonpath'])) {
      $json_object = new JsonObject($results, TRUE);
      $results = $json_object->get($this->configuration['data_jsonpath']);
    }
    return $results;
    ...
    

    Note: for the load() part, e complex result structure would be managed through field mapping (JSONPath field mapper for instance).

  • Issue was unassigned.
  • Status changed to Needs review 23 days ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 23 days ago
    2 pass
  • Now that 🐛 Storage client should return empty array instead of NULL Fixed is in, the patch needs a re-roll, though I'm really not sure about if this is the right way. But given the earlier code and now with the chance of an empty, is this the right way?

  • 🇨🇦Canada joseph.olstad

    @leymannx, that looks like an accurate reroll. I'm not sure if I understand what the implications are with the @guignonv suggestions in comment #32 .

    Unfortunately, my client ran out of budget to pay me to work on this so I have no time to dig hard back into this for now.

  • 🇫🇷France guignonv

    Regarding my #32 comment, you may ignore it here as we are dealing with v2. However, I did the change I proposed in v3 and v3 supports load() method returning NULL.

    To clarify, it is up to the community to decide how this issue could be solved for v2 and my previous proposal might bring too much changes for a stable v2. I hope it's clearer. :)

Production build 0.69.0 2024