Hi,
Looking at services_controller_execute() - services.runtime.inc line 152+ :
// Check if the arguments should be preprocessed
if (!empty($controller['endpoint']['preprocess'])) {
foreach ($controller['endpoint']['preprocess'] as $callable) {
call_user_func_array($callable, array(&$args, &$controller));
}
}
// Execute the controller callback
$result = call_user_func_array($controller['callback'], $args);
if (isset($server_root) && $server_root) {
chdir($server_root);
}
// Check if the result should be post-processed
if (!empty($controller['endpoint']['postprocess'])) {
foreach ($controller['endpoint']['postprocess'] as $callable) {
call_user_func_array($callable, array(&$args, $controller));
}
}
return $result;
}
I can see how the arguments can be affected by the preprocess functions - how would these functions be registered?
It looks like modules could be given the opportunity to modify the output of a resource before it is handed to the server, but I don't understand where to register the postprocess function. Also I don't see how the result is affected by calling the postprocess functions - or am I missing the point?
I need to modify node output before handing to the server, so currently I have written a custom node resource, but it may be more appropriate to do it with a post processing function.
Comments
Comment #1
mente commentedI found it working like this in Drupal 7, using Services 3.x:
Comment #2
dpiMente, context please.
I don't think the pre/post process functionality is hooked up in any way. Unfortunately there are no examples for them either, seems like an oversight.
I believe the code is has not been used before, the admin pages mention the pre/post processes, but are they doing anything with them?
Working 7.x-3.x-dev version here.
Comment #3
mente commentedAs an example, context of the saveEndPoint(), defined in tests/services.test
And preprocessing really works. Yes, it's not defined in admin pages, probably it should be added.
Comment #4
sinasalek commentedIt seems that post/pre process does not work. even if it does considering the fact that the result has not passed to the the post/pre process function it has no usage. it should be something similar to this, it should also support weights
Comment #5
marcingy commentedBumping to 7 as we need to fix and backport.
Comment #6
sinasalek commentedTo preserve backward compatibility
Also i think having a resource wide or service wide pre/post process callbacks is also very useful.
Note that the weight of the module that the callback is located should be less than service module!
Comment #7
sinasalek commentedAnother update, checking function existence otherwise the result becomes null
Comment #8
tedbowI have created a patch for this.
I have also changed this:
to this
There seems to be no reason to pass the $args and $controller by reference in the postprocess b/c the will not be used after this and the callback has already be executed at this point.
I have tested it and it works for my purposes.
Comment #9
kylebrowning commentedPatch looks fine, can anyone else in this issue confirm that this patch fixes the problem?
Comment #10
marcingy commentedThere are style issues, plus why do we have exist checks? If you are implementing a post/preprocess function then surely the code should exist, so we shouldn't be hidding errors.
Comment #11
tedbowAgreed on the exist checks. I removed them. Can you give me some ideas on the style issues with the patch? I did run it though "Coder" so I assume its something else.
Thanks
Comment #12
marcingy commentedYup no problem
You are missing some white space.
should be
Comment #13
tedbowthanks, for catching that. I added the whitespace.
Comment #14
kylebrowning commentedCommitted, updated patch here, setting to be ported to 6.x
Comment #15
tedbowkylebrowning, I just noticed that patch you have in #14
I think has a problem in the preprocess function call.
I think this is just copied from the postprocess call. I think the original line here was correct. The $result variable has not been made yet from the "$controller['callback']" and the $args and $controller variables should be sent by reference here, right?
This line is the only difference from the patch in #13 which I think is actually correct.
I checked these changes are in rc2.
Comment #16
kylebrowning commentedYou are correct, my bad.
So that line should read
call_user_func_array($callable, array(&$args, $controller));Comment #17
tedbowYeah that's right. Thanks
Comment #18
kylebrowning commentedComment #19
kylebrowning commentedComment #21
mnlund commentedIt looks like support for preprocess and postprocess is removed from 3.1. Is that correct?
Comment #22
mnlund commentedCan I open this? Refer to #21
Comment #23
mnlund commentedI see no that the preprocess functions are moved to the alter functions services_request_preprocess and services_request_postprocess, which is nice and works as expected. I just close this then.