开发者

Can Drupal change the template used based on a file extension in the url?

I am using Drupal 6.16 with a number of modules installed. I开发者_如何学Go was trying to find out if there is a way to change the output of a node when a different file extension is added to the url. For example:

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

Is this even possible with Drupal? Do I have to hack the core code to get this working?


You should not need to hack the core code. There are probably several contributed modules that can do this for you.

To output an XML version of a node, check out the Views Bonus Pack module, which extends the Views module. It has basic export capabilities, including CSV, TXT, DOC, and XML. The documentation is brief, but there is a README.txt file in the views_bonus/export/ directory that gives the basic steps for creating a feed in a view that will output XML.

You can set the path for the feed, so while I don't believe the .xml extension will work, you could set up a path with an additional component like this:

http://example.com/drupal?q=foo/bar      <-- normal output
http://example.com/drupal?q=foo/bar/xml  <-- XML output

To change the template file that is used for a node based on the path, you can use a preprocess function in your template.php file to add a template suggestion based on the path. This takes a bit more understanding of how the template files work, but ultimately you'll have more control of the output than you will with a view.


Here is how I fixed this.

  1. Add the custom_url_rewrite_inbound function to check for incoming request ending with .xml. If it finds a request ending with .xml it strips that off, so that the correct data can be located by the rest of the drupal machinery. It also sets 'subsite_xml_request' to true so that the appropriate theme template can be used later.

    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;
        }
    }
    
  2. Modify the phptemplate_preprocess_page function in your template.php to add additional '-xml' templates.

    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';
          }
        }
      }
    }
    
  3. Create the required page-xml.tpl.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜