3

On our website, we have a content type ("news"), which includes both public and private items. Normally, posts will be viewable by authenticated users only. However, I want to allow people to post content that is viewed publicly. The way I would prefer to do this is with a simple checkbox during node creation, which allows the node to be public, and viewable for anonymous users.

So far, I've tried the Content Access module, but the interface it provides is too complicated; I only need a checkmark for "allow anonymous to view".

1 Answer 1

2

You can get this to work using a combination of the Rules module and the Content Access module, whereas the Rules module will automatically (behind the scenes) trigger the logic that's needed. The logic itself is entirely hidden for the user (author) and is equivalent to using the interface that you consider too complicated.

Here are some more details to get it to work:

  1. Start from a rule that is similar to the example I included in my answer to "How to specify a Rules condition related to select list values?".

  2. This sample rule uses a field named field_optionlist, with 3 possible values A,B,C. In your case you could use any other fieldname (to fit your naming conventions). And your field only needs 2 values, say Y (= yes, instead of A) and N (= no, instead of B). Some more tips to get you going:

    • Make sure to define this field (for your content type news) BEFORE importing the rule sample in item '1.' above (if not the import of the rule will fail).
    • If you use another machine name for that field, also edit the field name in the sample rule before importing it.
    • Adapt the Rules condition related to "value = C" to "value = Y" (to fit your case), and the message included in the Rules Action.
  3. QA-test the intermediate result after completing step '2.': you should have a Drupal message show up when a news node is created with field value = Y (and not when it is not Y).

  4. Use the integration of the Content Access module with the Rules module to create a Rules Action that updates the access of the node, similar to the Rules Action I included in my sample rule included in my answer to the question "How to revoke edit access to the node's author after publishing a node?". The actual settings to use for the content access module should match with the settings you seemed to already have tried (but which you consider as too complicated to have the node editor set them).

Refinement: if you really want a "checkmark" (instead of such yes/no selection field explained above), create a boolean field and rework the rule detailed above to use that field instead.

Bonus: if you ever need similar logic for another content type, just define the SAME field (with same machine name). After doing so, you should not have to change anything to the Rule as defined above. That's because of the "entity has field" condition ...

4
  • That sounds perfect! I'm going to work on this soon, will accept the answer as soon as it seems to work. One more question, as I'm not entirely sure how Rules works (though I assume this works): When the content is created in any other way than by a User (in this case, by a migration), will the rule still trigger?
    – Erik S
    Commented Dec 6, 2015 at 11:11
  • As a side comment: While I've used Drupal sites with the content access interface before, and haven't had much issues with it then, for this website a large number of people will be able to make posts, without much regularity. Thus, I prefer not to get regular questions about which checks out of the 40 or so they had to check or uncheck.
    – Erik S
    Commented Dec 6, 2015 at 11:13
  • Since the sample rule is using the Rules event "node_insert", it better also gets triggered as part of a migration. If not, then either it's a rules bug, or something wrong in your migration procedure. Which module are you using for such migration? something like migrate_d2d, or feeds? Anyway, let me know after you got it to work, ok? Commented Dec 6, 2015 at 11:16
  • Just migrate, with a custom migrate which extends that module with migrate hooks. I'll see if it works after I update the migration to include news items, but so far, creating and updating public and private items works perfectly. Thanks!
    – Erik S
    Commented Dec 6, 2015 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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