You are here

function ckeditor_process_form in CKEditor - WYSIWYG HTML editor 6

1 string reference to 'ckeditor_process_form'
ckeditor_elements in ./ckeditor.module
Implementation of hook_elements(). Replace the textarea with CKEditor using a callback function (ckeditor_process_textarea)

File

./ckeditor.module, line 168
CKEditor - The text editor for the Internet - http://ckeditor.com Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.

Code

function ckeditor_process_form(&$form) {
  global $_ckeditor_configuration, $_ckeditor_ids;
  static $processed_textareas = array();
  static $found_textareas = array();

  //Skip if:

  // - we're not editing an element
  // - ckeditor is not enabled (configuration is empty)
  if (arg(1) == "add" || arg(1) == "reply" || !count($_ckeditor_configuration)) {
    return $form;
  }
  $ckeditor_filters = array();

  // Iterate over element children; resetting array keys to access last index.
  if ($children = array_values(element_children($form))) {
    foreach ($children as $index => $item) {
      $element =& $form[$item];
      if (isset($element['#id']) && in_array($element['#id'], $_ckeditor_ids)) {
        $found_textareas[$element['#id']] =& $element;
      }

      // filter_form() always uses the key 'format'. We need a type-agnostic
      // match to prevent FALSE positives. Also, there must have been at least
      // one element on this level.
      if ($item === 'format' && $index > 0) {

        // Make sure we either match a input format selector or input format
        // guidelines (displayed if user has access to one input format only).
        if (isset($element['#type']) && $element['#type'] == 'fieldset' || isset($element['format']['guidelines'])) {

          // The element before this element is the target form field.
          $field =& $form[$children[$index - 1]];
          $textarea_id = $field['#id'];
          array_push($processed_textareas, $textarea_id);

          //search for checkxss1/2 class
          if (empty($field['#attributes']['class']) || strpos($field['#attributes']['class'], "checkxss") === FALSE) {
            continue;
          }

          // Determine the available input formats. The last child element is a
          // link to "More information about formatting options". When only one
          // input format is displayed, we also have to remove formatting
          // guidelines, stored in the child 'format'.
          $formats = element_children($element);
          foreach ($formats as $format_id) {
            $format = !empty($element[$format_id]['#default_value']) ? $element[$format_id]['#default_value'] : $element[$format_id]['#value'];
            break;
          }
          $enabled = filter_list_format($format);
          $ckeditor_filters = array();

          //loop through all enabled filters
          foreach ($enabled as $id => $filter) {

            //but use only that one selected in CKEditor profile
            if (in_array($id, array_keys($_ckeditor_configuration[$textarea_id]['filters'])) && $_ckeditor_configuration[$textarea_id]['filters'][$id]) {
              if (!isset($ckeditor_filters[$textarea_id])) {
                $ckeditor_filters[$textarea_id] = array();
              }
              $ckeditor_filters[$textarea_id][] = $id . "/" . $format;
            }
          }
          drupal_add_js(array(
            'ckeditor' => array(
              'settings' => array(
                $textarea_id => array(
                  'input_format' => $format,
                ),
              ),
            ),
          ), 'setting');

          //No filters assigned, remove xss class
          if (empty($ckeditor_filters[$textarea_id])) {
            $field['#attributes']['class'] = preg_replace("/checkxss(1|2)/", "", $field['#attributes']['class']);
          }
          else {
            $field['#attributes']['class'] = strtr($field['#attributes']['class'], array(
              "checkxss1" => "filterxss1",
              "checkxss2" => "filterxss2",
            ));
          }
          array_pop($formats);
          unset($formats['format']);
        }

        // If this element is 'format', do not recurse further.
        continue;
      }

      // Recurse into children.
      ckeditor_process_form($element);
    }
  }

  //We're in a form
  if (isset($form['#action'])) {

    //some textareas associated with CKEditor has not been processed
    if (count($processed_textareas) < count($_ckeditor_ids)) {

      //loop through all found textfields
      foreach (array_keys($found_textareas) as $id) {
        $element =& $found_textareas[$id];

        //if not processed yet (checkxss class is before final processing)
        if (strpos($element['#attributes']['class'], "checkxss") !== FALSE && !in_array($element['#id'], $processed_textareas) && !empty($_ckeditor_configuration[$id]['filters']) && array_sum($_ckeditor_configuration[$id]['filters'])) {

          //assign default Filtered HTML to be safe on fields that do not have input format assigned, but only if at least one security filter is enabled in Security settings
          $ckeditor_filters[$element['#id']][] = "filter/0/1";
          $element['#attributes']['class'] = strtr($element['#attributes']['class'], array(
            "checkxss1" => "filterxss1",
            "checkxss2" => "filterxss2",
          ));
        }
      }
    }
  }
  if (!empty($ckeditor_filters)) {
    foreach ($ckeditor_filters as $id => $filters) {
      $arr['settings'][$id]['filters'] = $filters;
    }
    drupal_add_js(array(
      'ckeditor' => $arr,
    ), 'setting');
  }
  return $form;
}