0

I have a views block that I want to modify with javascript. I can get it to load with the following in my template.php:

function THEME_preprocess_block(&$variables) {  
      drupal_add_js(drupal_get_path('theme', 'THEME') . 
      '/js/script.js' ,  array('type' => 'file', 'scope' => 'footer'));
} 

However, I only want it to load when a certain block is displayed. I want to use something like:

function THEME_preprocess_block(&$variables) { 
    if ($block->block_id == 'matching_id') {
      drupal_add_js(drupal_get_path('theme', 'THEME') . 
      '/js/script.js' ,  array('type' => 'file', 'scope' => 'footer'));
    }
}

If this is possible how would I go about making sure it only runs on that block?

  • $block doesn't exist. You would need to use whatever is within the $variables array and key it off that way. – Kevin Apr 16 '18 at 20:12
0

You can just get the element like this:

$variables['elements']['#id']

And then if you would of some reason need to load the block you can do like this:

$block = entity_load('block', $variables['elements']['#id']);
$block_id = $block->id;
  • Poster does not need to load the block, the poster only needs to be able to find the block id which sounds like $variables['elements']['#id']. – Kevin Apr 16 '18 at 20:55

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.