From f8d264a135442d9c5e050558915378f21ed0f239 Mon Sep 17 00:00:00 2001
From: Tim Popham <popham@uw.edu>
Date: Tue, 11 Oct 2011 14:32:57 -0700
Subject: [PATCH] Process keywords from pane config argument input.

---
 includes/content.inc |   36 ++++++++++++++++++++++++++++++++----
 includes/context.inc |   22 ++++++++++++++++++++++
 2 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/includes/content.inc b/includes/content.inc
index a55f7ec..5bb5d8c 100644
--- a/includes/content.inc
+++ b/includes/content.inc
@@ -774,11 +774,39 @@ function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {
     }
   }
 
-  if (empty($conf['context'])) {
-    return;
+  $context = array();
+  if (!empty($conf['context'])) {
+    $subtype_context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
+
+    if ($subtype_context === FALSE) {
+      return FALSE;
+    }
+
+    $context = is_array($subtype_context) ? $subtype_context : array($subtype_context);
   }
 
-  $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
+  if (!empty($conf['arguments'])) {
+    $arguments_context = ctools_context_keyword_select($contexts, $conf['arguments']);
 
-  return $context;
+    if ($arguments_context === FALSE) {
+      return FALSE;
+    }
+
+    if (is_array($arguments_context)) {
+      $context = array_merge($context, $arguments_context);
+    }
+    else {
+      $context = array_merge($context, array($arguments_context));
+    }
+  }
+
+  if (!empty($context)) {
+    // Unwrap the context if there's only one.
+    if (count($context) == 1) {
+      return reset($context);
+    }
+    else {
+      return $context;
+    }
+  }
 }
diff --git a/includes/context.inc b/includes/context.inc
index cac5bb9..9f68302 100644
--- a/includes/context.inc
+++ b/includes/context.inc
@@ -555,6 +555,28 @@ function _ctools_context_select($contexts, $required, $context) {
   return $required->select($contexts, $context);
 }
 
+function ctools_context_keyword_select($contexts, $arguments) {
+  // Rekey the array by keyword, prepending the '%'.
+  for ($i = 0; $i < count($contexts); ++$i) {
+    $first = array_shift($contexts);
+    $contexts['%' . $first->keyword] = $first;
+  }
+
+  foreach ($arguments as $pane_arg_id => $keyword) {
+    if (strlen($keyword) > 1 && $keyword[0] == '%' && $keyword[1] != '%') {
+      list($keyword, $datum) = explode(':', $keyword);
+      if (!isset($contexts[$keyword])) {
+        return FALSE;
+      }
+      $context[] = $contexts[$keyword];
+    }
+  }
+
+  if (!empty($context)) {
+    return $context;
+  }
+}
+
 /**
  * Create a new context object.
  *
-- 
1.7.7

