Drupal 7: Hide text format information using the form api

Published by Lennart Van Vaerenbergh on November 24, 2017

Because it's not so well documented on the web, here's a small example of how to hide the text format switcher and information that, sometimes unwillingly, appears below the textarea. More specifically, how to hide it when you are using a form element of type 'text_format' with the Drupal form API. 
Text format options

Here's the code example showing a custom form and an after build callback to alter the form element after it got the format options applied to it.
 * Form callback; Builds our test form.
function custom_module_my_form($form, &$form_state) {
  $text = variable_get('my_text', array('value' => '', 'format' => ''));

  $form['my_text'] = array(
    '#type' => 'text_format',
    '#title' => t('My text'),
    '#default_value' => $text['value'],
    '#format' => $text['format'],
    '#after_build' => array('custom_module_hide_format_info'),

  return system_settings_form($form);

 * After build callback for text_format element.
 * Place this in a .module file or so, the function can be used
 * by any form text_format element.
function custom_module_hide_format_info($element) {
  if (isset($element['format'])) {
    $element['format']['#access'] = FALSE;

    // You can also hide specific parts if you comment out
    // the line above.
    $element['format']['format']['#access'] = FALSE;
    $element['format']['guidelines']['#access'] = FALSE;
    $element['format']['help']['#access'] = FALSE;

  return $element;

For entity and node fields, there are several other options out there in the form of contrib modules:
That's it. Happy formatting!

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
This challenge is for testing whether or not you are a human visitor and to prevent automated spam submissions.