Lesson 10.1: Workflows and triggers for developers
Workflows let code run around kernel operations: before content is published, after a user registers, before an order is confirmed. Before learners write an event type of their own (lesson 10.2), they need the model: workflows, events, triggers and the processes that wait. This lesson builds a small workflow in the administration and follows it through the kernel.
At a glance
| Course | Developers |
| Module | Module 10: Workflows, events and cronjobs |
| Duration | 45 minutes (20 presenting, 25 exercise) |
| Level | Intermediate |
| Audience | Developers |
| Prerequisites | Module 9; an administrator account on the training installation |
Learning objectives
After this lesson, learners can:
- explain workflow, event, trigger, workflow process and memento;
- create a workflow and connect it to
content/publishbefore with Setup > Triggers; - explain the difference between a before and an after trigger;
- describe how a waiting process is resumed by the workflow cronjob (
workflow.phpin the frequent part); - find waiting processes on Setup > Workflow processes.
Trainer's notes
How to present it
Source: Workflows: approvals, triggers and the processes that wait.
- The model (5 minutes). Project the table of section 1 of the guide: a workflow is a list of events; a trigger
connects a workflow to an operation (
content/publish, before or after); a process is one run for one object and version; the memento is what the kernel saved of the held-back operation. - The flow (5 minutes). Send for publishing starts
content/publish; the before trigger starts a process; an event that cannot finish answers "run me again later" and the version waits as pending; the workflow cronjob runs waiting processes again and finishes the operation. - Before or after (2 minutes). Before holds the operation until the workflow is done; after runs once the operation happened, for notifications and follow-up work.
- One workflow per trigger (2 minutes). Several steps go into one workflow; the Multiplexer event runs other workflows by section or class.
- The cronjob (3 minutes). Without
workflow.phprunning, approved content is never published. The process list shows whether it is scheduled.
Common questions
Which operations have triggers? Setup > Triggers lists every operation that can start a workflow, in words, with the module and the connection type.
Can a workflow run for one class only? The Multiplexer event chooses a workflow by section, class or user; an event of your own can check the class itself (lesson 10.2).
Where does the waiting version show for the editor? As pending, under My pending items.
Pitfalls
- A before-publish workflow on a shared training installation. Every learner's publishing goes through it. Use separate installations or remove the trigger at the end.
- No workflow cronjob. Processes wait for ever; check the status bar of the process list.
- Cancelling processes. Cancel... turns a pending version back into a draft; it deletes no content, but tell the editor.
Hands-on exercise
On the training installation, signed in to the administration as an administrator.
- Open Setup > Workflows, open a workflow group (or create one with New workflow group).
- Press New workflow, name it
Course: wait until date. - Choose Event / Wait until date in the list of events and press Add event; press OK to store the workflow without further settings.
- Open Setup > Triggers. Find the row for publishing content, before (
content/publish), and note which workflow it runs now. Choose your workflow and apply the change. - Publish an article in your course folder. Note whether it is published at once.
- Open Setup > Workflow processes and look for a process; read the status bar about the workflow cronjob.
- Run the workflow cronjob once from the shell:
php runcronjobs.php -s <sa> --script=workflow.php. - Set the trigger back to the workflow it ran before step 4 (or none) and apply.
- Read the code that ends a process step:
grep -n "STATUS_" kernel/classes/ezworkflowtype.php | head -12.
Expected result
- Step 5: the article is published at once, because the Wait until date event without a date attribute set lets it through; with dates configured it would wait.
- Step 6: an empty process list is normal when nothing waits; the status bar names the frequent part.
- Step 7 runs
workflow.phpand ends without errors. - Step 9 lists the status constants such as
STATUS_ACCEPTED,STATUS_REJECTEDandSTATUS_DEFERRED_TO_CRON. - The trainer checks that every learner set the trigger back.
Quiz
- What connects a workflow to an operation?
- A workflow must decide before content is visible. Which connection type? A. after B. before C. either D. none, use a cronjob
- What is a memento?
- What happens to approved content when the workflow cronjob does not run?
- Where do you see processes that wait?
Answers
- A trigger.
- B. Before: the operation is held back until the workflow is done.
- What the kernel saved of the held-back operation (module, operation, trigger and parameters) so it can carry it out later.
- It is never published; the processes wait.
- Setup > Workflow processes (
/workflow/processlist).
Further reading
- Workflows: approvals, triggers and the processes that wait.
- Cronjobs: running, scheduling and following them.
- My drafts and my pending items.
Related pages
- Module 10: Workflows, events and cronjobs
- Developers: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.