我正在使用安装了许多模块的 Drupal 6.16。我试图找出在将不同的文件扩展名添加到 url 时是否有办法更改节点的输出。例如:
http://example.com/drupal?q=foo/bar - returns a normal drupal node
http://example.com/drupal?q=foo/bar.xml - returns xml output of the node
Drupal 甚至可以做到这一点吗?我是否必须破解核心代码才能使其正常工作?
我正在使用安装了许多模块的 Drupal 6.16。我试图找出在将不同的文件扩展名添加到 url 时是否有办法更改节点的输出。例如:
http://example.com/drupal?q=foo/bar - returns a normal drupal node
http://example.com/drupal?q=foo/bar.xml - returns xml output of the node
Drupal 甚至可以做到这一点吗?我是否必须破解核心代码才能使其正常工作?
您不需要破解核心代码。可能有几个贡献的模块可以为您做到这一点。
要输出节点的 XML 版本,请查看 Views Bonus Pack模块,它扩展了 Views 模块。它具有基本的导出功能,包括 CSV、TXT、DOC 和 XML。文档很简短,但在 views_bonus/export/ 目录中有一个 README.txt 文件,它提供了在将输出 XML 的视图中创建提要的基本步骤。
您可以设置提要的路径,因此虽然我不相信.xml扩展程序会起作用,但您可以设置一个带有附加组件的路径,如下所示:
http://example.com/drupal?q=foo/bar <-- normal output
http://example.com/drupal?q=foo/bar/xml <-- XML output
要根据路径更改用于节点的模板文件,您可以使用 template.php 文件中的预处理函数来添加基于路径的模板建议。这需要对模板文件的工作方式有更多的了解,但最终您将拥有比使用视图更多的输出控制权。
这是我解决此问题的方法。
添加custom_url_rewrite_inbound函数以检查以 .xml 结尾的传入请求。如果它发现一个以 .xml 结尾的请求,它会将其删除,以便其他 drupal 机器可以找到正确的数据。它还将 'subsite_xml_request' 设置为 true,以便以后可以使用适当的主题模板。
function custom_url_rewrite_inbound (&$result, $path, $path_language) {
if(preg_match('/\.xml$/', $path)) {
$search = preg_replace('/^(.*)\.xml$/', "$1", $path);
if ($src = drupal_lookup_path('source', $search, $path_language)) {
$_REQUEST['xml_request'] = true;
$result = $src;
}
}
修改 template.php 中的phptemplate_preprocess_page函数以添加额外的“-xml”模板。
function phptemplate_preprocess_page(&$vars) {
if ($_REQUEST['xml_request']) {
if (module_exists('path')) {
$path = str_replace('/edit','',$_GET['q']);
$alias = drupal_get_path_alias($path);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$vars['template_files'][] = $template_filename . '-xml';
}
$vars['template_files'][] = 'page-xml';
}
}
}
}
创建所需的页面-xml.tpl.php