1

I'm trying to pass variables from my module to a template. Everything is working fine, but the variables passed to the view appear to be modificated.

Callback to generate and fill the view:

 function mycallback() {
    //(...)
      //var_dump($members); 
      return theme('team_members', $members);
    }

Contents of $members:

array
  0 => 
    object(stdClass)[55]
      public 'mid' => string '1' (length=1)
      public 'name' => string 'LLUIS' (length=5)
  1 => 
    object(stdClass)[18]
      public 'mid' => string '2' (length=1)
      public 'name' => string 'OSCAR' (length=5) 

This is the hook:

function global_preprocess_team_members(&$variables) {
  //var_dump($variables); 
  $variables['members'] = $variables;
}

The content of $members is the following one:

array
  0 => 
    object(stdClass)[55]
      public 'mid' => string '1' (length=1)
      public 'name' => string 'LLUIS' (length=5)
  1 => 
    object(stdClass)[18]
      public 'mid' => string '2' (length=1)
      public 'name' => string 'OSCAR' (length=5)
  'content' => null
  'theme_hook_suggestions' => 
    array
      empty

Somehow, content, theme_hook_suggestions has been added to the array, but there is more.

Finally, when I print the results on the template file (tpl.php) via var_dump() again:

array
  0 => 
    object(stdClass)[55]
      public 'mid' => string '1' (length=1)
      public 'name' => string 'LLUIS' (length=5)
  1 => 
    object(stdClass)[18]
      public 'mid' => string '2' (length=1)
      public 'name' => string 'OSCAR' (length=5)
  'content' => null
  'theme_hook_suggestions' => 
    array
      empty
  'members' => 
    array
      0 => 
        object(stdClass)[55]
          public 'mid' => string '1' (length=1)
          public 'name' => string 'LLUIS' (length=5)
      1 => 
        object(stdClass)[18]
          public 'mid' => string '2' (length=1)
          public 'name' => string 'OSCAR' (length=5)
      'content' => null
      'theme_hook_suggestions' => 
        array
          empty
      'members' => 
        &array
  'zebra' => string 'odd' (length=3)
  'id' => int 1
  'directory' => string 'sites/all/themes/global' (length=23)
  'classes_array' => 
    array
      0 => string 'team-members' (length=12)
  'attributes_array' => 
    array
      empty
  'title_attributes_array' => 
    array
      empty
  'content_attributes_array' => 
    array
      empty
  'title_prefix' => 
    array
      empty
  'title_suffix' => 
    array
      empty
  'user' => 
    object(stdClass)[9]
      (...)
  'db_is_active' => boolean true
  'is_admin' => boolean true
  'logged_in' => boolean true
  'is_front' => boolean false

I know Drupal adds some variables here (e.g. user session), but as you can see, my array has been added twice. One in the variables root, and another one inside the members variables (with the extra fields I do not want).

What am I doing wrong?

I thing the ideal situation is having the members variable inside the variables without the extra content (only once).

Do you have any idea?

1

This bit is the problem:

$variables['members'] = $variables;

When you declare named variables in your theme function they are passed in as named keys of the $variables array. So when you call your theme function:

// Note the array wrapping the passed variable and also the 'members' key of the array. This is required in Drupal 7.
print theme('team_members', array('members' => $members)); 

This is what you'll get in your preprocess function:

$members = $variables['members']; // This contains the original member array passed to the theme function as the named variable 'members'.

What you're doing at the moment is overwriting $variables['members'] with the $variables array itself, so all you're really doing is adding an extra array around the variables array.

Try putting the members that are passed into the preprocess function into a different variable:

$variables['custom_variable'] = do_something_to($variables['members']);

and outputting that one in your template file:

print $custom_variable;

I think that's what you're trying to do, have a look at hook_theme() for more information :)

  • Thanks @Clive (again :)). I misunderstood the way variables were passed to the view ( I should read more carefully the API...) I finally get rid of the preprocess function, I didn't need it. – Xavi Colomer Feb 3 '12 at 12:10

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.