4

Building a D7 site, I need to be able to automatically enable/disable Panel panes and/or views (or in the worst case scenario blocks) based on the time of day - different contact info for out-of-office hours and some other stuff. I also need to display drastically different sets of panes and views on the frontpage based on the day of week.

My temporary solution is to set up a number of Panels pages (for changing panes/views) and duplicate themes (for changing blocks) and run cronjobs with Drush's variable-set to change frontpage from one Panel page to another and to change theme to the one with different blocks enabled.

As simple as the above is, it's a terrible long-term solution because all other, unrelated changes made to the site then often require being made more than once and it's really hard to keep track of.

So is there a more straightforward way to configure per-pane, per-view or per-block setting to get enabled/disabled based on time of day / day of week?

  • Answering my own question - it appears that Views supports Drush and I can enable/disable specific views through my cronjobs. That mostly solves my problem. – Stan Mar 5 '12 at 0:29
2

Oddly enough, it doesn't look like any of the Date modules integrate with ctools for selection rules.

I think you have two options, that are essentially the same thing.

  • Panel page variants support PHP code selection rules.
  • Panels panes support PHP code visibility rules.

So, your page(s) could have variants based on date/time, and each variant could have panes that are only visible based on date/time.

Your PHP selection rules will use time and/or date. For example, this should set a rule to only select or be visible on Monday.

$now = time();
$day = date("D", $now);
return $day == "Mon";

The date function does default to the current time, but explicitly using a $now parameter can prevent problems when you call date multiple times when you cross midnight.

  • 1
    Thank you, this solved my problem with visibility of a login pane to a webshop with specific opening hours! I love Drupal and all the helpful people around it! – user9878 Sep 10 '12 at 11:07
0

If you're willing to use the "Rules" module, then you should be able to also get this to work for your "worst case scenario blocks" (as in your question). Provided you can express your actual "logic" (about the actual time-frames?) in appropriate Rules logic.

More details about doing so are explained below.

Part 1: Rules block visibility

Have a look at the Rules block visibility module. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless:

  • Need to show a block only for users registered more than a month ago?

  • Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

  • What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

With that, and as per the "if you can do it with Rules, you can use it to control block visibility" above, you've reduced your question to making Rules implement the "actual time-frames" logic you asked about.

Part 2: Create an appropriate Rules Component

The Rules block visibility module doesn't have a lot of documentation, except in the README.txt that comes with this module. Here is what the crucial part of it is (to get the idea):

Configuration is done on a per-block basis. To control a block visibility using a rule component, go to the block settings page, scroll down to the "Rules" tab, and select the Rules component that you want to use.

Notice that to be able to be used by this module, a Rules component must be constructed in a very specific way. See the next section for more information.

This module comes with 2 sample Rules Components, that should help to understand the concept behind it.

By looking at these samples, I was able to create another Rules Component which looks like so:

{ "rules_block_visibility_hide_blocks" : {
    "LABEL" : "Hide blocks",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "module" : { "label" : "Module", "type" : "text" },
      "delta" : { "label" : "Delta", "type" : "text" },
      "result" : { "label" : "Result", "type" : "boolean", "parameter" : false }
    },
    "IF" : [
      { "data_is" : {
          "data" : [ "site:current-date" ],
          "op" : "\u003E",
          "value" : 1485351000
        }
      },
      { "data_is" : {
          "data" : [ "site:current-date" ],
          "op" : "\u003C",
          "value" : 1485365400
        }
      }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}

You should be able to import this rule in your own environment.

What this Rules Component does is "only" return a boolean (1 or 0), based on the condition if the site's current time (in GMT) is between 2017-01-25 14:30:00 and 2017-01-25 18:30:00.

With that, we're coming close to an actual answer to your question, as detailed in Part 3 below.

Part 3:

Head over to the "Block" settings for the block you want to conditionally hide. At the bottom of it's settings, within the typical "Visibility settings", there is now (after you enabled the Rules block visibility module as in Part 1) an extra tab labeled "Rules". Using that tab will allow you to select a "Rules Component" that will do what's documented below that selection list field, ie: "Show this block only if the selected rule returns a positive value. Important: to be listed here, a block visibility rule must have specific parameters and return values.".

And guess what, in my case I just selected my Rules Component from Part 2.

Note: it doesn't matter what type of block it is (created by a view, or something else), it works in all cases.

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.