2

I am new to Drupal services; I have the below XML format, in which I need to expose nodes as services.

Can I do this using the Services module?

    <?xml version="1.0" encoding="UTF-8"?>
<ngd:mediafile xmlns:ngd="http://www.xyzy.com/ngdine"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.xyzy.com/ngdine mediafile.xsd">
  <ngd:description>
    <![CDATA[This is the description]]>
  </ngd:description>
  <ngd:mediatype id="1"/>
  <ngd:internal>Aalborg city.JPG</ngd:internal>
</ngd:mediafile>

Can I change the schema?

2 Answers 2

2

If you need to expose the output that way, THeres 2 ways you can do it. You can alter the response before it gets sent as a payload, or you can write your own Response Format.

Both should do what you need, but its merely a preference.

/**
 * Triggered when the REST server request a list of supported response formats.
 *
 * @param array $formatters
 *  An associative array of formatter info arrays keyed by type extension. The
 *  formatter info specifies an array of 'mime types' that corresponds to the
 *  output format; a 'view' class that is a subclass of RESTServerView; and
 *  'view arguments' that should be passed to the view when it is created;
 *  a 'model' can also be specified which the controller then must declare
 *  support for to be able to serve data in that format.
 * @return void
 */
 function hook_rest_server_response_formatters_alter(&$formatters) {
   /*
    * Sample modifications of the formatters array. Both yaml and
    * rss are formats that already are supported, so the changes are
    * nonsensical but illustrates the proper use of this hook.
    */

    // Add a Yaml response format.
    $formatters['yaml'] = array(
      'mime types' => array('text/plain', 'application/x-yaml', 'text/yaml'),
      'view' => 'RESTServerViewBuiltIn',
      'view arguments' => array('format' => 'yaml'),
    );

    // Add a Rss response format.
    $formatters['rss'] = array(
     'model' => 'ResourceFeedModel',
     'mime types' => array('text/xml'),
     'view' => 'RssFormatView',
    );
 }

The altering of the response is, function HOOK_services_request_postprocess_alter

2
  • Please mark this as the answer if it is correct. Commented May 6, 2013 at 18:07
  • I used this solution and it worked for me, looking at rest_server.views.inc in the Services module helped. In the case of XML you just need to create a formatter, and then copy the class RESTServerViewBuiltIn and render_xml() to a custom module and change it to suit the schema you are aiming for.
    – cdmo
    Commented May 8, 2013 at 12:42
1

Besides retrieving content as XML, do you need any other functionality provided by the Services module? If not, it might be overkill.

One simple alternative might be to write a module that implements hook_menu() to define a menu item for a wildcard path such as node/%/xml, which then, in its page callback, outputs the XML in your specified format.

In Drupal 7, you might do this as follows:

<?php

function mymodule_menu() {
  $items['node/%nid/xml'] = array(
    'page callback' => 'mymodule_xml_view',
    'page arguments' => array(1),
  );
  return $items;
}

function mymodule_xml_view($nid) {
  drupal_set_header('Content-Type: text/xml');

  echo '<?xml version="1.0" encoding="UTF-8"?>';
  echo '<ngd:mediafile ...>';
  echo '<nid>' . $nid . '</nid>';
  // ...
  echo '</ngd:mediafile>';

  drupal_exit();
}

Then, after enabling the new module, you can retrieve XML content by simply fetching, for example, URL http://example.com/node/42/xml .

2
  • What are those last three functions for? module_invoke_all('exit'); session_write_close(); exit();
    – cdmo
    Commented May 2, 2013 at 21:10
  • 1
    @cdmo: Drupal will, by default, try to render a themed page. Instead of doing that, we want to provide output directly, then stop Drupal from doing its thing. Those 3 invocations take care of that. (Edit: I just replaced it with drupal_exit(), which is a simpler way of achieving the same.)
    – smokris
    Commented May 3, 2013 at 0:12

Your Answer

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

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