| 5 filter.module | filter_form($value = FILTER_FORMAT_DEFAULT, $weight = NULL, $parents = array('format')) |
| 6 filter.module | filter_form($value = FILTER_FORMAT_DEFAULT, $weight = NULL, $parents = array('format')) |
Generates a selector for choosing a format in a form.
Parameters
$value: The ID of the format that is currently selected; uses the default format if not provided.
$weight: The weight of the form element within the form.
$parents: The parents array of the element. Required when defining multiple text formats on a single form or having a different parent than 'format'.
Return value
Form API array for the form element.
See also
Related topics
File
- modules/
filter/ filter.module, line 488 - Framework for handling filtering of content.
Code
<?php
function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = NULL, $parents = array('format')) {
$value = filter_resolve_format($value);
$formats = filter_formats();
$extra = theme('filter_tips_more_info');
if (count($formats) > 1) {
$form = array(
'#type' => 'fieldset',
'#title' => t('Input format'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => $weight,
'#element_validate' => array('filter_form_validate'),
);
// Multiple formats available: display radio buttons with tips.
foreach ($formats as $format) {
// Generate the parents as the autogenerator does, so we will have a
// unique id for each radio button.
$parents_for_id = array_merge($parents, array($format->format));
$form[$format->format] = array(
'#type' => 'radio',
'#title' => $format->name,
'#default_value' => $value,
'#return_value' => $format->format,
'#parents' => $parents,
'#description' => theme('filter_tips', _filter_tips($format->format, FALSE)),
'#id' => form_clean_id('edit-' . implode('-', $parents_for_id)),
);
}
}
else {
// Only one format available: use a hidden form item and only show tips.
$format = array_shift($formats);
$form[$format->format] = array(
'#type' => 'value',
'#value' => $format->format,
'#parents' => $parents,
);
$tips = _filter_tips(variable_get('filter_default_format', 1), FALSE);
$form['format']['guidelines'] = array(
'#title' => t('Formatting guidelines'),
'#value' => theme('filter_tips', $tips, FALSE, $extra),
);
}
$form[] = array('#value' => $extra);
return $form;
}
?> Login or register to post comments
Comments
How to add multiple input formats inside your module
To have a multiple textareas with input formats available for more than one textarea in your module, the input format, when declared, needs to know its parent(s), so it can look for a textarea inside that scope. When you have more than one textarea and need an input format, consider doing the following:
<?php
// first textarea
$form['scope1']['scope1.1']['text'] = array('#type' => 'textarea',
'#title' => t('A title'),
'#weight' => 2,
); // notice weight == 2
$form['scope1']['scope1.1']['format'] = filter_form(2, 3, array('scope1','scope1.1')); // to integrate wysiwyg
// second textarea
$form['scope2']['scope2.1']['text'] = array('#type' => 'textarea',
'#title' => t('A title'),
'#weight' => 8,
); // notice weight == 8
$form['scope2']['scope2.1']['format'] = filter_form(2, 9, array('scope2','scope2.1')); // to integrate wysiwyg
?>
What you need to watch here are the weights. For your input format to appear under the designated textarea, it needs to have the next consecutive weight.
Enjoy!
does setting #tree on the form change your example?
My form has the tree attribute set:
<?php
$form['#tree']=true;
?>
And I found I had to make my filter_form call a little differently, where you had:
<?php$form['scope2']['scope2.1']['format'] = filter_form(2, 9, array('scope2','scope2.1'));
?>
I had to set it to something like:
<?php$form['scope2']['scope2.1']['format'] = filter_form(2, 9, array('scope2','scope2.1','format'));
?>
or else my $form_state got spurious values in the related textarea field.
D7 Equivalent
What's the equivalent function for this in Drupal 7?
It looks like you use
It looks like you use filter_process_format in Drupal 7: http://api.drupal.org/api/drupal/modules--filter--filter.module/function...
I can't see how that operates
I can't see how that operates as an equivelent, or at least the documentation is obscure about it.
new for element
It doesn't work the same. In fact they are a new for element "text_format", see http://drupal.org/update/modules/6/7#text_format