I’m trying to develop a module for Drupal, I’m quite new.
The following piece of code is working well, except for the included file. The contents of the included file are being show outside of the template, while the rest is correctly shown inside the template. Why does this happen and how to solve this?
drupal_add_css(drupal_get_path('module', 'helloworld') . '/helloworld.css', array('group' => CSS_DEFAULT, 'every_page' => TRUE));
function helloworld_menu(){
$items = array();
$items['helloworld'] = array(
'title' => t('Hello world'),
'page callback' => 'helloworld_output',
'access arguments' => array('access content'),
);
return $items;
}
function helloworld_display(){
include_once ( dirname(__FILE__) . '/helloworld.display.php');
}
/*
* Display output
*/
function helloworld_output() {
header('Content-type: text/plain; charset=UTF-8');
header('Content-Disposition: inline');
$output = "<div id='hw_wrapper'>";
$output .= helloworld_display();
$output .= 'hej';
$output .= "</div>";
return $output;
}
I would rather use the following code.
Rename the helloworld.display.php file as helloworld-mypage.tpl.php, and put it in the directory containing the helloworld.module file.
I will add some notes:
hook_init() is used when the CSS file needs to be included in any page. If you need to use it just in your page, you could use the following code. (Replace
helloworld_output()I shown before with the following code; the rest is the same as before.)If you had a single call to
drupal_add_http_header(), you could replace it with an item in the #attached array, as in the following code.See drupal_process_attached() for more information.