Drupal Text Formats Tutorial

How to manage text formats in Drupal

The core Filter module that comes prepackaged with Drupal allows you to configure and manage text formats and the filters used with each format. Filters are used to control the elements (text, tags, etc.) that the user inputs in content items (nodes), comments, etc. Filters determine how the text will be displayed on the frontend of your site. For example, one of the default filters that comes prepackaged with Drupal allows you to limit the HTML tags that can be used; another one turns all URL and email addresses into links. Filters are import in protecting your site and controlling what users are allowed to do when inputting text (and code).

Text formats are used to combine filters. You can have different text formats and each can be with a different combination of enabled filters. You can configure each text format to be used only by users belonging to a particular user role(s), thus allowing you to give more options and privileges to trusted users and fewer to the mass user. Drupal comes prepackaged with three default text formats: Plain text, Filtered HTML, and Full HTML. By default, the plain text format can be used by all users, and as the name suggests allows them to use only plain text (URL and email addresses are turned into links). The filtered HTML format can be used by registered users and it allows them to use a limited set of HTML tags (e.g. <em>, <strong>, etc.); the list of allowed tags can be edited. By default, the full HTML text format can be used only by those belonging to the administrator user role, and it allows them to use all HTML tags.

To manage text formats, log in to the admin panel of your Drupal, click on the Configuration tab and on the page that opens click on Text formats:

Accessing Text Formats Settings

On the page that opens you'll see a table with the existing text formats. To configure a text format or disable it click on the respective buttons in the table. To add a new text format click on the Add text format button; it's above the top left corner of the table:

Adding and Editing Text Formats

The settings are the same both when editing a format and when adding a new one. We'll explore them by adding a new text format. The first field on the create/edit page is for the name of the text format. Just type the desired name. Under it you can select user roles by marking their checkboxes. If a user role is marked, the users belonging to that role will be allowed to use that text format when creating/editing nodes and comments. Below the settings for the user roles you'll find listed the available filters:

Configuring a Text Format-Name, Roles and Filters

The filters that come prepackaged with Drupal are Limit allowed HTML tags, Display any HTML as plain text, Convert line breaks into HTML, Convert URLs into links, Correct faulty and chipped off HTML. The Limit allowed HTML tags filter is used to control which HTML tags can be used with the text format. When this filter is enabled an option will appear which allows you to list the tags that are permitted. If you want all HTML tags to be allowed leave this filter disabled. It should also be disabled, if only plain text is allowed, but in this case the Display any HTML as plain text filter should be enabled; when this filter is enabled only plain text can be used anyway. For example, in the default plain text and full HTML text formats the limit HTML tags filter is disabled, but in the full HTML text format the display HTML as plain text filter is disabled, while in the plain text format it's enabled.

The filter Convert line breaks into HTML helps to keep the paragraph structure of the text, so you can enable that filter regardless of whether the text format allows any HTML tags or not. The Convert URLs into links filter, as the name suggests, turns URL and email addresses into links. This is a useful filter and can be used with both text formats that allow HTML tags and with plain text formats. The filter Correct faulty and chopped off HTML is useful for those text formats that allow HTML tags.

To avoid any confusion it's worth mentioning that the filter limit allowed HTML tags doesn't disable the use of HTML tags; it just gives you the chance to control which tags can be used. The filter that disables HTML tags is display any HTML as plain text.

To enable a filter simply mark its checkbox. The enabled filters will be listed under Filter processing order (the order in which you enable filters doesn't affect the processing order):

Configuring a Text Format-Filter Processing Order

The order in which the filters are listed is the order in which they will be applied to the text before it's displayed on the frontend. You can change the order of the filters by using the four-arrow buttons in front of their names. Just hover the mouse pointer over the button, click and press the left mouse button, and drag and drop the filter in the desired position. Keep in mind that the order in which the filters are applied is important. Some filters may neutralize the effect of others that have been applied before them. For example, if you put the display any HTML as plain text filter after the filters for converting URLs into links and for converting line breaks into HTML, the output on the frontend (articles, comments, etc.) will be a complete mess.

Some third party modules for managing text formats and filters may add more filters from which you can choose when creating/editing text formats.

When you enable some of the filters, this may add more settings that can be configured. From the default filters that come preconfigured with Drupal the only ones that have setting which can be changed are those for limiting the allowed HTML tags and for converting URLs into links. The settings are displayed under the list with the filter processing order. You can navigate between the settings for each filter by clicking on the buttons on the left:

Configuring a Text Format-Filter Settings

In the settings of the filter limit allowed HTML tags there's a text area in which you can list all the allowed tags.

From the settings of the filter for converting URLs into links you can specify the maximum length of links:

Configuring a Text Format-URL Filter Settings

Once you're done with the changes don't forget to click on the Save configuration button at the bottom of the page.

The text formats can be selected before nodes, comments, etc. are published on the frontend. This also depends on whether the particular text format can be used by the user role to which the user belongs. This, as we mentioned, is configured from the settings of the text format. Text formats can be selected, for example, when you (or any user belonging to a role with the appropriate permissions) create/edit nodes. Under the text area for the body of the node there's a Text format drop-down menu from which you can select the format:

Selecting a Text Format When Creating and Editing Nodes

There's the same drop-down menu on the form for submitting comments:

Selecting a Text Format When Submitting Comments