2

I'm using Advanced Poll module and it has a nice "Time management" feature. We can schedule when to open or close the poll.

I want to send an email notification to the administrator one day prior to poll close date. So that, admin gets notified and create an another poll.

Is there any way to make this possible with Rules module or hooks?

2

You will need to create a Rule that will schedule component evaluation (send email).

CREATE COMPONENT

  1. Create component plugin of type Rule (admin/config/workflow/rules/components/add) - name it "Notify admin about poll closure".
  2. Provide a variable for the component to act on. Variable Data type = Content, Label - Poll Node, Machine name = poll_node, Usage = Parameter.

screenshot

  1. Add Conditions if you need any
  2. Add Actions -> Send HTML e-mail. Set you email parameters below is the export of the component

    { "rules_notify_admin_about_poll_closure" : {
        "LABEL" : "Notify admin about poll closure",
        "PLUGIN" : "rule",
        "OWNER" : "rules",
        "REQUIRES" : [ "rules_i18n", "rules", "mimemail" ],
        "USES VARIABLES" : { "poll_node" : { "label" : "Poll Node", "type" : "node" } },
        "DO" : [
          { "mimemail" : {
              "key" : "poll_closure_notify_admin",
              "to" : "adminemail@email.com",
              "subject" : "Poll will close tomorrow",
              "body" : "Poll [poll-node:title] will close tomorrow",
              "plaintext" : [ "" ],
              "language" : [ "" ]
            }
          }
        ]
      }
    }  
    

CREATE RULE

  1. React on Events: "After saving new content of type Advanced Poll", "After updating existing content of type Advanced Poll"
  2. Add Conditions "Entity has field": Entity: node, Field: advpoll_dates
  3. Add action "Schedule component evaluation" - select the component that you created "Notify admin about poll closure".

    Scheduled Evaluation date -> node:advpoll-dates:value2. Add offset "-1 days". This will schedule the email 1 day before poll closing.

    Set an identifier to something like "Poll closure notification: [node:nid] - [node:title]". It should have unique values everytime new scheduled task is added in queue, that is why you should use node:title and node:nid.

    Set Poll Node field to node.

    Save your rule and test.

    { "rules_schedule_notification_about_poll_closure" : {
    "LABEL" : "Schedule notification about poll closure",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_scheduler" ],
    "ON" : {
      "node_insert--advpoll" : { "bundle" : "advpoll" },
      "node_update--advpoll" : { "bundle" : "advpoll" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "advpoll_dates" } }
    ],
    "DO" : [
      { "schedule" : {
          "component" : "rules_notify_admin_about_poll_closure",
          "date" : {
            "select" : "node:advpoll-dates:value2",
            "date_offset" : { "value" : -86400 }
          },
          "identifier" : "Poll closure notification: [node:nid]",
          "param_poll_node" : [ "node" ]
        }
      }
    ]}}
    

I provided an export of the component and the rule that you can use and modify to your preference. Hope this helps.

P.S. Cron should be enabled and configured on your website in order for scheduled events to work properly.

  • That"s indeed the way to go. However, I suggest you also add a "schedule_delete" action in your (last) rule. Have a look at "Step 3" of my answer to "drupal.stackexchange.com/questions/166809/…" for an example of what I mean. More specifically the part in the Rules Action that starts with "schedule_delete". – Pierre.Vriens Feb 5 '17 at 16:15
  • @Pierre.Vriens -the idea of scheduler_delete is smart, thank you for mentioning this. Although in this particular case I find it not necessary. Scheduler overrides events that have same identifier. E.g. "identifier" : "Poll closure notification: [node:nid] - [node:title]". In this case only one event with this identifier can be scheduled because identifier contains unique value of node:nid. – oksana-c Feb 9 '17 at 11:06
  • Any other Rule that will attempt scheduling a task with the same identifier will simply override the previous existing task (provided Rule is triggered on Poll node create and Poll node update events). Of course if the [node:title] changes - that may throw the Scheduler off a bit and it will definitely create a duplicate entry. So to improve this answer further and cover the scenario of duplicated scheduled tasks, I'd recommend only removing [node:title] from the identifier value (updated the answer to remove node:title). – oksana-c Feb 9 '17 at 11:06
  • Now that you removed that "title" part from the identifier, I do 100% agree that indeed in this case there is no more need for such (prior) "schedule_delete" (as in my first comment). It was exactly that title-part in your identifier that made me wonder about such potential duplicate entries I'd try to prevent. Up to you, but maybe you should integrate your comments in your answer also ... I think (comments may get deleted at any time by those who have the auth to do so ...). – Pierre.Vriens Feb 9 '17 at 15:01

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.