1

When I tried to configure drupal service admin/structure/services/list/MyServiceName/server, I only see the following response content types.

enter image description here

Is it possible to provide pdf, docx, pptx, xlsx etc. as response format?

1
  • ... and format them how?
    – AKS
    Jun 8, 2016 at 10:03

1 Answer 1

4

Yes it's definitely possible, but you'll need to write a formatter class for each type of file as the bundled REST server only has support for XML, JSON, JSONP, serialized PHP, bencode, and optionally YAML.

To start, you need to implement hook_rest_server_response_formatters_alter to let the REST server know about your new type, e.g.:

function MYMODULE_rest_server_response_formatters_alter(&$formatters) {
  $formatters['pdf'] = array(
    'mime types' => array('application/pdf'), 
    'formatter class' => 'MyModuleServicesPDFFormatter',
  );
}

Then add your formatter class, which needs to implement ServicesFormatterInterface and provide the render method:

class MyModuleServicesPDFFormatter implements ServicesFormatterInterface {
  public function render($data) {
    return some_function_to_convert_data_to_pdf_string($data);
  }
}

Then make sure that class is auto-loaded by putting this in your module's .info file:

files[] = relative/path/to/MyModuleServicesPDFFormatter.php

Repeat the process for all of the new formatters you wish to provide, clear the cache when you're done.

3

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.