Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
65
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

Download from Wow! eBook <www.wowebook.com>

CHAPTER 12 MANIPULATING USER INPUT: THE FILTER SYSTEM

Installing a Filter

Installing a filter follows the same procedure as installing a module, because filters live within module files. Making a filter available to use is therefore as easy as enabling or disabling the corresponding module by clicking on the Modules link in the top menu. Once installed, click on the Configuration link at the top of the page, and on the Configuration page, click on the Text formats link to assign the new filter to the text format(s) of your choosing. Figure 12-6 shows the relationship between filters and modules.

Figure 12-6. Filters are created as part of modules.

Knowing When to Use Filters

You might be wondering why a filter system is even needed when you can easily manipulate text using existing hooks found elsewhere. For example, it would be just as easy to use hook_node_view() to convert URLs to clickable links rather than using the URL filter. But consider the case in which you have five different filters that need to be run on the body field of nodes. Now suppose you’re viewing the default http://example.com/?q=node page, which displays ten nodes at a time. That means 50 filters need to be run to generate a single page view, and filtering text can be an expensive operation. It would also mean that whenever a node is called, it has to run through the filters, even if the text that’s being filtered is unchanged. You’d be running this operation over and over again unnecessarily.

The filter system has a caching layer that provides significant performance gains. Once all filters have run on a given piece of text, the filtered version of that text is stored in the cache_filter table, and it stays cached until the text is once again modified (modification is detected using an sha256 hash of the filtered contents). To go back to our example, loading ten nodes could effectively bypass all filters and just load their data straight from the cache table when that text hasn’t changed—much faster!

300

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]