You are here

function ctools_context_keyword_substitute in Chaos Tool Suite (ctools) 6

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

Perform keyword and context substitutions.

4 calls to ctools_context_keyword_substitute()
ctools_content_render in includes/content.inc
Get the content from a given content type.
ctools_custom_content_type_render in plugins/content_types/custom/custom.inc
Output function for the 'custom' content type. Outputs a custom based on the module and delta supplied in the configuration.
page_manager_http_response_render in page_manager/plugins/task_handlers/http_response.inc
views_content_views_panes_content_type_render in views_content/plugins/content_types/views_panes.inc
Output function for the 'views' content type.

File

includes/context.inc, line 595
Contains code related to the ctools system of 'context'.

Code

function ctools_context_keyword_substitute($string, $keywords, $contexts) {

  // Ensure a default keyword exists:
  $keywords['%%'] = '%';

  // Match contexts to the base keywords:
  $context_keywords = array();
  foreach ($contexts as $context) {
    if (isset($context->keyword)) {
      $context_keywords[$context->keyword] = $context;
    }
  }

  // Look for context matches we we only have to convert known matches.
  $matches = array();
  if (preg_match_all('/%([a-zA-Z0-9%:_-]+)/us', $string, $matches)) {
    foreach ($matches[1] as $keyword) {

      // Ignore anything it finds with %%.
      if ($keyword[0] == '%') {
        continue;
      }

      // If the keyword is already set by something passed in, don't try to
      // overwrite it.
      if (!empty($keywords['%' . $keyword])) {
        continue;
      }

      // Figure out our keyword and converter, if specified.
      if (strpos($keyword, ':')) {
        list($context, $converter) = explode(':', $keyword, 2);
      }
      else {
        $context = $keyword;
        if (isset($context_keywords[$keyword])) {
          $plugin = ctools_get_context($context_keywords[$context]->plugin);

          // Fall back to a default converter, if specified.
          if ($plugin && !empty($plugin['convert default'])) {
            $converter = $plugin['convert default'];
          }
        }
      }
      if (empty($context_keywords[$context]) || !empty($context_keywords[$context]->empty)) {
        $keywords['%' . $keyword] = '';
      }
      else {
        if (!empty($converter)) {
          $keywords['%' . $keyword] = ctools_context_convert_context($context_keywords[$context], $converter);
        }
        else {
          $keywords['%' . $keyword] = $context_keywords[$keyword]->title;
        }
      }
    }
  }
  return strtr($string, $keywords);
}