You are here

function ctools_content_select_context in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 includes/content.inc \ctools_content_select_context()

Select the context to be used for a piece of content, based upon config.

Parameters

$plugin: The content plugin

$subtype: The subtype of the content.

$conf: The configuration array that should contain the context.

$contexts: A keyed array of available contexts.

Return value

The matching contexts or NULL if none or necessary, or FALSE if requirements can't be met.

2 calls to ctools_content_select_context()
ctools_content_admin_title in includes/content.inc
Get the administrative title from a given content type.
ctools_content_render in includes/content.inc
Get the content from a given content type.

File

includes/content.inc, line 747
Contains the tools to handle pluggable content that can be used by other applications such as Panels or Dashboard.

Code

function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {

  // Identify which of our possible contexts apply.
  if (empty($subtype)) {
    return;
  }
  $subtype_info = ctools_content_get_subtype($plugin, $subtype);
  if (empty($subtype_info)) {
    return;
  }
  if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
    return $contexts;
  }

  // If the content requires a context, fetch it; if no context is returned,
  // do not display the pane.
  if (empty($subtype_info['required context'])) {
    return;
  }

  // Deal with dynamic required contexts not getting updated in the panes.
  // For example, Views let you dynamically change context info. While
  // we cannot be perfect, one thing we can do is if no context at all
  // was asked for, and then was later added but none is selected, make
  // a best guess as to what context should be used. THis is right more
  // than it's wrong.
  if (is_array($subtype_info['required context'])) {
    if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) {
      foreach ($subtype_info['required context'] as $index => $required) {
        if (!isset($conf['context'][$index])) {
          $filtered = ctools_context_filter($contexts, $required);
          if ($filtered) {
            $keys = array_keys($filtered);
            $conf['context'][$index] = array_shift($keys);
          }
        }
      }
    }
  }
  else {
    if (empty($conf['context'])) {
      $filtered = ctools_context_filter($contexts, $subtype_info['required context']);
      if ($filtered) {
        $keys = array_keys($filtered);
        $conf['context'] = array_shift($keys);
      }
    }
  }
  if (empty($conf['context'])) {
    return;
  }
  $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
  return $context;
}