You are here

function wysiwyg_process_form in Wysiwyg 5

Same name and namespace in other branches
  1. 5.2 wysiwyg.module \wysiwyg_process_form()
  2. 6.2 wysiwyg.module \wysiwyg_process_form()
  3. 6 wysiwyg.module \wysiwyg_process_form()

Process a textarea for Wysiwyg Editor.

This way, we can recurse into the form and search for certain, hard-coded elements that have been added by filter_form(). If an input format selector or input format guidelines element is found, we assume that the preceding element is the corresponding textarea and use it's #id for attaching client-side editors.

See also

wysiwyg_elements(), filter_form()

1 string reference to 'wysiwyg_process_form'
wysiwyg_form_alter in ./wysiwyg.module
Implementation of hook_form_alter().

File

./wysiwyg.module, line 79
Integrate client-side editors with Drupal.

Code

function wysiwyg_process_form(&$form) {

  // 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];

      // 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]];

          // Disable #resizable to avoid resizable behavior to hi-jack the UI,
          // but load the behavior, so the 'none' editor can attach/detach it.
          $extra_class = '';
          if (!isset($field['#resizable']) || !empty($field['#resizable'])) {

            // Due to our CSS class parsing, we can add arbitrary parameters
            // for each input format.
            $extra_class = ' wysiwyg-resizable-1';
            $field['#resizable'] = FALSE;
            drupal_add_js('misc/textarea.js');
          }

          // 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);
          array_pop($formats);
          if (($key = array_search('format', $formats)) !== FALSE) {
            unset($formats[$key]);
          }
          foreach ($formats as $format) {

            // Default to 'none' editor (Drupal's default behaviors).
            $editor = 'none';
            $status = 1;

            // Fetch the profile associated to this input format.
            $profile = wysiwyg_get_profile($format);
            if ($profile) {
              $editor = $profile->editor;
              $status = (int) wysiwyg_user_get_status($profile);

              // Check editor theme (and reset it if not/no longer available).
              $theme = wysiwyg_get_editor_themes($profile, isset($profile->settings['theme']) ? $profile->settings['theme'] : '');

              // Add plugin settings (first) for this input format.
              wysiwyg_add_plugin_settings($profile);

              // Add profile settings for this input format.
              wysiwyg_add_editor_settings($profile, $theme);
            }

            // Use a prefix/suffix for a single input format, or attach to input
            // format selector radio buttons.
            if (isset($element['format']['guidelines'])) {
              $element['format']['guidelines']['#prefix'] = '<div class="wysiwyg wysiwyg-format-' . $format . ' wysiwyg-editor-' . $editor . ' wysiwyg-field-' . $field['#id'] . ' wysiwyg-status-' . $status . $extra_class . '">';
              $element['format']['guidelines']['#suffix'] = '</div>';

              // Edge-case: Default format contains no input filters.
              if (empty($element['format']['guidelines']['#value'])) {
                $element['format']['guidelines']['#value'] = ' ';
              }
            }
            else {
              if (isset($element[$format]['#attributes']['class'])) {
                $element[$format]['#attributes']['class'] .= ' ';
              }
              else {
                $element[$format]['#attributes']['class'] = '';
              }
              $element[$format]['#attributes']['class'] .= 'wysiwyg wysiwyg-format-' . $format . ' wysiwyg-editor-' . $editor . ' wysiwyg-field-' . $field['#id'] . ' wysiwyg-status-' . $status . $extra_class;
            }
          }
        }

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

      // Recurse into children.
      wysiwyg_process_form($element);
    }
  }
  return $form;
}