开发者

HTML\rich text in Drupal's node title?

I need the node titles to be able to display some text formatting.开发者_Go百科

As far as I understand, Drupal's title field is just plain text, it doesn't allow HTML or any other input format. Is that true? Is there a way to override this, or another solution?

I am working with Drupal 6.


You can pretty easily do this in your theme, there are different ways to do with. The simplest would probably to do it in your template.php

/* this function generates the variables that are available in your node.tpl,
 * you should already have this in your template.php, but if not create it
 */
function mytheme_preprocess_node(&$vars) {
    // It's important to run some kind of filter on the title so users can't
    // use fx script tags to inject js or do nasty things.
    $vars['title'] = filter_xss($vars['node']->title);
}

What happens in the function is that it overwrites the default value for $title in the node.tpl which holds the variable used for the title. It is usually placed within a h1 or h2 tag, so you should be aware of that. The filter_xss is used to only allow basic html tags, so protect the site, you can look at that function here. That are some other filter functions, like check_markup, and filter_xss_admin, but you can give a second param for filter_xss which is an array of allowed tags, so should the default not be good enough, you can just change it to fit your needs.


Use function mytheme_preprocess_page for D7.


Extending the module mentioned by wiifm, for D7 there is now also: https://drupal.org/project/html_title_trash

It allows more tags and also works for block titles, rather than just node titles.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜