如何实现每周定时自动切换同一URL内容?Drupal环境方案咨询
Drupal 定时自动切换虚拟开放活动内容方案(无需手动干预)
Hey Dave, let’s walk through the best no-manual-effort solutions for your Drupal site to swap between placeholder content and your virtual open house live stream on a fixed weekly schedule, then switch back automatically 2 hours after the event ends.
1. Low-Code Option: Core + Rules Module (Recommended)
This is the easiest approach if you don’t want to write custom code:
- First, create two content types:
Virtual Open House Live– add fields for your live stream embed code, event details, etc.Placeholder Content– set up your default static content for non-event times.
- Install and enable the Rules module, then build two rules:
- Rule 1: Switch to Live Content
- Trigger: Use
Cron Scheduledand set it to your weekly start time (e.g., every Saturday at 9 AM). Pro tip: Make sure your server’s cron is configured to run Drupal’s cron regularly (every 15 mins works best) – use a command like0 9 * * 6 wget -O - https://your-site.com/cron.php?cron_key=YOUR_CRON_KEY > /dev/null 2>&1in your server’s crontab. - Action: Load your live content node, then set your target URL’s content to this node (if using a custom page) or switch a View’s display to show the live content.
- Trigger: Use
- Rule 2: Switch Back to Placeholder
- Trigger:
Cron Scheduledset to 2 hours after your event ends (e.g., Saturday at 7 PM if the event wraps at 5 PM). - Action: Load your placeholder node and replace the target URL’s content with it.
- Trigger:
- Rule 1: Switch to Live Content
2. Views-Based Auto-Filtering
If you prefer using Views for content display:
- Create a single View that includes both your live event content and placeholder content. Add fields for
Event Time Rangeand a boolean field likeIs Live Eventto tag your live content. - Add a time-based filter to the View:
- Configure it to show the live content only when the current time falls within your weekly event window. For all other times, it’ll automatically fall back to the placeholder content.
- Embed this View onto your fixed URL page. Just make sure to set the View’s cache to
Time-based(e.g., 5-minute cache) so it refreshes often enough to catch the schedule switch.
3. Custom Module (For Full Control)
If you have dev skills and need more flexibility, a tiny custom module gets the job done:
- Create a custom module, then define a route for your fixed URL (e.g.,
/virtual-open-house). - In the page callback function, check the current time against your schedule and load the appropriate content:
function my_open_house_module_page() { $current_time = \Drupal::time()->getCurrentTime(); $current_day = date('w', $current_time); // 6 = Saturday $current_hour = date('H', $current_time); // Define your schedule: Sat 9AM-5PM = live, 5PM-7PM = post-event (adjust as needed), rest = placeholder if ($current_day == 6 && $current_hour >= 9 && $current_hour < 17) { $content_node = \Drupal\node\Entity\Node::load(123); // Replace with your live node ID } elseif ($current_day == 6 && $current_hour >= 17 && $current_hour < 19) { // Optional: Show a "event just ended" message or replay, else use placeholder $content_node = \Drupal\node\Entity\Node::load(456); // Placeholder node ID } else { $content_node = \Drupal\node\Entity\Node::load(456); } return \Drupal::entityTypeManager()->getViewBuilder('node')->view($content_node); } - Enable the module, and your URL will automatically swap content based on the schedule.
Critical Notes to Avoid Issues
- Cron Reliability: If using Rules, ensure Drupal’s cron runs frequently (every 15 mins) so schedule triggers fire on time.
- Cache Bypass: If your site uses page caching, exclude your target URL from caching or set a short cache TTL (e.g., 5 mins) so users see the updated content immediately.
- Test First: Use Drush to simulate time changes and verify switches work:
drush php-eval "\Drupal::service('datetime.time')->setTime(strtotime('2024-05-18 09:00'));"
内容的提问来源于stack exchange,提问作者Dave Thackeray
相关产品推荐
相关产品推荐

