--- filterbynodetype.unpatched.module 2007-06-10 23:55:25.000000000 -0600 +++ filterbynodetype.module 2008-05-06 13:42:06.000000000 -0600 @@ -8,14 +8,14 @@ function filterbynodetype_menu($may_cach $items = array(); if ($may_cache) { - // We can't put this under the existing filters menu, sadly, because the help text added + // We can't put this under the existing filters menu, sadly, because the help text added // by the filters module looks all out of place on our page the. $items[] = array( 'path' => 'admin/settings/filterbynodetype', 'title' => t('Input filter node types'), 'callback' => 'drupal_get_form', 'callback arguments' => array('filterbynodetype_settings'), - 'weight' => 2, + 'weight' => 0, 'access' => user_access('administer filters'), ); } @@ -30,30 +30,99 @@ function filterbynodetype_menu($may_cach function filterbynodetype_form_alter($form_id, &$form) { if (isset($form['type']) && 'node-form' == $form['#id']) { $type = $form['type']['#value']; - $formats =& $form['body_filter']['format']; + // Process body. + if (isset($form['body_filter'])) { + $formats =& $form['body_filter']['format']; + foreach (element_children($formats) as $element) { + if (! variable_get('filterbynodetype_' . $formats[$element]['#return_value'] . '_' . $type, 1)) { + unset($formats[$element]); + } + } + + if (2 == count(element_children($formats))) { // 1 format and 1 extra item for the link + // If there's only one filter left, fold it down to just the description + $formats = $form['body_filter']['format']; + unset($form['body_filter']['format']); + // We don't know what the IDs are of the two fields, so we have to iterate to get them. + foreach (element_children($formats) as $element) { + if (isset($formats[$element]['#title'])) { + // This is a filter, so we assign it to the filter set as the only option. + $form['body_filter']['format'][$element] = array( + '#type' => 'value', + '#value' => $element, + '#parents' => array('format'), + ); + $form['body_filter']['format']['format'] = array( // I have no idea why it uses this structure, but this is what filter.module does. + '#type' => 'item', + '#description' => $formats[$element]['#description'], + ); + } + else { + // It's the guidelines text. + $form['body_filter']['format']['guidelines_link'] = array( + '#value' => $formats[$element]['#value'], + ); + } + } + } + if (1 == count(element_children($formats))) { // 1 extra item for the link, which means there's no filters left + // Do nothing. The form becomes unsubmittable all on its own. + } + } + + // Process cck text fields. + if (module_exists('content') && module_exists('text')) { + $fields = db_query("SELECT field_name FROM {node_field_instance} WHERE type_name = '%s' AND widget_type = '%s'",$type,'text'); + while($field = db_fetch_object($fields)) { + foreach (element_children($form[$field->field_name]) as $element) { + if (isset($form[$field->field_name][$element]['format'])) { + _filterbynodetype_form_alter_add_filter($type, $form[$field->field_name][$element]['format'], $field->field_name, $form); + } + } + } + } + } +} + +/** + * Adds the input filter chooser to a CCK text field + */ +function _filterbynodetype_form_alter_add_filter($type, &$formats, $form_elt_name, &$form) { + + foreach (element_children($formats) as $element) { + if (! variable_get('filterbynodetype_' . $formats[$element]['#return_value'] . '_' . $type, 1)) { + unset($formats[$element]); + } + } + + if (2 == count(element_children($formats))) { // 1 format and 1 extra item for the link. + // We don't know what the IDs are of the two fields, so we have to iterate to get them. foreach (element_children($formats) as $element) { - if (! variable_get('filterbynodetype_' . $formats[$element]['#return_value'] . '_' . $type, 1)) { - unset($formats[$element]); + if (isset($formats[$element]['#title'])) { + $formats['#access'] = false; + $formats[$element]['#default_value'] = $element; + } + else { + // It's the guidelines text. + $form['body_filter']['format']['guidelines_link'] = array( + '#value' => $formats[$element]['#value'], + ); } } + } - if (2 == count(element_children($formats))) { // 1 format and 1 extra item for the link - // If there's only one filter left, fold it down to just the description - $formats = $form['body_filter']['format']; - unset($form['body_filter']['format']); - // We don't know what the IDs are of the two fields, so we have to iterate to get them. + elseif (1 == count(element_children($formats))) { // 1 extra item for the link, which means there's no filters left. + // Do nothing. The form becomes unsubmittable all on its own. + } + else { + // Check if the default input format has not been removed, and then set the first one as default. + $default_filter = variable_get('filter_default_format',0); + if (!isset($formats[$default_filter])) { + $num = false; foreach (element_children($formats) as $element) { if (isset($formats[$element]['#title'])) { - // This is a filter, so we assign it to the filter set as the only option. - $form['body_filter']['format'][$element] = array( - '#type' => 'value', - '#value' => $element, - '#parents' => array('format'), - ); - $form['body_filter']['format']['format'] = array( // I have no idea why it uses this structure, but this is what filter.module does. - '#type' => 'item', - '#description' => $formats[$element]['#description'], - ); + if (!$num) $num = $element; + $formats[$element]['#default_value'] = $num; } else { // It's the guidelines text. @@ -63,23 +132,18 @@ function filterbynodetype_form_alter($fo } } } - if (1 == count(element_children($formats))) { // 1 extra item for the link, which means there's no filters left - // Do nothing. The form becomes unsubmittable all on its own. - } } } - - function filterbynodetype_settings() { - + $form = array(); - + $types = node_get_types(); $formats = filter_formats(); - + // Switch around to be filter-first. Row-major order - + foreach ($formats as $format) { $form['format_' . $format->format] = array( '#type' => 'fieldset', @@ -109,7 +173,7 @@ function theme_filterbynodetype_settings foreach (element_children($form) as $fieldset) { if ('fieldset' != $form[$fieldset]['#type']) continue; // Skip non-fieldset elements - + $row = array($form[$fieldset]['#title']); // The fieldset titles become row labels foreach (element_children($form[$fieldset]) as $element) { if ($first) {