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.
  • 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
0

It was quite some work to adapt the rendering process. There is a lot of code, I will just summarise the solution. If you want more details, just write a comment.

I created a custom render which collects all the libraries during rendering in an array. 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',
    ];

I had to to override the AjaxResponseAttachmentsProcessor and AssetResolver classes.

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

My current workaround with a list of entities: I created a custom controller instead of using views due to lack of resources, as there is a lot to override in views.

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.