Looking to schedule content for your Drupal 8 site? With Drupal 7, there are a few options available such as the Scheduler module or a custom-built solution. One of the main issues with Scheduler module is that it’s not very flexible, meaning the only scheduled operations that could be performed are to either publish or unpublish a node.
In Drupal 8, Scheduler module is still around, although still limited to only publish or unpublish operations. Of course, there is always the option to custom build a solution, but I’ve recently come across an even better alternative that provides a flexible way to schedule content changes. The Acquia-funded contributed module is called Scheduled Updates. In a nutshell, it allows you to perform scheduled operations on essentially any entity field values at a specific date/time. Another nice feature is that it allows you to configure multiple updates, such as publish a node at a specific date/time and then unpublish it at a later date/time.
To install and configure the module, follow the following steps:
Step 1: Download, install and enable
Download, install, and enable the Scheduled Updates module.
In a Drupal 8 composer workflow, the module should be added to your site’s composer.json and installed during the `composer install`.
The module can be enabled via Drush, such as `drush en scheduled_updates -y` or via the module list page.
Step 2: Configure Scheduled Updates module
Visit the configuration page under Configuration → Workflow.
For this example, we will be configuring a scheduled job to automatically set an Article node to `promoted` at a specific date/time. This will demonstrate a popular use case that is possible by this module.
- Click on Scheduled Update Types
- Click on Add Scheduled update type
- Give it a label of “Article scheduled promote”
- Choose `Entity Type` of `Content`
- Set `Update Field` to be `Promoted to front page`
- For `Default Value and Date Only Updates”, check the box for `Promoted to front page`. (This tells Drupal that when the scheduled update runs, that it should give the Article node a Promoted status). If we wanted, for instance, to unpromote an Article node, we would uncheck the box for “Promoted to front page”. Also, check the box for `Hide this field for a date only update.
- In the Update Reference Options, choose the entity bundle that will be affected by this configuration. We will just check Article.
- Under `Reference Field Options`, choose `Create new reference fields to enter updates.`.
- Add a label for the field that will be added to the node, such as `Promote on`.
- The `Update Limit` can stay at the default
- Select an option for `Update Runner`. In most cases, `Latest Revision` is the best option.
- Click Save.
Step 3: Manage Fields
If we go to the Article `Manage Fields` page, we can see a new field was created.
Step 4: Manage Form Display
If we go over to the Article `Manage Form Display` page, there are a few options, but for this tutorial, we can leave them unchanged.
Step 5: Create scheduled content
Now, we can create a new Article node and see the Scheduled Updates in action.
- Click the “Add new Promote on” button
- Enter a date and time when we want the node set to `Promoted to front page`.
- Note, with most browsers, there is a really nice date picker for date/time selection.
- Click “Create Promote on”.
Now that the scheduled update has been created, save the node.
It will be set to the `promoted` status once the scheduled update job runs during the next cron run.
Step 6: Perform scheduled content updates during cron run
The next time cron runs, we will get this message:
We can also see that the node is now set to the Promoted status on the node edit page.
Drupal is not guaranteed to run cron. Assuming Drupal has cron set to run every 3 hours, at the specified interval, such as 3 hours, cron will run cron every 3 hours if pages are being hit on your site, meaning if your site is getting enough traffic to get the pages hit that often.
To work around this limitation, and to make sure Scheduled Updates (or Scheduler for that matter) works properly, cron can be set on the server, assuming the server allows that sort of access. Depending on the timing and frequency of your updates, that will decide what cron setting you should configure on the server.
For example, if you create scheduled content on 30-minute increments (one at 12:30 PM, two at 3:00 PM, three at 4:30 PM), you’ll most likely want to have the server automatically run cron every 30 minutes to be sure that cron is run at the right time to kick off the scheduled update.
You can find further information at these resources:
In closing, I hope this blog post demonstrated the benefits of the Scheduled Updates module, such as it’s flexibility with entity field operations, robustness in adding multiple scheduled updates on a single node, and the ease of configuration and user experience.