Attached is a patch to add hook_restws_response_alter that passes the function called and the response prior to serialization.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

wwhurley’s picture

Might help if I actually attach the patch file.

wwhurley’s picture

Status: Active » Needs review
klausi’s picture

Status: Needs review » Needs work

Yes, this sounds like a good idea. Ideally this drupal_alter() call should be done in restws_handle_request() between a call to normalization and serialization. Unfortunately serialization and normalization is very interwoven in RESTWS, so I don't think it makes sense to try and split that up at this point (something for RESTWS 3.x since that would also mean an interface change). So we have to add this alter invocation to all format operation methods :-(. Very ugly, but that would be fine with me.

In Drupal 8 we use the Symfony Serializer component for REST module, which makes a lot more sense.

+++ b/restws.api.php
@@ -130,6 +130,21 @@ function hook_restws_request_alter(array &$request) {
 /**
+ * Alter the outgoing response.
+ * ¶

Should be "Alter the outgoing response values", since you don't want to alter the serialized string but the data structure. And there is a trailing white space.

+++ b/restws.api.php
@@ -130,6 +130,21 @@ function hook_restws_request_alter(array &$request) {
+  if ($function == 'viewResource') {
+    // Do something to the resource being returned
+  }

Add an example by setting a value in $response.

+++ b/restws.api.php
@@ -130,6 +130,21 @@ function hook_restws_request_alter(array &$request) {
 
diff --git a/restws.formats.inc b/restws.formats.inc

diff --git a/restws.formats.inc b/restws.formats.inc
index 3941b2f..6fa791b 100644

index 3941b2f..6fa791b 100644
--- a/restws.formats.inc

--- a/restws.formats.inc
+++ b/restws.formats.inc

You forgot the delete operation and the operations of the XML and RDF formats.

wwhurley’s picture

Status: Needs work » Needs review
FileSize
4.37 KB

Added the alter hooks to XML and RDF and changed the signature of the alter hook to also have the format name since the response is mixed. Added alter hook to update and delete in case they want to return anything beyond the default empty array. This came about from wanting to send additional information back from the resource, but since only specific functions are called on the resource it wasn't possible. There is an annoying amount of repetition involved, but I'm unsure of a better way of doing it. Attached is the revised patch.

klausi’s picture

Status: Needs review » Needs work
+++ b/restws.api.php
@@ -130,6 +130,24 @@ function hook_restws_request_alter(array &$request) {
+function hook_restws_response_alter($function, array &$response, $formatName) {

The type hinting to an array for $response is wrong, since that could also be an object (e.g. for the XML use case).

+++ b/restws.formats.inc
@@ -101,6 +101,9 @@ abstract class RestWSBaseFormat implements RestWSFormatInterface {
+    $function = __FUNCTION__;

We can omit that line. Just pass in __FUNCTION__ directly to drupal_alter().

+++ b/restws.formats.inc
@@ -101,6 +101,9 @@ abstract class RestWSBaseFormat implements RestWSFormatInterface {
+    drupal_alter('restws_response', $function, $values, $this->formatName);

according to the drupal_alter() docs the second parameter should be the alterable thing, and the rest should be context variables. $values and $function should be swapped.

Yes, this approach is repetitive and ugly, but as I said, from the current architecture we cannot do it differently.

wwhurley’s picture

Hah, silly me, I changed the PHP doc block but didn't change the function signature. I've changed the alter signature to move the response forward in the call. We can't pass __FUNCTION__ to drupal_alter because only variables can be passed by reference and PHP gives a nice big error when I tried that.

And again, patch attached.

wwhurley’s picture

Status: Needs work » Needs review
klausi’s picture

Status: Needs review » Fixed

Tweaked the comments a bit and committed this. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.