Implementation example of text_format form element on a custom admin page

Last updated on 14/09/2016

See the Form Api reference for all options.

mymodule/mymodule.module
/**
 * Implements hook_menu().
 */
function mymodule_menu() {
  $items['print/mymodule/content/here'] = [
    'title' => 'My Module',
    'page callback' => 'mymodule_page',
    'access callback' => TRUE,
    'description' => 'My Module description',
    'type' => MENU_NORMAL_ITEM,
  ];
 
  $items['admin/config/system/mymodule_config_page'] = [
    'title' => 'My Module content',
    'description' => 'Configure My Module content.',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['mymodule_admin_settings_form'],
    'access arguments' => ['administer site configuration'],
    'type' => MENU_NORMAL_ITEM,
    'file' => 'mymodule.admin.inc',
  ];
 
  return $items;
}
 
/**
 * Page callback for print/my/content/here route.
 */
function mymodule_page() {
  $content = variable_get('mymodule_page_content', ['value' => '']);
  return nl2br($content['value']);
}
mymodule/mymodule.admin.inc
/**
 * Admin page callback for mymodule module.
 */
function mymodule_admin_settings_form() {
  $content = variable_get('mymodule_page_content', '');
  $form['mymodule_page_content'] = [
    '#type' => 'text_format',
    '#format' => !empty($content['format']) ? $content['format'] : 'plain_text',
    '#title' => t('My module page content'),
    '#default_value' => !empty($content['value']) ? $content['value'] : '',
    '#rows' => 20,
  ];
 
  // If you want to hide the text format.
  // $form['#after_build'][] = 'mymodule_admin_settings_form_form_after_build';
 
  return system_settings_form($form);
}
 
function mymodule_admin_settings_form_form_after_build(&$form) {
  $form['additions']['format']['#access'] = FALSE;
  return $form;
}