This question is similar to this question but I don't understand how the URLs are modified enough for the answer there to make sense to me.

The module adds retrieve and index operations by default. I am attempting to extend privatemsg_services_resources with an additional option, unread.

I have added unread to the function as shown below, but I cannot figure out how to access it via URL.

function privatemsg_services_services_resources() {
  return array(
    'privatemsg' => array(
      'operations' => array(
        'index'    => array(
          'callback'        => '_privatemsg_services_get',
          'access callback' => 'user_is_logged_in',
          'args'            => array(),
        ),
        'unread'    => array(
          'callback'        => '_privatemsg_services_unread_count',
          'access callback' => 'user_is_logged_in',
            'args' => array(
              array(
                'name'        => 'uid',
                'type'        => 'int',
                'description' => t('UID of the user to check the number of unread messages for.'),
                'source'      => array(
                  'path' => 0,
                ),
              ),
            ),
          ),
        'retrieve' => array(
          'callback' => '_privatemsg_services_get_thread',
          'access arguments' => array(
            'get private messages from remote',
          ),
          'args' => array(
            array(
              'name'        => 'thread_id',
              'type'        => 'int',
              'description' => t('ID of the thread to be retrieved.'),
              'source'      => array(
                'path' => 0,
              ),
            ),
          ),
        ),
      ),
    ),
  );
}

When I go to www.myexample.com/drupalgap/privatemsg, I get the value for index.

When I go to www.myexample.com/drupalgap/privatemsg/THREAD_ID, I get the retrieve value for THREAD_ID.

When I go to www.myexample.com/drupalgap/privatemsg/unread/UID or www.myexample.com/drupalgap/privatemsg/unread, I get the message ["Invalid parameters passed."].

However, if I comment out the function for retrieve and then rename unread to retrieve (to test my code for unread), after clearing the caches, going to www.myexample.com/drupalgap/privatemsg/UID gives me the number of unread messages. So the code is OK, I'm just setting up the resource the wrong way.

How can I call both retrieve and unread from the URL?

  • The key is to use the actions array instead of the operations array when creating non-CRUD resources. Here's an example for DrupalGap: drupalgap.org/node/187 - FYI, all calls must use POST when using actions. When using operations the calls are directed based on the HTTP method (GET, POST, PUT, DELETE). – tyler.frankenstein Feb 12 '15 at 16:17
up vote 2 down vote accepted

The key is to use the actions array instead of the operations array when creating non-CRUD resources.

Here's an example for DrupalGap: http://drupalgap.org/node/187

FYI, all calls must use POST when using actions.

When using operations, the calls are directed based on the HTTP method (GET, POST, PUT, DELETE).

Don't forget to attach the X-CSRF-Token header to your POST calls. Its value can be retrieved here: ?q=services/session/token

Since this is for DrupalGap, and you're passing along an argument to your service resource, I'd recommend following these instructions, that way jDrupal can be utilized and your function(s) can easily be re-used:

http://drupalgap.org/node/203

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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