Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 470 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drupal 7 - How to load a template file from a module?

#1
I am trying to build my own module in Drupal 7.

So I have created a simple module called 'moon'

function moon_menu() {
$items = array();
$items['moon'] = array(
'title' => '',
'description' => t('Detalle de un Programa'),
'page callback' => 'moon_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);

return $items;
}

function moon_page(){


$id = 3;

$content = 'aa';

}

in moon_page() function, I like to load a custom template 'moon.tpl.php' from my theme file.

is this possible?
Reply

#2
For your own stuff (not overriding a template from another module)?

Sure, you only need to:

- Register your template with [hook_theme()][1]

- Call theme('moon', $args)

$args is an array that contains the arguments to the template as specified by your hook_theme() implementation.


[1]:

[To see links please register here]

Reply

#3
/*
* Implementation of hook_theme().
*/
function moon_theme($existing, $type, $theme, $path){
return array(
'moon' => array(
'variables' => array('content' => NULL),
'file' => 'moon', // place you file in 'theme' folder of you module folder
'path' => drupal_get_path('module', 'moon') .'/theme'
)
);
}

function moon_page(){

// some code to generate $content variable

return theme('moon', $content); // use $content variable in moon.tpl.php template
}
Reply

#4
For Drupal 7, it did not worked for me. I replaced line in hook_theme

'file' => 'moon', by 'template' => 'moon'

and now it is working for me.
Reply

#5
In drupal 7 I was getting the following error when using :

`return theme('moon', $content);`

Was resulting in "Fatal error: Unsupported operand types in drupal_install\includes\theme.inc on line 1071"

This was fixed using :

`theme('moon', array('content' => $content));`
Reply

#6
You may use moon_menu, with hook_theme

<?php

/**
* Implementation of hook_menu().
*/
function os_menu() {
$items['vars'] = array(
'title' => 'desc information',
'page callback' => '_moon_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}

function _moon_page() {
$fields = [];
$fields['vars'] = 'var';

return theme('os', compact('fields'));
}

/**
* Implementation of hook_theme().
*/
function os_theme() {
$module_path = drupal_get_path('module', 'os');

return array(
'os' => array(
'template' => 'os',
'arguments' => 'fields',
'path' => $module_path . '/templates',
),
);
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through