2

I'd like to change the view mode of a node based on the role of a user and another flag in the node, and by using the Rules module.

Since a call to display the node by default is full content, I wrote a rule that on Content is Viewed for that node type, it then checks the conditions and sets data value view-mode to teaser.

However, even when the conditions are met properly the node still displays full text.

From this I am assuming that Content is Viewed is being executed after rendering or am I missing something in my approach?

  • 2
    Nope, you are basically right. Content is Viewed is being executed at the point when altering view mode is pretty much useless. At least if I understand it properly, that is. And don't forget caching - maybe you have node display cached and that's why it is ignored? Why not to simply do it on theme level? – Mołot Jan 14 '14 at 15:49
1

What "Content is viewed" actually means, seems to be like so:

  • The event Content is viewed is what it is: the user has (already) started viewing the content.

  • The (system) event Drupal is initializing is what you should use instead (possible combined with other Events and/or Actions), if you want to trigger some Rules "Action(s)" BEFORE the user can start viewing (= looking at) the content.

For more details, and a working sample (in Rules 'export format'), refer to How to specify a Rules event like "Content is 'going to be' viewed"?

Be aware: this applies to D7 ... and D6 is another story (from the question context I'm not sure if this is fopr D6 or D7 ...).

-1

You might alternatively try the combination of Display Suite and Contextual View Modes (for setting the view mode) and Context (for getting the condition) modules. Obviously this is an overkill for what you and me want to achieve plus for some reason Contextual View Modes doesn't work for me right as I'm getting errors.

If you manage to use rules, please make sure to share the rule you wrote to change the view mode!

Edit: OK this setup actually does work very well and the errors don't interfere with functionality. Regarding your specific problem (I know it's a lot of modules but what the hell):

  • Install Display Suite, Contextual View Mode, Context
  • Set up your custom view modes with display suite
  • Add invisible taxonomy term to content type
  • Make rules trigger a term change in that taxonomy field when node is flagged (this is needed because context module doesn't listen to flags - the sandbox modules intended for that don't work for me either)
  • Create a taxonomy and user role context (as per your requirements)
  • Set up Contextual View Modes module (see module page for instructions)

This should work.

BTW I was using the latest development releases.

-1

This isn't good for performance I guess but try changing the view mode using the rule, then force entity save and redirect to the page again..

I'd consider the other solutions given here or panels with different variants.

-1

For such a simple task don't use Rules. Or any other dependency-heavy module. Getting to the root of your problem, you want to switch the view mode depending on some custom logic. Then all you need is one small snippet placed in a basic custom module: hook_entity_view_mode_alter().

Change the view mode of an entity that is being displayed.

You can easily add any custom logic inside this sample code:

function hook_entity_view_mode_alter(&$view_mode, $context) {

  // Render node in a different view mode when accessed as teaser.
  if ($context['entity_type'] == 'node' && $view_mode == 'teaser') {
    $view_mode = 'teaser_foo';
  }
}

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.