1

Our Drupal system has the Services module enabled and is able to return nodes as json using the following url format

http://www.example.com/my_endpoint_path/node/123.json

But I want to retrieve a node using the URL alias I set in the URL path settings tab. I hoped the following would work

http://www.example.com/my_endpoint_path/my_pages_URL_alias.json

But it does not. Is there a way to configure Drupal/Services to fetch nodes by that URL rather than the node id?

2

In Service Module API, Some hooks are presents to help you. You need to alter or add your own implementation of this service.

The second link is a file where you have the default service implementation. You can try to alter this configuration with your own by using "hook_services_resources_alter".

I can't test but you can try and custom this snippet :

function YOUR_MODULE_services_resources_alter(&$resources, &$endpoint) {
    $['node']['operations']['retrieve']['callback'] = '_your_retreive_method';
    $['node']['operations']['retrieve']['args']     = array(
        'name' => 'node_alias',
        'optional' => FALSE,
        'source' => array('path' => 0), // Put your path index here
        'type' => 'string',
        'description' => 'The alias node to get',
    );
}

function _your_retreive_method($node_alias){
    // transform your alias to nid
    $path   = drupal_get_normal_path($node_alias);    
    if($path !== $node_alias && ctype_digit($nid = str_replace('node/','',$path))){
        return _node_resource_retrieve($nid);
    }
    return false;
}

Links :

  • Thanks - this helped get me started. Unfortunately it doesn't work with multi-part aliases, which is a pretty serious limitation. (my-alias will work but not about-us/my-alias.) I've remedied this by converting the slashes on the client end and then reverting them in my callback function. I would rather send the alias as a parameter in a query, but couldn't implement it successfully. – othermachines Nov 3 '15 at 17:21
  • 1
    Here's a gist that handles multi-part aliases, provided the slashes are converted in the request: gist.github.com/laradevitt/6ab86bf7ebbd3215b20b – othermachines Nov 3 '15 at 17:39
0

Payou's answer worked for me with some additions. In the YOUR_MODULE_services_resources_alter function I also needed to specify a custom access function:

  $resources['node']['operations']['retrieve']['access callback'] = '_YOUR_MODULE_node_resource_access';
  $resources['node']['operations']['retrieve']['access arguments append'] = TRUE;

And add the access function:

<?php
function _YOUR_MODULE_node_resource_access($op, $node_alias)
{
  $path = drupal_get_normal_path($node_alias[0]);
  if ($path !== $node_alias && ctype_digit($nid = str_replace('node/', '', $path))) {
    return _node_resource_access($op, array($nid));
  }
  return services_error(t('Content could not be found.'), 404);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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