Hi,

thanks for this module.
However, I have a problem.
I have 2 input formats on my site; 'Full HTML' and "Wikiformat'.
I want authenticated users to be use 'Full HTML' on the forums and 'Wikiformat' in the wiki.

The problem is that they are able to choose from both input formats in the forums when replying to posts, which I don't want (it'll confuse the poor bastards).

I see no option in this module to restrict formats for comments.

Is this possible somehow?

thanks
Morten

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Crell’s picture

Category: bug » feature

Comments are not currently supported, just node bodies. It's probably possible to extend it to do comments, but I haven't tried doing so.

physiotek’s picture

i would like to have this possibility also.
thanks,
pht3k

momper’s picture

yes - that would be nice ...

greetings momper

tjerah’s picture

The code below should work but needs some refinements.


function filterbynodetype_form_alter($form_id, &$form) {
  if (isset($form['type']) && 'node-form' == $form['#id']) {
    //use the existing code
  }

 // ********* APPLY TO COMMENTS *********************
  if ('comment-form' == $form['#id']) {
    $node = node_load($form['nid']['#value']);
    $type = $node->type;
    $formats =& $form['comment_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['comment_filter']['format'];
      unset($form['comment_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['comment_filter']['format'][$element] = array(
            '#type' => 'value',
            '#value' => $element,
            '#parents' => array('format'),
          );
          $form['comment_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['comment_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.
    }
  }
}

Hope it helps..

Crell’s picture

@tjerah: Can you roll a proper patch? Thanks: http://drupal.org/diffandpatch

dejbar’s picture

@tjerah : Correct me if I'm wrong your but your solution only alters the form so a user can't select a non-allowed filter. This is probably all a lot of sites need however a malicious user could still manually enter another filter option. In most case this level of paranoia probably isn't warranted. But if you were trying to restrict users from PHP filter access on comments or you had a really bad case of persistant link spammers then maybe it could be a problem.

Anyhow I've hacked by own shorter version of this. I'm not using 'Filter by node type' at the moment so this doesn't depend on it.

function mymodule_form_alter($form_id, &$form) {

  if($form_id == 'comment_form') {
      $formats =&  $form['comment_filter']['format'];
      foreach($formats as $i => $f) {
          // delete all filter options apart from filter '1'
          if(is_array($f) && $f['#title'] && ($f['#return_value'] != 1)) {
              unset($formats[$i]);
          }
      }

  }
}

Thanks pointing me in the right direction.

Crell’s picture

This is probably all a lot of sites need however a malicious user could still manually enter another filter option.

Nope. Drupal's Form API, by default, will check to ensure that a submitted value for a select box or radio button is one of the values listed in the form. That was one of its original reasons for existing. If the form, as it exists post form_alter, has only 2 possible radio buttons, then Drupal will reject any other value before it ever gets back to our submit handler. Form API is cool like that. :-)

dejbar’s picture

That is indeed cool. Thanks for letting me know.

alippai’s picture

subscribing

Crell’s picture

Version: 5.x-1.x-dev » 6.x-1.x-dev

New features only accepted against the D6 version. And this still doesn't have a patch.

Richard Blackborder’s picture

Status: Active » Needs review
FileSize
3.54 KB

Here's a patch for a version of the module that handles comments per content type identically to the content types themselves. I've used some of the code from the module and the code from tjerah's post, and modified both slightly.

Richard Blackborder’s picture

Actually, that patch doesn't apply the permissions settings. Here's an updated patch.

Crell’s picture

Status: Needs review » Needs work

Haven't looked at the code yet, but it doesn't make sense, IMO, to restrict comments to the same format as the node. That makes sense only for forums, but not for anything else. It should either be all comments period, or comments-per-nodetype. The latter is probably more flexible.

Richard Blackborder’s picture

Status: Needs work » Needs review

There's a bit of misunderstanding here. Reading my post again, I can see why, but the patch gives comments-per-nodetype.

Here's what the form layout looks like (list items for checkboxes):

Allowed input formats:

  • Filtered HTML
  • Full HTML
  • Filtered HTML with Javascript tags
  • BBCode

Specify which input formats will be allowed 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.

Allowed input formats (comments):

  • Purified HTML
  • Full HTML
  • Filtered HTML with Javascript tags
  • BBCode

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.
Crell’s picture

Ah! OK, good, that's what we should do. :-) I'll see if I can review this soon, but, um, no promises. (Anyone else who wants to jump in here and review the patch, feel free.)

batbug2’s picture

Status: Needs review » Needs work

applied patch from #12, see no changes at all in the node type settings form

well scratch that. this was due to http://drupal.org/node/326373,
new options do show up, settings work fine. approved.

batbug2’s picture

Status: Needs work » Reviewed & tested by the community

changing the title

batbug2’s picture

Status: Reviewed & tested by the community » Needs work

well, thinking about that - you should apply the fix from http://drupal.org/node/326373 to you patch also. It's just one letter :)

batbug2’s picture

Oh, forgot, one more thing
this line

return if user_access('bypass filter restrictions');

gives php error

Richard Blackborder’s picture

Status: Needs work » Needs review
FileSize
4.52 KB

Here's the updated version.

skizzo’s picture

will this feature be backported to D5?

Richard Blackborder’s picture

Personally I only learned D6 coding, so it won't be backported by me, sorry.

Crell’s picture

The D5 version is in support mode now, so I won't be adding new features to it. I don't have enough spare bandwidth to work on new features for both versions.

I'll try to test this patch as soon as I get the chance. Others are welcome and encouraged to do so in the meantime. :-)

gdd’s picture

Status: Needs review » Needs work

I gave this patch a run-through today, and it works well although I have some comments on the code and implementation

- This patch also fixes a typo bug (the role 'administer filters' is mis-spelled in filterbynodetype_form_node_type) which has already been fixed and committed in #326373: Non-uid 1 cannot set configuration

- I think it makes more sense to put the comment input filter settings under 'Comment Settings' rather than in 'Submission Form Settings' with the other choices.

- There is a lot of repeated code here and I'm thinking there should be a way to break it out into a function call for the sake of maintainability.

Otherwise this is great and well-needed.

Richard Blackborder’s picture

Thank you for reviewing the patch heyrocker.

- This patch also fixes a typo bug (the role 'administer filters' is mis-spelled in filterbynodetype_form_node_type) which has already been fixed and committed in #326373: Non-uid 1 cannot set configuration

Previous reviewers asked for that change to be included in this patch. I remade the patch to do so. I do not know which way to go with this now.

Crell’s picture

The dev release of this module already has that bugfix, so the patch here won't apply if you try to fix it again. Don't fix what's already been fixed. :-)

Richard Blackborder’s picture

Okay, I have attempted to address these issues:

- I think it makes more sense to put the comment input filter settings under 'Comment Settings' rather than in 'Submission Form Settings' with the other choices.

I agree, but I don't think it is possible. The comment module uses hook_form_alter, just like filterbynodetype does. comment.module thus overwrites the comment section with a blank fieldset element, erasing any fields which filterbynodetype may have added to it. Going by this, there's nothing I can do about this.

- There is a lot of repeated code here and I'm thinking there should be a way to break it out into a function call for the sake of maintainability.

I've done that in this new version of the patch. I didn't move everything inside the if blocks, but trying to move all the code out would require a lot more if statements and probably create more maintenance worries.

Crell’s picture

We can always push the module weight up to be comment.module +5 or something. That can be done in the installer and in an update hook.

Richard Blackborder’s picture

Status: Needs work » Needs review
FileSize
1.2 KB
8.1 KB

I've applied that technique, but I also had to move some of the code around, so this patch is now bigger than either module file. I've also patched the install file, which allowed me to add variable clean up to filterbynodetype_uninstall() as well.

Summit’s picture

Subscribing, greetings, Martijn

TripleEmcoder’s picture

Hello,

I made a patch to support comments before finding this discussion. It does solve some the most important issues you mention here, however, so I submitted it anyway. It's against 6.x-1.2, so a made a separate issue. I needed it in production, so I didn't even think about working with 6.x-dev - sorry about that!

Anyway, please check out #351313: Comment support, maybe it could be useful.

Marcin

Richard Blackborder’s picture

Through my own use I did find a bug with this patch and I fixed it.

jrglasgow’s picture

the patches in #32 are working for me.

I have re-rolled them though into one file.

jrglasgow’s picture

I found some typos in a comment or two, those have been fixed in this patch

Richard Blackborder’s picture

Thanks jrglasgow.

I've been using the old version for about 3 months, and this new version for about a week now with no problems.

With that plus jrglasgow's review, would it be appropriate to status change this to "reviewed & tested by the community"?

Maybe even... get it into the module?

jrglasgow’s picture

Status: Needs review » Reviewed & tested by the community

I feel that it is in fine working order. I think it should be considered tested

gdd’s picture

Status: Reviewed & tested by the community » Closed (fixed)

This patch has been committed, thanks all for your help and patience.

I'm going to do some slight code cleanup and roll a new release later today.

Crell’s picture

Status: Closed (fixed) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.