Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Attached is a patch to add hook_restws_response_alter that passes the function called and the response prior to serialization.
Comment | File | Size | Author |
---|---|---|---|
#6 | response_alter_hook.1878064-6.patch | 4.37 KB | wwhurley |
#4 | response_alter_hook.1878064-4.patch | 4.37 KB | wwhurley |
#1 | response_alter_hook.1878064.patch | 1.79 KB | wwhurley |
Comments
Comment #1
wwhurley CreditAttribution: wwhurley commentedMight help if I actually attach the patch file.
Comment #2
wwhurley CreditAttribution: wwhurley commentedComment #3
klausiYes, 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.
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.
Add an example by setting a value in $response.
You forgot the delete operation and the operations of the XML and RDF formats.
Comment #4
wwhurley CreditAttribution: wwhurley commentedAdded 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.
Comment #5
klausiThe type hinting to an array for $response is wrong, since that could also be an object (e.g. for the XML use case).
We can omit that line. Just pass in __FUNCTION__ directly to drupal_alter().
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.
Comment #6
wwhurley CreditAttribution: wwhurley commentedHah, 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.
Comment #7
wwhurley CreditAttribution: wwhurley commentedComment #8
klausiTweaked the comments a bit and committed this. Thanks!