Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.370 diff -u -p -r1.370 form.inc --- includes/form.inc 5 Sep 2009 15:05:01 -0000 1.370 +++ includes/form.inc 9 Sep 2009 01:53:15 -0000 @@ -1884,107 +1884,6 @@ function form_process_radios($element) { } /** - * Add text format selector to text elements with the #text_format property. - * - * The #text_format property should be the ID of an text format, found in - * {filter_format}.format, which gets passed to filter_form(). - * - * If the property #text_format is set, the form element will be expanded into - * two separate form elements, one holding the content of the element, and the - * other holding the text format selector. The original element is shifted into - * a child element, but is otherwise unaltered, so that the format selector is - * at the same level as the text field which it affects. - * - * For example: - * @code - * // A simple textarea, such as a node body. - * $form['body'] = array( - * '#type' => 'textarea', - * '#title' => t('Body'), - * '#text_format' => isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT, - * ); - * @endcode - * - * Becomes: - * @code - * $form['body'] = array( - * // Type switches to 'markup', as we're only interested in submitting the child elements. - * '#type' => 'markup', - * // 'value' holds the original element. - * 'value' => array( - * '#type' => 'textarea', - * '#title' => t('Body'), - * '#parents' => array('body'), - * ), - * // 'format' holds the text format selector. - * 'format' => array( - * '#parents' => array('body_format'), - * ... - * ), - * ); - * @endcode - * - * And would result in: - * @code - * // Original, unaltered form element value. - * $form_state['values']['body'] = 'Example content'; - * // Chosen text format. - * $form_state['values']['body_format'] = 1; - * @endcode - * - * @see system_elements(), filter_form() - */ -function form_process_text_format($element) { - if (isset($element['#text_format'])) { - // Determine the form element parents and element name to use for the input - // format widget. This simulates the 'element' and 'element_format' pair of - // parents that filter_form() expects. - $element_parents = $element['#parents']; - $element_name = array_pop($element_parents); - $element_parents[] = $element_name . '_format'; - - // We need to break references, otherwise form_builder recurses infinitely. - $element['value'] = (array)$element; - $element['value']['#weight'] = 0; - unset($element['value']['#description']); - $element['#type'] = 'markup'; - $element['#theme'] = NULL; - $element['#theme_wrappers'] = array('text_format_wrapper'); - $element['format'] = filter_form($element['#text_format'], 1, $element_parents); - - // We need to clear the #text_format from the new child otherwise we - // would get into an infinite loop. - unset($element['value']['#text_format']); - } - return $element; -} - -/** - * Theme a text format form element. - * - * @param element - * An associative array containing the properties of the element. - * Properties used: #children, #description - * @return - * A string representing the form element. - * - * @ingroup themeable - */ -function theme_text_format_wrapper($element) { - $output = '
' . "\n"; - - $output .= $element['#children'] . "\n"; - - if (!empty($element['#description'])) { - $output .= '
' . $element['#description'] . "
\n"; - } - - $output .= "
\n"; - - return $output; -} - -/** * Theme a checkbox form element. * * @param $element Index: modules/filter/filter.module =================================================================== RCS file: /cvs/drupal/drupal/modules/filter/filter.module,v retrieving revision 1.287 diff -u -p -r1.287 filter.module --- modules/filter/filter.module 30 Aug 2009 06:04:09 -0000 1.287 +++ modules/filter/filter.module 9 Sep 2009 02:21:40 -0000 @@ -67,6 +67,28 @@ function filter_theme() { } /** + * Implement of hook_element_info_alter(). + * + * Elements defined by Filter module are wrappers around the respective, + * non-prefixed elements, extended by additionally required properties for + * filter_process_format(). + * + * For better maintenance, stub element types are inherited from their + * respective, original element types. + * + * @see filter_process_format() + */ +function filter_element_info_alter(&$type) { + $type['textfield_format'] = $type['textfield']; + $type['textfield_format']['#format'] = FILTER_FORMAT_DEFAULT; + array_unshift($type['textfield_format']['#process'], 'filter_process_format'); + + $type['textarea_format'] = $type['textarea']; + $type['textarea_format']['#format'] = FILTER_FORMAT_DEFAULT; + array_unshift($type['textfield_format']['#process'], 'filter_process_format'); +} + +/** * Implement hook_menu(). */ function filter_menu() { @@ -589,28 +611,102 @@ function check_markup($text, $format = F /** * Generate a selector for choosing a format in a form. * - * @ingroup forms - * @param $selected_format - * The ID of the format that is currently selected. + * The form element will be expanded into two separate form elements, one + * holding the content of the element, and the other holding the text format + * selector. The original element is shifted into a child element, but is + * otherwise unaltered, so that the format selector is at the same level as the + * text field which it affects. + * + * The optional #format property should be the ID of an text format, found in + * {filter_format}.format. By default, FILTER_FORMAT_DEFAULT is used. + * + * For example: + * @code + * // A simple textarea, such as a node body. + * $form['body'] = array( + * '#type' => 'textarea_format', + * '#title' => t('Body'), + * '#format' => isset($node->format) ? $node->format : NULL, + * ); + * @endcode + * + * Becomes: + * @code + * $form['body'] = array( + * // Type switches to 'markup', as we're only interested in submitting the child elements. + * '#type' => 'markup', + * // 'value' holds the original element. + * 'value' => array( + * '#type' => 'textarea', + * '#title' => t('Body'), + * '#parents' => array('body'), + * ), + * // 'format' holds the text format selector. + * 'format' => array( + * '#parents' => array('body_format'), + * ... + * ), + * ); + * @endcode + * + * And would result in: + * @code + * // Original, unaltered form element value. + * $form_state['values']['body'] = 'Example content'; + * // Chosen text format. + * $form_state['values']['body_format'] = 1; + * @endcode + * * @param $weight * The weight of the text format. * @param $parents - * Required when defining multiple text formats on a single node or having a different parent than 'format'. + * Required when defining multiple text formats on a single node or having a + * different parent than 'format'. + * * @return * HTML for the form element. + * + * @ingroup forms */ -function filter_form($selected_format = FILTER_FORMAT_DEFAULT, $weight = NULL, $parents = array('format')) { - $selected_format = filter_resolve_format($selected_format); +function filter_process_format($element) { + // Determine the form element parents and element name to use for the text + // format widget. + if (isset($element['#format_parents']) { + $parents = $element['#format_parents']; + } + else { + $parents = $element['#parents']; + $element_name = array_pop($parents); + $parents[] = $element_name . '_format'; + } + + // Inject the real form element widget, f.e. a textarea. + // We need to break references, otherwise form_builder() recurses infinitely. + $element['value'] = (array)$element; + $element['value']['#type'] = str_replace('_format', '', $element['value']['#type']); + $element['value']['#weight'] = 0; + // The form element's description is handled in theme_text_format_wrapper(). + unset($element['value']['#description']); + // We need to clear the #process from the new child, otherwise we would run + // into an infinite loop. + unset($element['value']['#process'][array_search('filter_process_format', $element['value']['#process'])]); + + // Make the stub element output the text format wrapper instead. + $element['#type'] = 'markup'; + $element['#theme'] = NULL; + $element['#theme_wrappers'] = array('text_format_wrapper'); + + $selected_format = filter_resolve_format($element['#format']); $formats = filter_formats(); - drupal_add_js('misc/form.js'); - drupal_add_css(drupal_get_path('module', 'filter') . '/filter.css'); $element_id = form_clean_id('edit-' . implode('-', $parents)); $form = array( '#type' => 'fieldset', - '#weight' => $weight, + '#weight' => isset($element['#weight']) ? $element['#weight'] : NULL, '#attributes' => array('class' => array('filter-wrapper')), + '#attached_js' => array('misc/form.js'), + '#attached_css' => array(drupal_get_path('module', 'filter') . '/filter.css'), ); $form['format_guidelines'] = array( '#prefix' => '
', @@ -640,7 +736,30 @@ function filter_form($selected_format = '#weight' => 1, ); - return $form; + $element['format'] = $form; + + return $element; +} + +/** + * Theme a text format form element. + * + * @param $element + * An associative array containing the properties of the element. + * Properties used: #children, #description + * @return + * A string representing the form element. + * + * @ingroup themeable + */ +function theme_text_format_wrapper($element) { + $output = '
' . $element['#children']; + if (!empty($element['#description'])) { + $output .= '
' . $element['#description'] . '
'; + } + $output .= "
\n"; + + return $output; } /** Index: modules/system/system.module =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.module,v retrieving revision 1.785 diff -u -p -r1.785 system.module --- modules/system/system.module 5 Sep 2009 15:05:04 -0000 1.785 +++ modules/system/system.module 9 Sep 2009 00:39:45 -0000 @@ -338,7 +338,7 @@ function system_elements() { '#size' => 60, '#maxlength' => 128, '#autocomplete_path' => FALSE, - '#process' => array('form_process_text_format', 'ajax_process_form'), + '#process' => array('ajax_process_form'), '#theme' => 'textfield', '#theme_wrappers' => array('form_element'), ); @@ -363,7 +363,7 @@ function system_elements() { '#cols' => 60, '#rows' => 5, '#resizable' => TRUE, - '#process' => array('form_process_text_format', 'ajax_process_form'), + '#process' => array('ajax_process_form'), '#theme' => 'textarea', '#theme_wrappers' => array('form_element'), );