Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.838
diff -u -p -r1.838 system.module
--- modules/system/system.module	8 Nov 2009 10:29:23 -0000	1.838
+++ modules/system/system.module	9 Nov 2009 02:42:42 -0000
@@ -308,6 +308,16 @@ function system_element_info() {
     '#attributes' => array(),
     '#items' => array(),
   );
+  $types['contextual_links'] = array(
+    '#theme' => 'links',
+    '#pre_render' => array('system_build_contextual_links'),
+    '#contextual_links' => array(),
+    '#links' => array(),
+    '#attributes' => array('class' => array('contextual-links')),
+    '#attached' => array(
+      'library' => array(array('system', 'contextual-links')),
+    ),
+  );
   // By default, we don't want AJAX commands being rendered in the context of an
   // HTML page, so we don't provide defaults for #theme or #theme_wrappers.
   // However, modules can set these properties (for example, to provide an HTML
@@ -3533,8 +3543,6 @@ function theme_system_settings_form($var
 
 /**
  * Template variable preprocessor for contextual links.
- *
- * @see system_build_contextual_links()
  */
 function system_preprocess(&$variables, $hook) {
   static $hooks;
@@ -3564,10 +3572,14 @@ function system_preprocess(&$variables, 
   }
 
   if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
-    $variables['contextual_links'] = system_build_contextual_links($element);
-    if (!empty($variables['contextual_links'])) {
-      $variables['classes_array'][] = 'contextual-links-region';
-    }
+    // Initialize the template variable as a renderable array of type
+    // 'contextual_links'.
+    $variables['contextual_links'] = array(
+      '#type' => 'contextual_links',
+      '#contextual_links' => $element['#contextual_links'],
+    );
+    // Label this element as having contextual links associated with it.
+    $variables['classes_array'][] = 'contextual-links-region';
   }
 }
 
@@ -3619,17 +3631,7 @@ function system_build_contextual_links($
     $item['localized_options']['query'] += $destination;
     $links[$class] += $item['localized_options'];
   }
-  $build = array();
-  if ($links) {
-    $build = array(
-      '#theme' => 'links',
-      '#links' => $links,
-      '#attributes' => array('class' => array('contextual-links')),
-      '#attached' => array(
-        'library' => array(array('system', 'contextual-links')),
-      ),
-    );
-  }
-  return $build;
+  $element['#links'] = $links;
+  return $element;
 }
 
