Use Fields to store text variables, especially those that require text format support

Created on 5 March 2009, over 15 years ago
Updated 16 June 2023, about 1 year ago

A big goal for Drupal 7 is to enable text formats (formerly known as "input formats") on as many textareas as possible, both in Drupal core and contrib. This gives fine-grained control over allowed HTML tags and opens up new possibilities for different ways to input the text, including much better integration with rich text editors (see http://drupal.org/project/wysiwyg).

A list of places in core that don't use text formats but should is at http://groups.drupal.org/node/9072. What's stopping them? It's probably just the fact that dealing with text formats is kind of a pain - you have to figure out how to store the data correctly, display it correctly, etc.

This patch takes the first steps towards making it easy. Since the new Field API in core provides native support for text format handling within text.module, and since it also supports attaching fields to any kind of "object", all we have to do is treat some of the random textareas in Drupal -- that are currently stored in the variable table -- as field-enabled objects, and we get everything else for free! (We also get the benefit of not filling up the variable table with gigantic paragraphs of text that get loaded on every page view, even though they are rarely needed...)

The attached patch is basically working code, although it's still rough around the edges and the exact implementation strategy is certainly open to debate. However, it currently does the following:

  • Adds an API for storing text-format-enabled data that works very similarly to variable_get/set. If you want to store/display any random piece of text with an associated text format, you can simply call functions like drupal_save_text('variable_name', $text, $format), drupal_get_text('variable_name') and drupal_get_raw_text('variable_name'), and Drupal's field system takes care of everything else for you.
  • If you're using system_settings_form(), it gets even easier. If you want to convert a textarea on one of these forms to be text-format-enabled, all you have to do is add this single line of code to the form element:

    #text_format => TRUE,

    Then, when displaying the text, find places in your code that used to be along the lines of this:

    filter_xss(variable_get('variable_name'))

    and replace them with this:

    drupal_get_text('variable_name')

    That's it, no more work required. (Note that this is also a big security improvement, since using this system means you no longer have to remember to sanitize the text yourself every time you display it.)

  • As proof of concept, the attached patch converts a single textarea in Drupal (the site mission statement) to use the new system.

Comments?

πŸ“Œ Task
Status

Closed: outdated

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated 21 minutes ago

Created by

πŸ‡ΊπŸ‡ΈUnited States David_Rothstein

Live updates comments and jobs are added and updated live.
  • wysiwyg

    Involves an in-browser What-You-See-Is-What-You-Get content editor.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024