You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现每周定时自动切换同一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.


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 Scheduled and 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 like 0 9 * * 6 wget -O - https://your-site.com/cron.php?cron_key=YOUR_CRON_KEY > /dev/null 2>&1 in 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.
    • Rule 2: Switch Back to Placeholder
      • Trigger: Cron Scheduled set 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.

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 Range and a boolean field like Is Live Event to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 18:12:48