Can't query a view with contextual filter on cron run

Created on 6 May 2023, over 1 year ago
Updated 8 May 2023, over 1 year ago

I'm trying to create an ECA model, that will loop through all created by user comments, and depending on rating in that comment, will add to author some bonuses.
However the case, is that, I can't manage to query the view (with all user comments) specifically for author of the comments.

The reason why it's not working, is because it switches user to admin, and loops only through comments created by admin, but if i remove "Switch user" action, view won't even query, because of permission issue (as I know). I have tried to pass contextual filter argument to view query action, but it wasn't working either.

I have view with comments, that has contextual filter "Comment: Author uid" (photo below), and I have this ECA model created.

So my question is: Is it possible to query this kind of view with contextual filter? And if possible, then how to do it correctly?

💬 Support request
Status

Fixed

Version

1.1

Component

Documentation

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Etzesty
  • 🇩🇪Germany jurgenhaas Gottmadingen

    When using the switch user action then the current user (i.e. the logged in user) for that Drupal session is that user you switched to.

    The contextual filter on the uid is correct, but it's default value should not be the current user, but the raw value 1. Then, your ECA model should provide the user ID as the argument to the view. Then you receive the comments for that user.

  • Still can't get it to work.
    My ECA model must check every user, not only specific one, but because "Views: Execute query" action is not firing, I need to switch user to specific user.
    (For example user1 posted several comments and user2 posted several comments as well. When cron is executing, i need to check comments from user1 and depending on calculation only from his comments, give him bonus. And I need to do the same action with user2)
    This ECA is working with any other event, but not working with "Cron event".
    (Added my ECA model, maybe it'll be easier to understand my goal here).

  • Little bit thinking and I figured why it's not working like that.
    When cron executes, current user is with id 0, and then it just doesn't know from where to load users.
    So, I think, I need to load authors firstly, and after that load comments.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    So, I think, I need to load authors firstly, and after that load comments.

    That's exactly it. Your first view should provide a list of users that have commented something. Then, your ECA model loops through all those users and loads their comments. With them, you can finally do your magic for each user and their comment(s).

  • Yeah, got it working smoothly.
    Thank you @jurgenhaas for responses and appreciate your work on this module )

  • Status changed to Fixed over 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024