我需要创建一个自定义模块,用于在特定菜单项上生成静态 HTML 页面。我知道如何使用菜单 Hook 创建菜单项,但是我将如何让模块在该菜单页面上生成静态 html?
基本上,我创建了一个静态 HTML 站点,需要将其包含到模块中。然后我将该模块发送给其他人以安装在他们的网站上。
我还必须包含所有图像、CSS 和 JavaScript。它还需要位于当前主题之外,因为它也具有不同的外观和感觉。
最佳答案
像平常一样创建 .html、.css 和 .js 文件(但是,不要在 .html 文件中包含 <html>
、 <head>
或 <body>
标记,因为这些标记在您的模块已加载)。您定义的任何 CSS 都将覆盖 Drupal 的默认 CSS。
然后您可以使用表单 API 的 item
元素来导入静态 HTML 页面。
static_page.module
/**
* Implements hook_menu().
*/
function static_page_menu() {
$items = array();
$items['staticpage'] = array(
'title' => t('My Static Page'),
'page callback' => 'drupal_get_form',
'page arguments' => array('static_page_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_form().
*/
function static_page_form($form, &$form_state) {
// Add CSS and JS
drupal_add_css(drupal_get_path('module', 'static_page') . '/static_page.css');
drupal_add_js(drupal_get_path('module', 'static_page') .'/static_page.js');
// Import your static HTML page
$form['html'] = array(
'#type' => 'item',
'#markup' => file_get_contents(drupal_get_path('module', 'static_page') .'/static_page.html'),
);
return $form;
}
如果您不希望静态页面包含为网站其余部分显示的标准页眉、面包屑、侧边栏和页脚,您可以使用 CSS 隐藏它们。
static_page.css
#header,
#breadcrumb,
.sidebar,
#footer-wrapper {
display: none !important;
}
关于html - 自定义 Drupal 模块中的静态 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26767284/