0

I am doing templating for a module that adds a block in the module's folder so that the template can be there, and it may be overridden in the theme if desired. I want the module to support different "sources" (but only one will be active/used at a time), which may each have a separate template and variable preprocessing. I have hook_theme() working, and the template works. But the .vars.php file I have contains the preprocess function, and it doesn't get called.

Here is a basic example of what I have:

/MODULE.module

function MODULE_theme($existing, $type, $theme, $path) {
  $source = '';
  switch ($source) {
    case 'SRC_A':
      $path .= '/sources/SRC_A/theme';
    default:
      $source = 'default';
      $path .= '/theme';
  }

  return array(
    'MODULE_block' => array(
      'path' => $path,
      'variables' => array('data' => array(), 'source' => $source),
      'template' => 'MODULE-block',
    )
  );
}

/MODULE/sources/SRC_A/theme/MODULE-block.tpl.php

(contains template HTML with references to PHP variables)

/MODULE/sources/SRC_A/theme/MODULE-block.vars.php

/**
 * Implements template_preprocess_HOOK() for theme_MODULE_block().
 */
function template_preprocess_MODULE_block(&$vars) {
  // Add variables before they get to the template file.
  $vars['some_key'] = 'some_value';
}

I originally was trying to modify the $template value in hook_theme() to MODULE-block--SRC-A, but I decided that didn't matter, and if only one template is active at once I should just call them all MODULE-block.tpl.php regardless of the source, since they are all in different folders.

The template works, and adding a template in my theme will correctly override it. But the .vars.php file with the preprocess function does not seem to do anything unless it is in my actual theme and is THEME_preprocess_MODULE_block instead.

I'm still pretty new to this, but I looked at a lot of pages online, and there aren't any similar examples in my code base that have a module-specific .vars.php file that is located in the module's folder rather than in the theme.

I do not know what I am doing wrong. Any ideas?

2
  • I'm not sure I understand - if the template is defined in the theme layer, that will always override the original. That said, why not preprocess the template as you seem to do above with the variables you want to pass into the block?
    – Kevin
    Jan 11, 2019 at 20:49
  • @Kevin The template is not going to be defined in the theme layer right now. I'm leaving it open so that it can be, but it probably won't be. As I said above, I'm trying to preprocess the template to pass in the variables using template_preprocess_MODULE_block, but the function is never called for some reason. I don't know why. I looked using dpm() and MODULE_block is in the theme registry.
    – mbomb007
    Jan 11, 2019 at 20:51

1 Answer 1

0

I figured it out. There was already a preprocess function with the name template_preprocess_MODULE_block in the module file. Declaring the same function a second time has no effect.

Your Answer

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

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