0

I'm trying to create a custom Drupal 7 module where it renders a custom page template. I want the data on /animals and /animals/getanimal to be in a custom HTML template.

After much searching and few successes, I've found that hook_preprocess_html works and that I can stop Drupal from loading the default template, but I'm struggling to pass data to my templates from the menu hook.

Is there a way to pass data from the menu hook to hook_preprocess_html, or is there another way where I can load my own custom template from the URLs in my custom module?

/**
 * Implements hook_menu()
 * @return mixed
 */
function animal_farm_menu() {

  $items['animals'] = array(
    'title'           => 'Discover animals',
    'page callback'   => 'getDiscover',
    'page arguments'  => array(),
    'access callback' => TRUE,
    'type'            => MENU_NORMAL_ITEM,
    'menu_name'       => 'main-menu',
  );

  $items['animals/getanimal'] = array(
    'title'           => 'all animals',
    'page callback'   => 'getAnimal',
    'page arguments'  => array(),
    'access callback' => TRUE,
    'type'            => MENU_NORMAL_ITEM,
    'menu_name'       => 'main-menu',
  );

  return $items;
}


function getDiscover()
{
  $result = ['cat','dog','monkey','cow'];
  return $result;
}

function getAnimal()
{
  $result = ['cat','dog','monkey','cow'];
  return $result;
}


/**
 * Implements hook_preprocess_html().
 */
function animal_farm_preprocess_html(&$vars) {

  $node = menu_get_object();

  $path = current_path();

  // echo $path;

  // Add a new theme suggestion when viewing node's of mynodetype.
  if ($path == 'animals') {
    $vars['theme_hook_suggestions'][] = 'html__mynodetype';
    $vars['data'] = [1,2,4,'monkey'];
  }

  if ($path == 'animals/getanimal') {
    $vars['theme_hook_suggestions'][] = 'animal_farm_discover';
    $vars['data'] = [1,2,4,'monkey'];
  }

}

/**
 * Implements hook_theme().
 */
function animal_farm_theme($existing, $type, $theme, $path) {
  $theme = array();

  //Add our templates/html--mynodetype.tpl.php to the theme registry
  $theme['html__mynodetype'] = array(
    'template' => 'html--mynodetype',
    'path' => drupal_get_path('module', 'animal_farm') . '/templates',
  );
  return $theme;
}
  • Why you are not using hook_preprocess_page for this as you are trying to alter page tpl. To pass data you can through hook_theme. – Prem Patel Jul 15 '18 at 17:44
  • Please follow the below link for your query solution drupal.org/forum/support/theme-development/2013-04-19/… – Prem Patel Jul 15 '18 at 17:49
  • @prem I've tried that and it pulls in the base template, I don't want any template apart from my own custom one in the module. – user2075215 Jul 15 '18 at 17:50
  • @PremPatel I've tried the above solution too, but it too pulls in the base template, I don't want any template to come in to the module output. – user2075215 Jul 15 '18 at 17:55
  • Is there any specific reason, not to use html.tpl file? As it's only contain basic layout of page like head, and body tag with class. – Prem Patel Jul 15 '18 at 17:59
0

D7's html.tpl.php doesn't contain menu in $vars, as it's supposed to represent the template that contains the high-level HTML document. This data is in page.tpl.php via $main_menu / $secondary_menu vars.

If you need menu data in hook_preprocess_html() you can do something like call menu_tree_all_data('main-menu') in your code which will retrieve the menu data for main-menu.

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.