2

I know it is really easy to disable comments if your content is organized by content type, but what if it is organized by taxonomy, is it possible to disable comments on all nodes with, say, the term 'news' attached to them? If so, how?

1
  • I'm trying to figure this out as well... – Capi Etheriel Mar 19 '12 at 23:38
3

For anyone not already a seasoned Rules user, here are detailed steps / a worked example for @Wheelz's approach.

This answer shows you how to enable comments when a specific taxonomy term is present and to disable them when that taxonomy term is absent. As per @Wheelz's answer it requires the Rules module. These steps have been tested against 7.x-2.2.

  1. Create a new Rule called, e.g. 'Enable comments when tagged blog'

  2. Add an event of the Before saving content type (use the 'React on event' dropdown and under the subdivision Node, select Before saving content). Save.

  3. Add a condition to test for the presence of the taxonomy field that will control whether comments are allowed.

    • Add a condition of the Entity has field type (use the 'Select the condition to add' dropdown and under the subdivision Entities, select Entity has field)

    • Select node (saved content) from the 'Data selector' dropdown. (Or type node into the 'Data selector' field.) (Don't be tempted by the node:... (saved content) option. It's not what is needed here.) This specifies the type of entity for which you want to check for the presence of a field. In this case we want to check for nodes.

    • Select the field to check for. The dropdown will give you a list of machine names (lower case and with underscores), e.g a field labelled 'Tags' might have the machine name field_tags. (If necessary look up the machine name for the relevant field in your the 'Manage Fields' tab of the relevant content type(s).)

    • Save

  4. Add a condition to test for the taxonomy term that will enable comments.

    • If your taxonomy field can only have one value, then select a condition of the Data comparison type. Otherwise select a condition of the List contains item type.

    • Select the field to test using the 'Data selector' drop down. This is a two step process. First select node:... (saved content). When you do this, the dropdown items change. Select node:<your field's machine name - but with dashes not underscores>, e.g. node:field-tags. (Don't be tempted by node:field-tags:... (Tags) - that's not what is needed here.)

    • Continue

    • If the next page isn't showing a 'Value' dropdown, click on the 'Switch to the direct input mode' button.

    • Use the 'Value' dropdown to select the taxonomy term for enabling comments, e.g. 'Blog'.

    • Save

  5. Add an action that will enable comments

    • Add an action of the 'Set a data value' type.

    • Use the 'Data selector' to select the comment mode, This is a two step process. First select node:... (saved content). When you do this, the dropdown items change. Select node:comment (Comments allowed). Alternatively, you can directly enter node:comment into the 'Data selector' field. If you let you let your mouse hover over `node:comment (Comments allowed)' entry in the dropdown you will see a helpful tip: Whether comments are allowed on this node: 0 = no, 1 = closed (read only), 2 = open (read/write). This helps with the next step.

    • Continue

    • If the next page isn't showing a 'Value' field, click on the 'Switch to the direct input mode' button.

    • Enter 2 into the 'Value' field. This sets comments to 'Open (read/write)'.

The rule just created will switch on comments when the chosen taxonomy term is present. But if that taxonomy term is later removed the comments will still be open. To turn off comments when the taxonomy term is absent, first clone the rule just made and then make a couple of small tweaks.

  1. From the main Rules configuration page, click the ‘clone’ link for the rule you have just created, e.g. 'Enable comments when tagged blog'

  2. Change the name of the rule, e.g. to ‘Disable comments when not tagged blog’. Save changes.

  3. Edit the ‘List contains item’ condition.

    • Check the ‘Negate’ box. If the original condition succeeded, i.e. evaluated as TRUE, when the Tags field included the term ‘Blog’, this modified condition will succeed only when the Tags field does not include that term.

    • Save.

  4. Edit the ‘Set a data value’ action. Replace the value 2 with 0. Save.

This second rule switch off comments when the chosen taxonomy term is absent.

Note: if a 'List contains item' gives an Error: The data type of the configured argument does not match the parameter's list requirement error, then your term reference field is set to allow only one value. You will need to delete the List contains item condition and replace it with a Data comparison condition instead. In the complementary case where Data comparison condition is used when a List contains item condition is needed, errors will be generated when the rule is run and the rule will not work.

An enable rule will look something like this

2

Use Rules to check if a piece of content has a term on 'before saving content' and if so disable comments?

You need to create an action set component to set the node:comment value to 0 then call that as the action in your rule.

1

I ended up copying comment-wrapper.tpl.php to the theme folder, wrapping everything in an if condition that checked for a $skip_all variable. Then, in mytheme_preprocess_comment_wrapper, I checked if $vars['node'] fit the conditions to hide the comments ($node->type was enough for me, but you can check $node->taxonomy). If it did, I set $vars['skip_all'] = TRUE;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.