How to create a Drupal rule to check (on cron) a d

2020-02-08 21:56发布

问题:

I'm trying to create a custom rule (using the Rules module) so that every time the cron runs, this rule checks a date field in a custom content type I created. If that date has passed then I want to set a list widget from active to ended.

This is how far I get when trying to create this rule:

  1. Set React on event to Cron maintenance tasks are performed
  2. Add Condition > Set Select the condition to add to Data comparison > Continue

Here is the issue: Data selectors only has site and no access to field data.

Any ideas where I'm going wrong here?

回答1:

The problem with Rules condition "Cron maintenance tasks are performed" is that at that point, there is no access to the node object so any checks/manipulations on the node are not possible. As a solution, instead of Event = Cron maintenance tasks are performed, use Event = Node: Content is viewed. You can leave it open for any content type so that when someone visits the website and opens at least one page, some action will be triggered.



回答2:

You need to create a rules component first:

  1. Go to Rules > Components (admin/config/workflow/rules/components)
  2. Then create a new component and select 'Rule' from select list
  3. Set a name for this component and in the table below select:
    1. Data type: Text Token
    2. Label: A name that you want
    3. Machine name: Use the same name of the label but set here only lower case and underscore
    4. Usage: Parameter
  4. In the component add the condition 'Data comparison' and select node:type
  5. Add other conditions that you want
  6. Set the actions that you want and save
  7. Now go to Rules (admin/config/workflow/rules) and create the rule with action on cron maintenance (as you have already done)
  8. Jump the conditions section and in the actions:
    1. New action: Add a variable
    2. Value: Text
    3. Then write the value of this variable just like the machine name of content type that you want to cycle on (if you want you can change the name and machine name of this variable in the section below)
  9. Now add another action 'Fetch entity by property'
    1. Entity type value: Node
    2. Property value: Type
    3. Data selector: the variable created at the point 8
  10. Now add a loop in parameter list use the variable provided by 'Fetch entity by property'
  11. Add an action in the loop (click on the link to the right of loop row) and select the component created in the point 2 and pass the variable provided by loop

I state: I do not know if it works but at least it should direct you towards the right path

Sorry for my english, I hope you understand everything :)



回答3:

Yes you should be able to get this to work using the Rules module to implement what you're looking for, but I recommend you to also combine that with the Views Rules module. Some details about this module (from its project page):

Provides Views directly as Rules actions and loops to seamlessly use view result data.

The previous quote may seem a bit cryptic (it may make you think like "so what, how can this help me?"). Therefor some more details about how to move forward using these modules:

  1. Create a view (using Views) so that you have 1 Views result (row) with all the nodes you want to be processed (related to your custom content type and if possible filter somehow using your date field). Whereas that view has fields (columns) for whatever is needed in subsequent steps, e.g the node ID, the date field, and possibly other fields as well. You'll need these View fields later on as values to be processed by your rule, "to set a list widget from active to ended*" (as in your question). Important: use a Views display type of "Rules".
  2. Remove that "add condition" (in the custom rule you started) and, instead, use the Views Rules module to iterate over each of these Views results in a Rules action, using the Rules technique known as a "Rules Loop".
  3. For each iteration step in your Rules loop, perform a Rules Action to "do your thing" (= to set a list widget from active to ended). At that point you'll have all data from each column of your Views results available as so called Rules Parameters. So at that point it's a piece of cake to adapt the value of that list widget for the node you're processing in that loop.
  4. Optionally, you may also want to add whatever extra Rules Condition(s), also up to your own imagination. Typically the things you cannot, or have not yet, expressed as a Views filter. However, if you have a choice between using a Views filter and an extra Rules Condition, I recommend to go for the Views filter, because that will reduce the number of your iterations in your Rules loop (performance!).

Easy, no?