You are here

function panels_everywhere_site_template_get_base_contexts in Panels Everywhere 7

Same name and namespace in other branches
  1. 6 plugins/tasks/site_template.inc \panels_everywhere_site_template_get_base_contexts()

Figure out the base contexts in use for the page.

This only works with CTools API v1.8 or greater; prior to that Page Manager only supported arguments.

1 string reference to 'panels_everywhere_site_template_get_base_contexts'
site_template.inc in plugins/tasks/site_template.inc

File

plugins/tasks/site_template.inc, line 54

Code

function panels_everywhere_site_template_get_base_contexts($task, $subtask, $placeholders) {
  $contexts = array();

  // Only load taxonomy support if the Taxonomy module is loaded.
  $load_taxonomy = FALSE;
  if (module_exists('taxonomy')) {
    $load_taxonomy = TRUE;
  }
  if ($placeholders) {
    $managed_page = ctools_context_create_empty('managed_page');
    $url = ctools_context_create_empty('string');
    $alias = ctools_context_create_empty('string');
    $node = ctools_context_create_empty('node');
    $account = ctools_context_create_empty('user');

    // This context will only be created if the Taxonomy module is loaded.
    if ($load_taxonomy) {
      $term = ctools_context_create_empty('entity:taxonomy_term');
    }
  }
  else {
    $page = page_manager_get_current_page();
    $managed_page = ctools_context_create('managed_page', $page);
    $url = ctools_context_create('string', $_GET['q']);
    $alias = ctools_context_create('string', drupal_get_path_alias($_GET['q']));

    // If using a Page Manager Page, attempt to inherit our known contexts from
    // this.
    if ($page) {
      $node = _panels_everywhere_find_context($page['contexts'], 'node');
      $account = _panels_everywhere_find_context($page['contexts'], 'user');

      // We're already including the logged in user. We don't want that to
      // accidentally show up as the user being viewed if we're doing
      // something unrelated and the logged in user was added as a context.
      if (isset($account->data->logged_in_user)) {
        $account = ctools_context_create_empty('user');
      }

      // This context will only be loaded if the Taxonomy module is loaded.
      if ($load_taxonomy) {
        $term = _panels_everywhere_find_context($page['contexts'], 'entity:taxonomy_term');
      }
    }
    else {

      // Attempt to drive our basic contexts from the environemnt if we are
      // not using a panel page.
      // First, find a node. This is the same code used by Views.
      $raw_node = _panels_everywhere_find_node_context();
      $node = !empty($raw_node) ? ctools_context_create('node', $raw_node) : ctools_context_create_empty('node');

      // Now try to find a user.
      $raw_user = _panels_everywhere_find_user_context();
      $account = !empty($raw_user) ? ctools_context_create('user', $raw_user) : ctools_context_create_empty('user');

      // Now try to find a term.
      // This context will only be loaded if the Taxonomy module is loaded.
      if ($load_taxonomy) {
        $raw_term = _panels_everywhere_find_term_context();
        $term = !empty($raw_term) ? ctools_context_create('entity:taxonomy_term', $raw_term) : ctools_context_create_empty('entity:taxonomy_term');
      }
    }
  }

  // Create a CTools context out of the current user - if the user is logged in
  // then work off the full user object, otherwise just use the anonymous user
  // object.
  $user_object = user_is_logged_in() ? user_load($GLOBALS['user']->uid) : $GLOBALS['user'];
  $user = ctools_context_create('user', $user_object);

  // First, the managed page. The id is weird because this was formerly an
  // object so we want to retain that so pre-existing templates don't just
  // break.
  panels_everywhere_site_template_add_context($contexts, $url, t('Internal URL'), 'url', 'argument_string_1');
  panels_everywhere_site_template_add_context($contexts, $alias, t('Aliased URL'), 'alias', 'alias');
  panels_everywhere_site_template_add_context($contexts, $managed_page, t('Managed page'), 'page', 'argument_managed_page_1');
  panels_everywhere_site_template_add_context($contexts, $user, t('Logged in user'), 'user', 'logged-in-user');
  panels_everywhere_site_template_add_context($contexts, $node, t('Node being viewed'), 'node', 'node');
  panels_everywhere_site_template_add_context($contexts, $account, t('User being viewed'), 'account', 'account');

  // This context will only be added if the Taxonomy module is loaded.
  if ($load_taxonomy) {
    panels_everywhere_site_template_add_context($contexts, $term, t('Taxonomy term being viewed'), 'term', 'term');
  }

  // Allow other modules to also add contexts to the site template.
  drupal_alter('panels_everywhere_contexts', $contexts, $placeholders);
  return $contexts;
}