0

I have a render array:

$element = array(
   '#theme' => 'custom_theme_func',
);

drupal_render($element);

I have a preprocess function in which I'd like to add theme wrappers to this render array:

function my_module_custom_theme_func(&$element)
{
    $element['theme_wrappers'] = array('some_theme_func');
}

The problem that I'm running into is that some_theme_func() is not being called. If I add it to the list of theme wrappers before I call drupal_render(), however, it works.

2 Answers 2

1

There are two errors in your code:

  • If you use '#theme' => 'custom_theme_func' to reference the theme function, Drupal will look for theme_custom_theme_func(), not my_module_custom_theme_func().
  • The argument a theme function gets is always $variables, an array that in your case probably contains $variables['element'].
    What exactly that array contains depends from the definition given in the implementation of hook_theme(). For example, theme_status_report() is defined as:

    'status_report' => array(
      'render element' => 'requirements', 
      'file' => 'system.admin.inc',
    ), 
    

    The array it gets contains $variables['requirements'].

    theme_image() is defined as:

    'image' => array(
      'variables' => array('path' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => array()),
    ), 
    

    The array it gets can contain $variables['path'], $variables['width'], $variables['height'], $variables['alt'], $variables['title'], and $variables['attributes'].

As for setting #theme_wrappers, setting them in the theme function doesn't have any effect, as the theme function doesn't receive the $variables array as reference; therefore, it cannot change its content, and pass those changes back to drupal_render().

As zdecibel said, you can use the preprocess function to set the theme wrappers, even if it is probably better to define them directly in the code you shown at the beginning (the first 3 lines), as you are not dynamically changing the theme wrappers. (At least, that is what you shown in your code.)

0

Because of function my_module_custom_theme_func don't pass a reference parameter. you can try

function template_preprocess_custom_theme_func(&vars){
  $element = $vars['element'];
  $element['theme_wrappers'] = array('some_theme_func');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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