You are here

function wysiwyg_process_form in Wysiwyg 6.2

Same name and namespace in other branches
  1. 5.2 wysiwyg.module \wysiwyg_process_form()
  2. 5 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 143
Integrates client-side editors with Drupal.

Code

function wysiwyg_process_form(&$form) {
  static $triggered_fields = 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];

      // 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' || $item === 'signature_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]];

          // Allow modules to programmatically enforce no client-side editor by
          // setting the #wysiwyg property to FALSE.
          if (isset($field['#wysiwyg']) && !$field['#wysiwyg']) {

            // A 'format' element should not have any child elements that may
            // need processing, so it should be safe to skip the recursion that
            // happens at the end of this function, and move on to the next
            // element on the same level.
            continue;
          }
          $settings = array(
            'field' => $field['#id'],
          );

          // If this textarea is #resizable the 'none' editor will attach/detach
          // it to avoid hi-jacking the UI.
          if (!empty($field['#resizable'])) {
            $settings['resizable'] = 1;
          }

          // 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]);
          }
          if (count($formats) > 1) {

            // Let the client check the radio buttons for the active format.
            $settings['select'] = $element[reset($formats)]['#name'];
          }
          else {

            // Directly specify which the single available format is.
            $settings['activeFormat'] = reset($formats);
          }
          foreach ($formats as $format_id) {
            $format = 'format' . $format_id;

            // Initialize default settings, defaulting to 'none' editor.
            $settings[$format] = array(
              'editor' => 'none',
              'status' => 1,
              'toggle' => 1,
            );

            // Fetch the profile associated to this input format.
            $profile = wysiwyg_get_profile($format_id);
            if ($profile) {
              $settings[$format]['editor'] = $profile->editor;
              $settings[$format]['status'] = (int) wysiwyg_user_get_status($profile);
              if (isset($profile->preferences['show_toggle'])) {
                $settings[$format]['toggle'] = (int) $profile->preferences['show_toggle'];
              }

              // 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);
            }
          }

          // Store the unaltered content so it can be restored if no changes
          // intentionally made by the user were detected, such as those caused by
          // WYSIWYG editors when initially parsing and loading content.
          if (!empty($field['#value'])) {
            $original = $field['#value'];
            $field['#attributes']['data-wysiwyg-value-original'] = $original;
            $field['#attributes']['data-wysiwyg-value-is-changed'] = 'false';
          }

          // Tag this field to make it easier to find on the client.
          if (empty($field['#attributes']['class'])) {
            $field['#attributes']['class'] = 'wysiwyg';
          }
          else {
            if (!isset($field['#attributes']['class'])) {
              $field['#attributes']['class'] = '';
            }
            $field['#attributes']['class'] .= ' wysiwyg';
          }
          if (!isset($triggered_fields[$field['#id']])) {
            drupal_add_js(array(
              'wysiwyg' => array(
                'triggers' => array(
                  $field['#id'] => $settings,
                ),
              ),
            ), 'setting');
            $triggered_fields[$field['#id']] = TRUE;
          }
        }

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

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