diff -Naur filterbynodetype/filterbynodetype.module filterbynodetype+comments/filterbynodetype.module --- filterbynodetype/filterbynodetype.module 2008-10-23 07:29:33.000000000 +0200 +++ filterbynodetype+comments/filterbynodetype.module 2008-12-26 13:38:28.203125000 +0100 @@ -15,7 +15,9 @@ * Implementation of hook_perm(). */ function filterbynodetype_perm() { - return array('bypass filter restrictions'); + return array( + 'administer filter restrictions', + 'bypass filter restrictions'); } /** @@ -24,7 +26,7 @@ * Allow users with the necessary permission to change what input formats * are allowed on a give node type. */ -function filterbynodetype_form_node_type_form_alter(&$form, $form_state) { +function _filterbynodetype_form_node_type_form_alter(&$form, $form_state) { $options = array(); foreach (filter_formats() as $format) { @@ -45,6 +47,20 @@ '#access' => user_access('adminster filters'), ); + $comment_defaults = array(); + foreach (filter_formats() as $format) { + $comment_defaults[] = variable_get('filterbynodetype_' . $format->format . '_' . $form['#node_type']->type . '_comment', 1); + } + + $form['comment']['comment_input_formats'] = array( + '#type' => 'checkboxes', + '#title' => t('Allowed input formats'), + '#description' => t('Specify which input formats will be allowed on this node type\'s comments. Note that a user must still have access to the appropriate input format in order to be able to use it.'), + '#options' => $options, + '#default_value' => $comment_defaults, + '#access' => user_access('adminster filters'), + ); + // Attach our submit handler so we can save our extra values. $form['#submit'][] = 'filterbynodetype_node_type_form_submit'; } @@ -58,6 +74,49 @@ $form_values = $form_state['values']; foreach (filter_formats() as $format) { variable_set('filterbynodetype_' . $format->format . '_' . $form_values['type'], $form_values['input_formats'][$format->format]); + variable_set('filterbynodetype_' . $format->format . '_' . $form_values['type'] . '_comment', $form_values['comment_input_formats'][$format->format]); + } +} + +function filterbynodetype_form_alter_format($type, &$container, $suffix) { + + $formats =& $container['format']; + + foreach (element_children($formats) as $element) { + if (isset($formats[$element]['#return_value']) && ! variable_get('filterbynodetype_' . $formats[$element]['#return_value'] . $suffix, 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 = $container['format']; + unset($container['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. + $container['format'][$element] = array( + '#type' => 'value', + '#value' => $element, + '#parents' => array('format'), + ); + $container['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. + $container['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. } } @@ -66,44 +125,22 @@ */ function filterbynodetype_form_alter(&$form, $form_state, $form_id) { + if ($form['#id'] == 'node-type-form' && user_access('administer filter restrictions')) { + _filterbynodetype_form_node_type_form_alter(&$form, $form_state); + } + // For node edit forms, filter out disallowed input formats. - if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id && !user_access('bypass filter restrictions')) { + if ($form['#id'] == 'node-form' && !user_access('bypass filter restrictions')) { $type = $form['type']['#value']; - $formats =& $form['body_field']['format']; - foreach (element_children($formats) as $element) { - if (isset($formats[$element]['#return_value']) && ! variable_get('filterbynodetype_' . $formats[$element]['#return_value'] . '_' . $type, 1)) { - unset($formats[$element]); - } - } + $container =& $form['body_field']; + filterbynodetype_form_alter_format($type, &$container, '_' . $type); + } - 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_field']['format']; - unset($form['body_field']['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_field']['format'][$element] = array( - '#type' => 'value', - '#value' => $element, - '#parents' => array('format'), - ); - $form['body_field']['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_field']['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. - } + // For comment edit forms, filter out disallowed input formats. + if ($form['#id'] == 'comment-form' && !user_access('bypass filter restrictions')) { + $type = node_load($form['nid']['#value'])->type; + + $container =& $form['comment_filter']; + filterbynodetype_form_alter_format($type, &$container, '_' . $type . '_comment'); } }