4

I am trying to have one tpl file per view mode inside a module folder. But I can't get drupal to pick up the right tpl file.

I have been trying solutions on these pages:

http://www.metachunk.com/blog/adding-module-path-drupal-7-theme-registry

/**
 * Implements hook_theme_registry_alter()
 **/
function nwd_press_room_theme_registry_alter(&$theme_registry) {
    global $theme_key;
    $mod_path = drupal_get_path('module', 'nwd_press_room');
    $theme_registry_copy = $theme_registry;       // munge on a copy
    _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', $theme_key, $mod_path);
    $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
    $hooks = array('node');

    foreach ($hooks as $h) {
        if (!isset($theme_registry[$h]['theme paths'])) {
                $theme_registry[$h]['theme paths'] = array();
        }
        nwd_press_room_insert_after_first_element($theme_registry[$h]['theme paths'], $mod_path);
    }

    dpm($theme_registry);

}

/**
 * Helper function for re-ordering arrays (needed by theme_registry_alter)
 */
function nwd_press_room_insert_after_first_element(&$a, $element) {

    if(is_array($a)) {
        $first_element = array_shift($a);
        if ($first_element) {
            array_unshift($a, $first_element, $element);
        } else {
            array_unshift($a, $element);
        }
    }
}

Since I still want the tpl file to be overridable in the theme. Does anyone have an example that works?

1
  • If Drupal core developers wanted you to use _theme_process_registry, they would not have "prepended" that function with an underscore. Commented May 2, 2014 at 5:19

2 Answers 2

1

I created this module a while back. Could be useful here: https://drupal.org/project/view_mode_templates

How to use:

Download and install view_mode_templates module. In your custom module, implement the following hook which returns a list of templates you want to override:

/**
 * Implements hook_view_mode_templates().
 */
 function MODULE_view_mode_templates() {
  return array(
    'node_article',
    'node__article__teaser',
    'node__article__full',
    'taxonomy_term__tags__full',
  );
 }

Then you create your template in a templates directory. Eg. templates/node--article--teaser.tpl.php.

It works with any entity: nodes, users, field_collection..etc.

0
2

Found the solution after extensive search and trial & error:

/**
 * Define the new view modes
 *
 * implements hook_entity_info_alter()
 * @param $entity_info
 */
function my_module_entity_info_alter(&$entity_info) {

    $entity_info['node']['view modes']['grid'] = array(
        'label' => t('Grid'),
        'custom settings' => TRUE,
    );

    $entity_info['node']['view modes']['list'] = array(
        'label' => t('List'),
        'custom settings' => TRUE,
    );
}

/**
 * Tell Drupal to also look for template files in the modules folder
 *
 * implements hook_theme_registry_alter
 *
 * @param $theme_registry
 */
function my_module_theme_registry_alter(&$theme_registry) {

    $path  = drupal_get_path('module', 'my_module');
    $theme_registry += drupal_find_theme_templates($theme_registry, '.tpl.php', $path);
}

/*
 * Implements template_preprocess_node().
 */
function my_module_preprocess_node(&$variables){

    // Add the theme hook suggestion to include view mode
    $variables['theme_hook_suggestions'][] = 'node__'.$variables['type'].'__'.$variables['view_mode'];

  }
}

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