Hiding the CKEditor

02/05/2017

This snippet disables the rich text in Drupal 7 for all the fields of type text or large text.  Additionally, it disables the CKEditor toolbar like the selection of the text format:

/**
 * Implements hook_form_alter().
 */
function module_form_alter(&$form, &$form_state, $form_id) {
  // Fields should have hidden the CKEditor.
  $fields = field_read_fields(array(
    'type' => array('text_long', 'text_with_summary'),
  ));
  callback_hide_wysiwyg($form, $fields);
}

/**
 * Hide CKEditor appeareance.
 *
 * Callback for module_form_alter().
 *
 * @param $form
 * @param $fields
 *   Fields with CKEditor that have to be disabled.
 *
 * @ingroup callbacks
 */
function callback_hide_wysiwyg(&$form, $fields) {
  foreach ($fields as $key => $value) {
    // Hide CKEditor appeareance.
    if (isset($form[$key])) {
      $form[$key][LANGUAGE_NONE][0]['#wysiwyg'] = FALSE;
      $form[$key]['#after_build'][] = 'callback_hide_text_format';
    }
  }
}

/**
 * Hide CKEditor text format.
 *
 * Callback for module_form_alter().
 *
 * @param $form
 *
 * @return mixed
 *   Same form without access to text formats.
 *
 * @ingroup callbacks
 */
function callback_hide_text_format(&$form) {
  // Hide CKEditor toolbar.
  $form[LANGUAGE_NONE][0]['format']['#access'] = FALSE;

  return $form;
}

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.