Hello,

A wonderful idea is to attach a JS to a block and have it loaded only when the block is visible instead of attaching JS to whole website on all pages. But the question is why did you hardcod a footer scope for all block javascript files?

It is quite often when we need to load an external JS library BEFORE the block contents because there is some embedded JS code that relies on JS library. This is hardoced in javascript_libraries_block_view_alter():

function javascript_libraries_block_view_alter(&$data, $block) {
  ...
  $options['scope'] = 'footer';
  ...
}

We are now preparing the patch which is needed for our project. We will upload it as soon as it is ready for you to review it. For now, I wonder did you have any plans to make an optional scope for a block? We are making one optional scope per each block. Your thoughts?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dmitry.kischenko’s picture

I have done a patch for this feature request. I did the following:

1. A scope for a block is added - now you can specify header or footer for JS files attached to a block.

2. JS file inline inserting is changed to #attached attribute that work in a block cache mode - instead of inserting block files using inline mode we can use a core Render API feature - put all JS files into $block['content']['#attached']['js']. This is compatible with caching and no need to write your own function.

3. Code refactoring - moved much code that is needed for back end to .admin.inc file. Size of module file is now three times less.

Please review this patch. Thank you.

ardas’s picture

Just a few more words about this patch... the main reason is to implement a scope option for each block. We are working on the project that is hosted in Acquia hosting. Mori Sugimoto suggested us to use this module and we really liked it. But we need an ability to load some JSes in header otherwise our blocks will not work.

In addition to this change we also find that you insert JSes using inline method because drupal_add_js() will not be called in a block cache module (because hook_block_view_alter is not called) and this is right but we suggested to use #attached feature of Render API that works pretty good.

And in the end, we decided to move some code in admin.inc file to reduce module's size as much as we can.

We are interested in this module and are going to include it in our Drupal build. This is why we would like to co-maintain this module if possible.

Thanks!

ardas’s picture

Status: Active » Needs review

We need review now :)

Status: Needs review » Needs work
dmitry.kischenko’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
dmitry.kischenko’s picture

We moved function javascript_libraries_custom_load to .module file
Please, review patch.
Thanks.

ardas’s picture

Hi guys,

Could you please test the patch or instruct us how we can do this (if possible).

FYI, this patch has two things:
1. Moving as much source code as possible into admin.inc since we don't need to load it on each and every page of the site (reducing memory usage)
2. Supporting scope parameter on the block level so that user can specify a scope for each block - sometimes we need to have a JS library to load in header BEFORE the code in the block.

If you have any concerns for this patch lets discuss them.

Thank you.