3

Currently, I am using the following code.

/**
 * Implements hook_theme_registry_alter().
 */
function MY_MODULE_theme_registry_alter(&$theme_registry) {
  // Defined path to current module.
  $is_admin = Drupal::service('router.admin_context')->isAdminRoute();
  $node = \Drupal::routeMatch()->getParameter('node');
  if (isset($node)) {
    if ($node->getType() == 'MY_CONTENT_TYPE' && $is_admin == 0) {
      $module_path = drupal_get_path('module', 'reveal_js');
       $theme_registry['html']['path'] = $module_path . '/templates';
       $theme_registry['page']['path'] = $module_path . '/templates';
    }
  }
}

This works, but when going to another page, I have to rebuild cache in order for it to re-evaluate whether the node is 'MY_CONTENT_TYPE' or a different type.

  • you should define hook_theme and use node__MY_CONTENT_TYPE and use node as base hook for it and place your template in your module's templates directory. This approach of your is very bad. – user21641 Oct 22 '15 at 11:50
6

It looks like hook_theme_registry_alter() is only executed when the theme cache is rebuilt. This means that what you need to do is use one of the theme_suggestion_alter hooks, such as hook_theme_suggestions_alter() or hook_theme_suggestions_node_alter(). These get run for each page before it is cached, I believe, instead of only once on cache rebuild.

So, what you want to do in your module is create a theme implementation using hook_theme(). That would look something like this:

function MYMODULE_theme($existing, $type, $theme, $path) {
  return array(
    'my_theme_implementation' => array(
      'path' => drupal_get_path('module', 'reveal_js') . '/templates',
      'template' => 'my_theme_implementation',
    ),
  );
}

And then your hook_theme_suggestions_node_alter() would be something like

function MYMODULE_theme_suggestions_node_alter(&$suggestions, $variables) {
  $is_admin = Drupal::service('router.admin_context')->isAdminRoute();
  $node = $variables['elements']['#node']
  if ($node->getType() == 'MY_CONTENT_TYPE' && $is_admin == 0) {
    $suggestions[] = 'my_theme_implementation';
  }
}

Does that all make sense?

1

Tried both solutions listed and only the first one, using hook__theme_registry_alter had the desired result that I was looking for, i.e., I wanted to create a template that would behave just like creating a template file in a theme would do, i.e., want it to be processed just like a page template. You can see that with the working implementation you get the full set of preprocess functions.

    [page] => Array
    (
        [template] => page
        [path] => modules/futusign/templates
        [type] => base_theme_engine
        [theme path] => core/themes/bartik
        [render element] => page
        [preprocess functions] => Array
            (
                [0] => template_preprocess
                [1] => template_preprocess_page
                [2] => contextual_preprocess
                [3] => futusign_preprocess_page
            )

    )

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.