0

I have a custom view and a custom row template. For each row, the template defines a variable $rowdata. For example:

> row #0:  $rowdata = array(0=>array('a','b'), 1=>array('a','c'))  
> row #1:  $rowdata = array(0=>array('a','b'), 1=>array('a','d'))
> row #2:  $rowdata = array(0=>array('x'), 1=>array('x'))

What I Am Trying To Accomplish

I want merge the rowdata from all the rows:

   $merged_rowdata = array(
      0=>array('a','b'), 
      1=>array('a','c'), 
      2=>array('a','b'), 
      3=>array('a','d'), 
      4=>array('x'), 
      5=>array('x')
    )

and store the result in Drupal.settings.mymodule.$viewID.merged_rowdata:

Drupal.settings.mymodule.$viewID.merged_rowdata = array(
  0=>array('a','b'), 
  1=>array('a','c'), 
  2=>array('a','b'), 
  3=>array('a','d'), 
  4=>array('x'), 
  5=>array('x')
)

(Note: All rows have the same value for $viewID)

What I Tried

I tried placing the following code in my row template:

drupal_add_js(array('mymodule' => array($viewID => array('merged_rowdata' => $rowdata))), 'setting');

But this does not merge the rowdata in the way that I want, as described above. Instead, it replaces $viewID with the row_index:

Drupal.settings.mymodule = array(
  0 => array('merged_rowdata'=>array(0=>array('a','b'), 1=>array('a','c'))),
  1 => array('merged_rowdata'=>array(0=>array('a','b'), 1=>array('a','d'))),
  2 => array('merged_rowdata'=>array(0=>array('x'), 1=>array('x')))
)

I found that if I replace the variable $viewID with a string literal, eg 'myview':

drupal_add_js(array('mymodule' => array('myview' => array('merged_rowdata' => $rowdata))), 'setting');

then the rowdata gets merged in the way that I want:

Drupal.settings.mymodule.myview.merged_rowdata = array(
  0=>array('a','b'), 
  1=>array('a','c'), 
  2=>array('a','b'), 
  3=>array('a','d'), 
  4=>array('x'), 
  5=>array('x')
)

Anyone know how I can get my desired result? Is there some way to "tell" drupal_add_js that the variable $viewID has the same value for all the rows?

Background

My style template loops through each row of the view output, and executes the row template on each row. Here is the code for my style template:

<?php foreach ($rows as $id => $row): ?>
  <div>
    <?php print $row; ?>
  </div>
<?php endforeach; ?>
2
  • Sounds like you might be putting the code in the wrong spot (e.g. if your code is being run through a foreach loop that gets processed by another template file)... Can you post the name and contents of the template file? – jerdiggity May 28 '13 at 7:24
  • @jerdiggity: I added a 'background' section, that I hope answers your question – user606696 May 28 '13 at 20:00
1

If the string literal is giving you what you want, I'd try this:

drupal_add_js(array('o4mmgal' => array($view->name => array('merged_rowdata' => $rowdata))), 'setting');
0

I was able to solve my problem by changing the datatype of $viewID from a number to a string.

This was the original setting of $viewID:

$viewID = $view->vid; // eg $viewID = 9

This is the new setting:

$viewID = 'vid' . $view->vid; // eg $viewID = 'vid9'

Now, when I place this code inside my row template:

drupal_add_js(array('mymodule' => array($viewID => array('merged_rowdata' => $rowdata))), 'setting');

I get the desired result

Drupal.settings.mymodule.$viewID.merged_rowdata = array(
  0=>array('a','b'), 
  1=>array('a','c'), 
  2=>array('a','b'), 
  3=>array('a','d'), 
  4=>array('x'), 
  5=>array('x')
)

Here is my interpretation of what is going on:

drupal_add_js merges data differently depending on whether the keys in a particular subarray are numeric. If it sees all numeric keys, then it assumes the subarray is sequential (as opposed to associative), and resets the keys to make them sequential starting at 0.

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.