--- filterbynodetype.module.old 2011-08-04 13:13:06.000000000 -0400 +++ filterbynodetype.module 2011-08-04 13:13:20.000000000 -0400 @@ -36,6 +36,15 @@ $defaults[] = variable_get('filterbynodetype_' . $format->format . '_' . $form['#node_type']->type, 1); } + $default_options = array(); + $i = 0; + foreach($options as $format => $name) { + if ($defaults[$i]) { + $default_options[$format] = $name; + } + $i++; + } + $form['submission']['input_formats'] = array( '#type' => 'checkboxes', '#title' => t('Allowed input formats'), @@ -45,6 +54,15 @@ '#access' => user_access('administer filters'), ); + $form['submission']['default_input_format'] = array( + '#type' => 'radios', + '#title' => t('Default input format'), + '#description' => t('Specify which input formats will be used by default on this node type\'s body field. Note that a user must still have access to the appropriate input format in order to be able to use it.'), + '#options' => $default_options, + '#default_value' => variable_get('filterbynodetype_default_' . $form['#node_type']->type, ''), + '#access' => user_access('administer filters'), + ); + // Attach our submit handler so we can save our extra values. $form['#submit'][] = 'filterbynodetype_node_type_form_submit'; } @@ -59,6 +77,7 @@ foreach (filter_formats() as $format) { variable_set('filterbynodetype_' . $format->format . '_' . $form_values['type'], $form_values['input_formats'][$format->format]); } + variable_set('filterbynodetype_default_' . $form_values['type'], $form_values['default_input_format']); } /** @@ -67,43 +86,56 @@ function filterbynodetype_form_alter(&$form, $form_state, $form_id) { // 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')) { - $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]); - } - } - - 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. + if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) { + if (!user_access('bypass filter restrictions')) { + $type = $form['type']['#value']; + $formats =& $form['body_field']['format']; 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'], - ); + if (isset($formats[$element]['#return_value']) && ! variable_get('filterbynodetype_' . $formats[$element]['#return_value'] . '_' . $type, 1)) { + unset($formats[$element]); } - else { - // It's the guidelines text. - $form['body_field']['format']['guidelines_link'] = array( - '#value' => $formats[$element]['#value'], - ); + if (empty($form['#node']->nid)) { + // only set the default on the node/add form + $formats[$element]['#default_value'] = variable_get('filterbynodetype_default_' . $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. + } } - 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. + else if (user_access('bypass filter restrictions') && empty($form['#node']->nid)) { + $type = $form['type']['#value']; + $formats =& $form['body_field']['format']; + foreach (element_children($formats) as $element) { + $formats[$element]['#default_value'] = variable_get('filterbynodetype_default_' . $type, ''); + } } } }