0

I've been trying to load a custom template for a custom module page in the administration section, but the template isn't being loaded. This is the code so far:

custom_theme.module

/**
 * Implements hook_menu().
 */
function module_custom_menu() {

    $template_path = drupal_get_path('module', 'module_custom') . '/templates';

    $items['admin/module-custom'] = array(
        'title' => 'Turnos',
        'description' => 'Custom module functionality',
        'position' => 'right',
        'weight' => -15,
        'page callback' => 'system_admin_menu_block_page',
        'access arguments' => array('administer site configuration'),
        'file' => 'system.admin.inc',
        'file path' => drupal_get_path('module', 'system'),
    );

    $items['admin/module-custom/availability'] = array(
        'title' => 'Captación de disponibilidad',
        'description' => 'Users availability functionality',
        'page callback' => 'user_availabity',
        'access arguments' => array('administer site configuration'),
        'type' => MENU_NORMAL_ITEM,
    );

    return $items;

}

/**
 * Implements hook_theme()
 */
function module_custom_theme($existing, $type, $theme, $path) {

    $template_path = drupal_get_path('module', 'module_custom') . '/templates';

    return array(
        'user_availability' => array(
            'variables' => array('users', NULL),
            'template' => 'module-custom-admin',
            'path' => $template_path
        )
    );

}

/**
 * Load user availability
 */
function user_availabity() {

    $users = entity_load('user');    
    return theme('user_availabity', $users);

}

module-custom-admin.tpl.php

<div>
    <h2>Template subtitle</h2>
</div>
<div>
    <?php foreach ($users as $key => $user) : ?>
        <div>
            <?php print_r($user); ?>
        </div>
    <?php endforeach; ?>
</div>

But the template's code isn't shown: Module admin page Thanks for any advice.

0

hook_theme expect second parameter to be an array. See the doc here. In your implementation below you are passing an object.

/**
 * Load user availability
 */
function user_availabity() {

    $users = entity_load('user');    
    return theme('user_availabity', $users);

}

What you can do is to typecast the value.

$users = (array) entity_load('user', array(uid));

Here is an example from my existing project.

/**
 * Implements hook_theme().
 */
function derusni_theme($existing, $type, $theme, $path){
  return array(
    'quotation_list' => array(
      'template' => 'quotation-list',
      'path' => $path . '/templates',
      'type' => 'theme',
      'variables' => array(),
    ),
  );
}

And here is function that implements that theme function.

/**
 * List the quotation started by user.
 */
function derusni_page_quotation_started() {
  $variables = array();

  $quotation_ids = _get_quotations(arg(1), 'Started');
  foreach ($quotation_ids as $quotation_id) {
    $variables['quotations'][] = entity_load_single('quotation', $quotation_id);
  }
  $variables['edit_path'] = "/admin/structure/entity-type/quotation/quotation/";
  $variables['dest_path'] = '?destination=/' . current_path();

  return theme('quotation_list', $variables);
}

HTH

| improve this answer | |
  • @AndresMauricio please see my update with example – Gulok Jul 23 '16 at 3:29
  • Thank you very much, I tried the same as your example and it worked. – Mauricio Forero Jul 29 '16 at 19:15
  • Glad it worked. – Gulok Jul 30 '16 at 3:03
0

It is a well known issue of the hook_theme() function.

For successfully loading the template file, you have to follow the naming convention to rename the template name, which must be same as the theme key, with converting _ to -.

/**
 * Implements hook_theme()
 */
function module_custom_theme($existing, $type, $theme, $path) {

    $template_path = drupal_get_path('module', 'module_custom') . '/templates';

    return array(
        'user_availability' => array(
            'variables' => array('users', NULL),
            'template' => 'user-availability',
            'path' => $template_path
        )
    );

}
| improve this answer | |
  • Well, I tried as suggested in the link, but that didn't work either. – Mauricio Forero Jul 22 '16 at 22:33
  • Did clear cache? – Jimmy Ko Jul 22 '16 at 22:37
  • 1
    Yeah, I always do that after any change in the module's code. – Mauricio Forero Jul 25 '16 at 19:54
  • How about you define the theme key with module name prefix? It is rare case for me to have a theme key completely different from the module name. I guess if it causes the problem. – Jimmy Ko Jul 25 '16 at 20:00
  • 1
    Part of your indications were helpful, thank you too. – Mauricio Forero Jul 29 '16 at 19:16

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.