0

According to the Services API in Drupal 7, we can use hook_services_request_postprocess_alter() to alter drupal's REST response before returning it to the user. This is useful if you want to alter the structure of an array, or strip out data we don't want returned to the API user. However, I don't see any way to identify what resource is being requested explicitly.

The Hooks passes: ($controller, $args, &$result);

The controller has semi-identifiable information, such as the callback which was fired, or the file where this resource exists. For instance, when firing a node INDEX request, we'd see:

[help] => List all nodes
[file] => Array
    (
        [type] => inc
        [module] => services
        [name] => resources/node_resource
    )

[callback] => _node_resource_index
[args] => Array
    (
        [0] => Array
            (
                [name] => page
                [optional] => 1
                [type] => int
                [description] => The zero-based index of the page to get, defaults to 0.
                [default value] => 0
                [source] => Array
                    (
                        [param] => page
                    )

            )

Etc. However, there is nothing standard here across services which would tell us: "This is a node INDEX request, using the NODE endpoint."

To be clear, we're referring to node in the resource sense, such as my_host/my_endpoint/node.json. Meaning, which of our endpoint's resources is being requested here, and what (if any) method/operation are we performing.

I was hoping there was something standard within this hook to identify this information, as opposed to relying on more external data, such as the GET or POST variables.

Anyone have insight into this?


[edit]:

hook_services_request_preprocess_alter() is passed an $options array, which contains the endpoint resource and method. I need access to this information in the postprocess hook mentioned above, but don't see any clean, Drupal-way to do that. this is what I am looking for.

The array looks as follows:

Array(
  [version] => ,
  [resource] => node, 
  [method] => 1.json
);
12
  • The callback function should be plenty to identify which resource is being used - what other data were you hoping to get?
    – Clive
    May 22, 2014 at 8:09
  • @Clive The callback could easily be altered by another hook, so I don't want to depend upon that when, say, I want to know "This was a node endpoint with a create request." There needs to be some explicit way to determine this, which cannot be altered. I'm not seeing that sort of thing in the available params of the hook.
    – Atomox
    May 22, 2014 at 13:53
  • But what's a "node endpoint" in this context? Is that a concept the services module provides that you know of? In my experience it's not - the services module defines a bunch of services on behalf of core modules, it doesn't describe a story for each or anything like that. It isn't clever enough to know that a node is one thing, and a taxonomy term is another thing, and so on, it's pretty dumb in that respect. Also an 'endpoint' refers to where you're getting the data from (i.e. site.com/my_endpoint), so you already have that information when you make the API call...
    – Clive
    May 22, 2014 at 13:55
  • ...services are supposed to be (and are) generic enough that they don't depend on any particular endpoint being used, which is likely why it's not available. I think you're looking for the Services module to define a Grammar for you, which you can parse to get contextual, human-readable information about the resources its using. To the best of my knowledge no such thing exists
    – Clive
    May 22, 2014 at 13:57
  • @Clive The person making the API call has the information that they are hitting endpoint/node.json, but Drupal should provide a way to get that as well. That is something it knows, and not something that can be changed by hooks. That information is not compromising the generic nature of services, and is very important in context of some of the callbacks being exposed in services, particularly as defined in services.alter.api.php. There must be some way to determine that information within the callbacks, without relying on the $_SERVER globals.
    – Atomox
    May 22, 2014 at 15:12

1 Answer 1

0

if you take a look to $args array, you can find your node type, for example I have a content type "video", my $args are like this:

Array
(
    [0] => 2
    [1] => *
    [2] => Array
        (
            [type] => video
            [uid] => 92
        )

    [3] => 2
)

Then $args[2] has the content type (because I set it like a parameter in my resource: http://site.local/api/v1/node?parameters[type]=video&parameters[uid]=92&pagesize=2&page=2

1
  • You're not wrong, but I really try not to depend upon the args array in this case. Drupal should offer context within the function variables to determine this, not some external source. That is what I was hoping to find.
    – Atomox
    Dec 23, 2014 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.