0

Goal:

  1. Have a REST endpoint which will deliver list of rendered entities.
  2. Each row should contain a ready-to-use HTML.
  3. All the necessary JavaScript and CSS files should be also included on the page.
  4. P.S. I need this endpoint for lazy-loading of entities.

Given:

  1. I have a Views display with REST export.
  2. I render entities there using the rendered_entity field.

Problem:

  1. JavaScript and CSS files, needed for rendering, are not attached on the page.
  2. E.g. scripts for display of an image gallery, lazy-loading of images.
3
  • Possible workaround: attach the needed libraries on the main page.
    – Eyal
    Nov 24 '16 at 22:42
  • I think that the views module uses drupal Ajax APIs to add CSS and JavaScrip.
    – Eyal
    Nov 24 '16 at 22:43
  • @Eyal, I do not know all the assets on the main page. Some view entries might contain some JS, which was not yet rendered. Ajax API is not used in Views REST export.
    – milkovsky
    Nov 25 '16 at 11:38
1

We came up with a new module: Ajax Assets Plus.

The module provides a new render which collects all the necessary libraries during rendering. It results in an array:

return [
      'commands' ['An array with ajax commands for settings'],
      'content' => 'Rendered content(HTML)',
      'libraries' => 'all the needed assets grouped into libraries',
    ];

We had to to override the AjaxResponseAttachmentsProcessor and AssetResolver classes.

Also we implemented the hook_js_settings_build() to collect all the libraries on the page, as ajaxPageState does not contain all the dependencies.

2
  • it would be great if you could add more details. This was more than 3 years ago, are you still following this approach to get rendered HTML content in a REST endpoint?
    – Pere
    Jun 8 '20 at 8:34
  • 1
    @Pere, we have released a module for this afterwards. I have updated the answer.
    – milkovsky
    Jun 8 '20 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.