A while ago I got Availability Calendar to work just fine. Went on to other things.

Recently, I've been using Page Theme module to theme panel pages with unique URLs. Today, I started to add some content to a content type not related to any specially themed pages. When I edited a month, say, February 25th, "FULLY BOOKED" and clicked save, I would receive an error upon viewing the saved page:

warning: Invalid argument supplied for foreach() in /Users/Cale/Sites/acquia-drupal/sites/all/modules/availability_calendars/availability_calendars.module on line 258.

I was using an older dev version, so I upgraded to 6x.1x-dev today. Error went away as I was able to edit Availability Calendar content successfully, BUT then noticed, after enabling this module, every page I had themed using Page Theme module immediately shut down and reverted back to my default theme.

I have made no changes to the Availability Calendar. I even tried with the 6x-1.5 version.. But again, my Page Theme module breaks down.

I don't know have a clue what's doing this but these seem two very unrelated modules. However, I cannot use this module if it is breaking down my Page Theme pages. Any help?

Thanks

CommentFileSizeAuthor
#3 1076786.patch2.21 KBfietserwin

Comments

calefilm’s picture


257  db_query('DELETE FROM {availability_calendars_week} WHERE nid = %d AND year = %d AND month = %d', $nid, $year, $month);
258  foreach ($notes as $week => $note) {
259  db_query('INSERT INTO {availability_calendars_week} (nid, year, month, week, note) VALUES (%d, %d, %d, %d, "%s")', $nid, $year, $month, $week, $note);
calefilm’s picture

MY FIX:

I don't know what I'm doing but the older dev version wasn't disabling my themes. So I went into the old availability_calendars.module file at availability_calendars/availability_calendars.module and replaced line 16 in the new file.

OLD


function availability_calendars_init() {
  // css is added to all pages, since blocks will need to make use of it
  // base css
  drupal_add_css(drupal_get_path('module', 'availability_calendars') . '/availability_calendars.css');
  // current theme info
  $theme_data = _system_theme_data(); // array containing info about all themes
  $theme_key = array_pop(explode('/', path_to_theme())); // get the currently enabled theme's namespace, can't find a better way to do this
  $theme = $theme_data[$theme_key]; // object containing all info about current theme
  $theme_bases = $theme->base_themes; // array of current theme's base themes

  // themes we will perform specific theming on
  $core_themes = array('garland', 'bluemarine', 'chameleon', 'marvin', 'pushbutton');
  $base_themes = array('fusion_core', 'tao', 'zen', 'ninesixty');

  // check to see if we are using a core theme
  if (in_array($theme_key, array_merge($core_themes, $base_themes))) $add_css = TRUE;
  // check whether the theme being used is a subtheme of one of our themed base themes
  foreach ($base_themes as $key) {
    if (is_array($theme_bases)) {
      if (array_key_exists($key, $theme_bases)) {
        $theme_key = $key; // if the current theme is a subtheme of one of our base themes then use the base theme's namespace instead of our enabled theme's
        $add_css = TRUE;
        break;
      }
    }
  }
  if ($add_css) drupal_add_css(drupal_get_path('module', 'availability_calendars') . '/css/' . $theme_key . '.css');
}

NEW


function availability_calendars_init() {
  // css is added to all pages, since blocks will need to make use of it
  drupal_add_css(drupal_get_path('module', 'availability_calendars') .'/availability_calendars.css');
}

Now, I got my themes back. And the error is gone.

fietserwin’s picture

Status: Active » Needs review
StatusFileSize
new2.21 KB

On porting the code to D7, I found that the hook_init() did contain some code that the original developer himself had doubts about (comment: can't find a better way to do this). So I looked for a better way of adding the css and came up with the following code for D7:

old: see #2

new

function availability_calendars_init() {
  // css is added to all pages, since blocks will need to make use of it
  drupal_add_css(drupal_get_path('module', 'availability_calendars') .'/availability_calendars.css');

  $current_theme = variable_get('theme_default', 'none');

  // themes we will perform specific theming on
  $core_themes = array('garland', 'bluemarine', 'chameleon', 'marvin', 'pushbutton');
  $base_themes = array('fusion_core', 'tao', 'zen', 'ninesixty');

  $add_css = FALSE;
  // check to see if we are using a core theme
  if (in_array($current_theme, array_merge($core_themes, $base_themes))) {
    $add_css = TRUE;
  }
  else {
    // check whether the theme being used is a subtheme of one of our themed base themes
    $themes = list_themes();
    $theme = $themes[$current_theme];
    while (!empty($theme->base_theme)) {
      $theme = $themes[$theme->base_theme];
      if (in_array($theme->name, $base_themes)) {
        $add_css = TRUE;
        break;
      }
    }
  }
  if ($add_css) {
    drupal_add_css(drupal_get_path('module', 'availability_calendars') . '/css/' . $theme_key . '.css');
  }

Can you test and review this patch?

nicholas.alipaz’s picture

fietserwin, the developer here. I have an issue with what you are doing via variable_get(). This does not account for when users are allowed to select their own theme for the site. Any ideas about that? We need to determine which theme is actually in use.

nicholas.alipaz’s picture

Status: Needs review » Needs work
fietserwin’s picture

You're right, but then I guess this can't be known in the init hook. The theme has not yet been initialized (when I was debugging through that piece of code I saw that our call to list_themes() was the first (the static inside had not yet been initialized). Moreover other modules exist that can change the theme in use on a per page/user or whatever basis.

So I think if you really want to solve this, you have to do it in a hook that's later in the process (or assure you're the last to init, as I guess that the choice of theme will be made in the init phase).

Some links:
- http://drupal.org/node/224333#custom_theme
- http://api.drupal.org/api/drupal/developer--globals.php/global/custom_th...

fietserwin’s picture

For D7 it turns out that theme initializing is done before hook_init (guess why). So, we may assume that in hook_init we have available the globals $theme_info and $base_theme_info. So for D7 I was able to rewrite this part cleanly:

  // Theme specific css
  $add_theme_specific_css = FALSE;

  // Themes we will perform specific theming on
  $core_themes = array('garland', 'bluemarine', 'chameleon', 'marvin', 'pushbutton');
  $base_themes = array('fusion_core', 'tao', 'zen', 'ninesixty');

  // Check to see if we are using a core theme
  global $theme_info, $base_theme_info;
  $theme = $theme_info;
  if (in_array($theme->name, array_merge($core_themes, $base_themes))) {
    $add_theme_specific_css = TRUE;
  }
  else {
    // Check whether the theme being used is a subtheme of one of our themed base themes
    while (!empty($theme->base_theme)) {
      $theme = $base_theme_info[$theme->base_theme];
      if (in_array($theme->name, $base_themes)) {
        $add_theme_specific_css = TRUE;
        break;
      }
    }
  }
  if ($add_theme_specific_css) {
    drupal_add_css(drupal_get_path('module', 'availability_calendars') . '/css/' . $theme->name . '.css', array('every_page' => TRUE));
  }

D6 has the same globals, but we can not assume that they have been set in hook_init, However in a theme function they will be set. So,in D6, we could move this code to the theme_availability_calendars_node() function. In that case, it will not be rendered as often, only when necessary, so it may lead to another aggregation. How bad is that?

fietserwin’s picture

Status: Needs work » Closed (won't fix)

Considering:
- the difficulties we have in determining the actual theme that will be used on a given page (see comments #3, #4, #6, and #7)
- the fact that in the 6.x-2.x branch this problem has been removed by rewriting the styling stuff, so we don't do any theme specific includes anymore
- the fact that this errors only surfaces in some configurations (no single theme in use; modules changing theme in the init phase, ...???)

I am going to close this call as a won't fix.

Note that the 6.x-2.x branch will *not* suffer from this error. Thus if you are experiencing this issue, please upgrade to the 6.x-2.x branch (and don't forget to run update.php).