Programmatically create Services endpoints

Published by Lennart Van Vaerenbergh on March 31, 2015

Many tutorials online for this one. Though I stumbled upon several issues.

My custom endpoints were not displaying in the UI (services list). It turned out it was because of one missing line of code. I followed this tutorial on drupal.org.

After you defined the hook_ctools_plugin_api() in your module file ...
/**
 * Implements hook_ctools_plugin_api().
 */
function mf_feeds_ctools_plugin_api($owner, $api) {
  if ($owner == 'services' && $api == 'services') {
    return array(
      'version' => 3,
    );
  }
}
... you can define the endpoints using:
/**
 * Implements hook_default_services_endpoint().
 */
function mymodule_default_services_endpoint() {
 
  $endpoint = new stdClass();
  $endpoint->disabled = FALSE;
  $endpoint->api_version = 3;
  $endpoint->name = 'feeds';
  $endpoint->server = 'rest_server';
  $endpoint->path = 'feeds';
  $endpoint->authentication = array();
  $endpoint->server_settings = array();
  $endpoint->resources = array(
    'rss' => array(
      'operations' => array(
        'index' => array(
          'enabled' => '1',
        ),
      ),
    ),
  );
  $endpoint->debug = 0;
  $endpoints['feeds'] = $endpoint;

  return $endpoints;
}
Line 13: Make sure this line is present. If not, your endpoint is not appearing anywhere. Not cool.
Line 23: Define the debug mode. If not defined, you get the notice: "Notice: Undefined property: stdClass::$debug in services_ctools_export_ui_form()" on your services list page.
Line 24: Make sure you add a key value to the $endpoints array, to make sure it doesn't conflict with other implementations of this hook.

Those were the obstacles I encountered. Hope it helps someone out!

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
CAPTCHA
This challenge is for testing whether or not you are a human visitor and to prevent automated spam submissions.