From ab033ed0d866472d2cf720ef8375ce57e48d9418 Mon Sep 17 00:00:00 2001
From: Chris Oden <wodenx@gmail.com>
Date: Sat, 5 Oct 2013 10:43:08 -0400
Subject: [PATCH 1/2] Supply method argument to documentation callbacks

---
 services_documentation.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/services_documentation.inc b/services_documentation.inc
index 1c2c871..a84dd20 100644
--- a/services_documentation.inc
+++ b/services_documentation.inc
@@ -51,7 +51,7 @@ function services_documentation_method_element($method) {
   // Call the documentation callback to generate the render element.
   if (!empty($method['documentation callback']) && function_exists(($method['documentation callback']))) {
     $documentation_callback = $method['documentation callback'];
-    $element = call_user_func_array($documentation_callback, array());
+    $element = call_user_func_array($documentation_callback, array($method));
   }
   else {
     // @todo build defaults from variables.
-- 
1.8.1.3


From 3c5fdca6a54678704b7cc6d3a4756097ab86c37a Mon Sep 17 00:00:00 2001
From: Chris Oden <wodenx@gmail.com>
Date: Sat, 5 Oct 2013 10:43:47 -0400
Subject: [PATCH 2/2] Provide default method element documentation.

---
 services_documentation.inc | 77 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 62 insertions(+), 15 deletions(-)

diff --git a/services_documentation.inc b/services_documentation.inc
index a84dd20..b0c978e 100644
--- a/services_documentation.inc
+++ b/services_documentation.inc
@@ -28,10 +28,7 @@ function services_documentation_build_documentation($endpoint) {
               continue;
             }
             if (is_array($resource[$method_bundle_type][$method_name])) {
-              // @todo make callback optional, add UI for specifying values.
-              if (!empty($method['documentation callback'])) {
-                $documentation_resources[$resource_name][$method_bundle_type][$method_name] = $method;
-              }
+              $documentation_resources[$resource_name][$method_bundle_type][$method_name] = $method;
             }
           }
         }
@@ -42,9 +39,14 @@ function services_documentation_build_documentation($endpoint) {
   return $documentation_resources;
 }
 
-
 /**
  * Generate a renderable element for a services documentation method.
+ *
+ * @param $method
+ *   The array describing the method, as returned by hook_services_resources().
+ *
+ * @return
+ *   An array suitable for rendering by the services documentaiton theming layer.
  */
 function services_documentation_method_element($method) {
 
@@ -54,16 +56,8 @@ function services_documentation_method_element($method) {
     $element = call_user_func_array($documentation_callback, array($method));
   }
   else {
-    // @todo build defaults from variables.
-    $element = array(
-      '#name' => $method['name'],
-      '#path' => $method['endpoint']->path . '/' . $method['name'],
-      '#weight' => 1,
-      '#description' => '',
-      '#request' => '',
-      '#response' => '',
-      '#auth' => !empty($method['endpoint']->authentication) ? TRUE : FALSE,
-    );
+    // If no documentation callback, use our default.
+    $element = services_documentation_default_method_element($method);
   }
 
   // Specifying #type attaches #pre_render and #theme callbacks
@@ -218,9 +212,62 @@ function services_documentation_verbs($style) {
         'update' => variable_get('services_documentation_verbs_custom_update', 'update'),
         'delete' => variable_get('services_documentation_verbs_custom_delete', 'delete'),
         'index' => variable_get('services_documentation_verbs_custom_index', 'index'),
+        'actions' => variable_get('services_documentation_verbs_custom_actions', 'action'),
+        'targeted_actions' => variable_get('services_documentation_verbs_custom_targeted_actions', 'targeted action'),
+        'relationships' => variable_get('services_documentation_verbs_custom_relationships', 'relationship'),
       );
       break;
   }
 
   return $verbs;
 }
+
+/**
+ * Provide a default documentation element for a services method.
+ *
+ * @param array $method
+ *   Information about the method as provided by hook_services_resources().
+ * @return array
+ *   A renderable array of documentation as would be returned by a documentation
+ *   callback.
+ */
+function services_documentation_default_method_element($method) {
+  $element = array(
+    '#name' => $method['name'],
+    '#path' => $method['name'],
+    '#weight' => 1,
+    '#description' => isset($method['help']) ? $method['help'] : '',
+    '#request_url' => '',
+    '#request_data' => '',
+    '#response' => '',
+    '#auth' => !empty($method['endpoint']->authentication) ? TRUE : FALSE,
+    '#parameters' => isset($method['args']) ? $method['args'] : array(),
+    '#method' => $method,
+  );
+
+  // Calculate the path.
+  if($method['endpoint']->server == 'rest_server') {
+    // @todo will this work for any server or just REST?
+    switch($method['bundle']) {
+      case 'operations':
+        $path = array();
+        break;
+      case 'actions':
+        $path = array($method['name']);
+        break;
+      case 'targeted_actions':
+      case 'relationships':
+        $path = array('*', $method['name']);
+        break;
+    }
+    // Add arguments to the path.
+    foreach ($element['#parameters'] as $key => $arg) {
+      if (isset($arg['source']) && is_array($arg['source']) && isset($arg['source']['path'])) {
+        $path[$arg['source']['path']] = '[' . $arg['name'] . ']';
+      }
+    }
+    $element['#path'] = $method['resource']['name'] . '/' . implode('/', $path);
+  }
+
+  return $element;
+}
-- 
1.8.1.3

