form_process_text_format
- Versions
- 7
form_process_text_format($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:
<?php
// A simple textarea, such as a node body.
$form['body'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#text_format' => isset($node->format) ? $node->format : filter_default_format(),
);
?>Becomes:
<?php
$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'),
...
),
);
?>And would result in:
<?php
// Original, unaltered form element value.
$form_state['values']['body'] = 'Example content';
// Chosen text format.
$form_state['values']['body_format'] = 1;
?>See also
system_element_info(), filter_form()
Related topics
Code
includes/form.inc, line 2113
<?php
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;
}
?>Login or register to post comments 