Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I've got a hook_node_view which I'm trying to add a property of the node to an array that I have created in the Drupal.settings

drupal_add_js(array('embedded_videos'=>array()));

So in the node_view I can do

function mymod_node_view($node, $view_mode, $langcode) {
if($node->type == 'video'){
   drupal_add_js(array(
    'vimn_video' => array('embedded_videos' => $node->nid,
      ),
    ),
  'setting');
}  

But this overwrites what is in there, whereas I want to add to the array. Because this node_view is running multiple times as I have 2 video nodes embedded into an article node.

Any ideas appreciated. I am new to the usage of Drupal.settings javascript api.

edit: The issue was that I needed to put $node->nid into an array array($node->nid), then it will add an item to the array rather than overwriting it.

share|improve this question

2 Answers 2

Your first declaration should indicate that you are passing a setting to drupal_add_js():

drupal_add_js(array('your_module' => array()), 'setting');

Then you can add other properties to your array in hook_node_view():

function your_module_node_view($node, $view_mode, $langcode) {
    $your_array = array(
      'vimn_video' => array(
        'embedded_videos' => array($node->nid), // NOTE: $node->nid as an array
      ),
    );
    drupal_add_js(array('your_module' => $your_array), 'setting');  
}  

You can retrieve the properties from javascript as:

Drupal.settings.your_module.vimn_video.embedded_videos; // an array with node ids.
share|improve this answer

I believe that you use hook_js_alter (and possibly drupal_js_defaults) https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_js_alter/7

Something like (untested of course):

function mymodule_js_alter(&$javascript) {
  if ( true ) {
    $javascript['settings']['vimn_video']['embedded_videos'] = $node->nid;
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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