2

I'm adding an event list to a website and I want to limit what anonymous users see based on a field value, a checkbox "Private event". If the checkbox is marked then only a subset of the fields for the given event are shown. Furthermore, after a year of the event I want all fields to become visible to anonymous users. I took a look at the Access Control module but couldn't find a way to have this conditional type of access to fields. Is there another module that I could use?

  • It sounds like your issue is too localized for a module with general application. You should look, instead, at putting your logic into a preprocess function to hide or show the fields based on your logic. Have a look at Setting up variables for use in a template for a starting point. – Triskelion Aug 24 '13 at 19:51
  • @Triskelion It doesn't quite feel right to me to put this in the theme. Sure, usually a site only runs a single theme, but lets say you have one mobile and one 'normal' theme, then you'd have to re-implement this logic in both themes. It feels like this is to much logic/site functionality to be in the presentation layer. – Letharion Aug 24 '13 at 19:58
  • @Letharion Good point. Only the last two layers of preprocess are theme-specific so it could probably be set up as a module_preprocess function. That way it would apply to all themes. However, I like your approach below. – Triskelion Aug 24 '13 at 20:46
  • @Triskelion Ah, you're right of course, I just read template and thought of the theme. :) – Letharion Aug 24 '13 at 23:13
1

I would do like this:

  1. Define a new 'Restricted' View mode. Either manually with hook_entity_info_alter or with Display Suite.
  2. Create a new Page in Page manager, using the by default available Node view.
  3. Create two separate variants in the page.
    1. The first one will use a selection rule on ´Role: Anonymous´ and ´Field Private event: True´. In this Variant, display the node with the 'Restricted' view mode.
    2. The second variant will not need a selection rule, as it will be the fall through option. Here you show the node with the normal 'Full' view mode.

For making everything visible after a year , it sounds like either a hook_cron with some code in it, or perhaps a combination of some Rules with a VBO, which would flip the private field off after a year. Sounds like that might be a separate question though.

  • 1
    Could the selection rule not also test the date, eliminating the need to alter the Field Private Event? – Triskelion Aug 24 '13 at 20:49
  • It absolutely could, but last time I checked the generic per field access checks weren't sophisticated enough to do something like comparing dates, so I believe that would require a custom access plugin. (Or a patch to ctools) – Letharion Aug 24 '13 at 23:16
  • Hi Letharion, thanks for fast reply. If I do what you suggested does this mean that I will have to basically maintain two pages, one for logged in users and another one for anonymous ones? – danielhauagge Aug 26 '13 at 0:02
  • I don't fully understand what you are asking. Technically, you'd be maintaining two "View modes", to differentiate the rendering, and two "Variants", to tell Drupal when to use which rendering. I don't know if that answers your question or not. There's no duplication of work or code, if that's what you mean. – Letharion Aug 26 '13 at 11:18

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.