In case there's a Context OG Action 'Set Group Context', og_theme_custom_theme is invoked before it (returning empty og_context()), thus the context has not been applied yet. How can I enforce the theme to be set after all the contexts have been triggered?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

agentrickard’s picture

Status: Active » Needs review
FileSize
717 bytes

As far as I can tell, there is a very nasty cache bug in og_context() that sometimes causes the page to be cached with the wrong theme.

This admittedly ugly patch works around the problem by invalidating the page cache until og_context() sends a proper value.

agentrickard’s picture

agentrickard’s picture

Status: Needs review » Needs work

In my case, I've tracked this down to a problem in entity_cache. If the parent group node is not in {cache_entity_node}, the lookup sometimes fails.

I wonder if this is a problem with entity_metadata_wrapper().

agentrickard’s picture

That's not it. But on cache clear, menu_get_custom_theme() is returning NULL.

agentrickard’s picture

After thorough research, I don't think this is a context issue. It looks like a race condition in core.

See #943616: hook_custom_theme() does not work as expected and #1120188: Default Theme intermittently replaces Sub Theme for information about the reasons.

I ended up writing a custom hook_init() to force the theme to reset. I don't think that approach is appropriate for a patch to OG Theme. Though it could be moved to og_theme_init().

/**
 * Implements hook_init().
 */
function custom_init() {
  // This is a very nasty hack around https://drupal.org/node/1120188 and
  // https://drupal.org/node/943616.
  // FORCE the theme to re-initialize.
  unset($GLOBALS['theme']);
  drupal_theme_initialize();
}
agentrickard’s picture

Status: Needs work » Closed (works as designed)

That approach has issues with subthemes and admin pages, so I wrote a more robust version.

/**
 * Implements hook_init().
 */
function custom_init() {
  global $user;
  // This is a very nasty hack around https://drupal.org/node/1120188 and
  // https://drupal.org/node/943616.
  // FORCE the theme to re-initialize.
  if ($admin = path_is_admin(current_path())) {
    return;
  }

  // Only run if the page has not been cached.
  // This is a minor performance hit for authenticated users.
  $page_cache = cache_get(current_path(), 'cache_page');
  if (!$user->uid && $page_cache) {
    return;
  }

  // Reset loaded CSS, because sub-themes already ran.
  drupal_static_reset('drupal_add_css');

  // Reload the theme.
  unset($GLOBALS['theme']);
  drupal_theme_initialize();
}
justanothermark’s picture

Although this issue is closed, we had the same problem with a different workaround that I thought was worth sharing for anyone that finds the issue in the future.

Our approach was to avoid using og_context if it was possible to find the group type and ID from arg(). This only works for groups that are nodes but I'm sure it could be easily modified for other entities if required. It falls back to og_context if the shortcut doesn't work but

Again, as agentrickard has said, this is very much a workaround and I wouldn't expect it to be committed to the module unless a generic version could be made.