如何包含一些文件?
方法一
您可以将文件放在 web-root 文件夹下的任何位置,该文件夹是带有 index.php 文件的文件夹(主文件夹)。
$type = 'module';
$name_of_module = 'custom_module';
$path = drupal_get_path($type, $name_of_module);
include_once($path . 'custom_file.inc.php');
有效类型如下:
模块名称是模块文件夹中.info 文件的文件名。
Read more about drupal_get_path()
方法二
要包含现有模块中的文件,您应该使用:
module_load_include('inc', 'content', 'includes/content.node_form');
如何包含模板文件?
模板文件更好的地方是主题文件夹,因为其他人会期望模板文件在那里。
Read more about template naming suggestions.
但是如果这个模板没有你的自定义模块就没有意义,你可以把这个模板文件放在自定义模块文件夹中。
要在主题中附加模板,您应该使用:
function MYTHEME_preprocess_page(&$variables, $hook) {
if (array_key_exists('node', $variables)) {
if ($variables['node']->type == 'article'){
$variables['theme_hook_suggestions'][] = 'page__article';
}
}
}
在模块中附加模板:
/**
* Implements hook_theme().
*/
function MODULE_theme($existing, $type, $theme, $path) {
return array (
'node__CONTENTTYPE' => array (
'variables' => array( . . . ),
'template' => 'node--CONTENTTYPE' ,
'base hook' => 'node',
'path' => drupal_get_path('module', 'CUSTOM_MODULE'),
),
);
}
模板文件命名为node--CONTENTTYPE.tpl.php,放在CUSTOM_MODULE文件夹下。
Read more about hook_theme()
不要忘记清除缓存。