2

I'd like some advice as I don't know if I'm doing this the correct way...

I'd like to create a web service to get a specific node type. I don't need create/update/delete functionality, just being able to retreive the data is enough. It also shouldn't contain any authentication (the data is public).

This node type contains several field collections. Each field collection could occur multiple times in the node. (for example, there's the node type "Car" which contains the field collections "Audi", "BMW", "Skoda". Each of these can occur multiple times in the node).

The JSON I'd like to return should look like this:

{"cars": {
    "title": "newest cars",
    "image": "http://www.example.com/image.jpg",
    "audi": [
       {
        "title": "A1",
        "price": "expensive"
       },
       { "title": "A6",
         "price": "very expensive"
       }
    ],
   "BMW": [
       // multiple BMW's
    ],
}} 

It's possible that zero cars of a specific brand are returned.

I've been looking how to do this in Drupal. I don't have much Drupal experience, so I'm sorry if this is a very easy thing.

I'm currently using the services module. I've had a look at this tutorial:
http://pingv.com/blog/an-introduction-drupal-7-restful-services

It looks pretty easy, however, the field collection module adds all fields in a different database table. With all fields I have, I'd have to join more than 20 tables in the query and the Drupal database can get pretty confusing this way.

I'm wondering if there's a much easier way to do this. I've already looked at other modules like "Services Views", but these don't seem to do the things I want:

  • Return specific node (and specific node id) which contains field collections
  • Return image url
  • Return only the fields I want
  • Return easy, readable names
  • Accept certain parameters in the URL (which I'd like to define myself) to filter

Is there an easy way to do this or is the only way creating the very complex query?

Thanks!

2
  • you can retrieve all node filed values of a particular nid. you can get them like http://yourdomain/[endpoint]/node/[nid] . If you want to get only fields you want, I believe service views module does that. Jan 19, 2016 at 13:40
  • what are you not able to achieve with services views? I see all what you want is possible with services views
    – nikunj
    Jan 20, 2016 at 12:58

2 Answers 2

2
+50

You can use any of the following modules:

  1. Views Datasource

Pros

  • Very easy to export using views.
  • Does not need require a lot knowl

Cons

  • This implementation is only to export data, meaning that is not bidirectional.

Description

Views Datasource is a set of plugins for Drupal Views for rendering content in a number of shareable, reusable formats based on XML, JSON and XHTML. These formats allow content in a Drupal site to be easily used as data sources for Semantic Web clients and web mash-ups. Views Datasource plugins output content from node lists created in the Drupal Views interface in a variety of formats - XML data documents using schemas like OPML and Atom, RDF data documents using a vocabulary like FOAF, JSON data documents in a format like Exhibit JSON, and XHTML data documents using a microformat like hCard.

  1. Services

Pros

  • Very flexible.
  • Bidirectional.
  • A lot support and documentation by the Drupal community on how to use it.
  • Its the module with most install comparing the another two modules.

Cons

  • Very complex compare to the rest modules.

A standardized solution for building API's so that external clients can communicate with Drupal. Out of the box it aims to support anything Drupal Core supports and provides a code level API for other modules to expose their features and functionality. It provide Drupal plugins that allow others to create their own authentication mechanisms, request formats, and response formats.

  1. RESTful Web Services

Pros

  • The Drupal 8 version of this module is in core.
  • Bidirectional
  • In theory migrating to Drupal 8 from this module will be a lot easier to compare to the rest of them. Since this in core now.

Cons

  • Does not have as many install as the rest of the modules.
  • Level difficulty is in between views dataresource and services.
  • This project is not as flexible as services.

Exposes Drupal resources (e.g. entities) as RESTful web services.

The module makes use of the Entity API and the information about entity properties (provided via hook_entity_property_info()) to provide resource representations for all entity types (nodes, comments, users, taxonomy terms, ..). It aims to be fully compliant to the REST principles.

In the end is comes down to your need you should check this presentation - Producing REST Web Services in Drupal 7 - Baltimore Drupal Camp 2014. In the presentation is going to create a webservice and show a more details explanation on the best use case of each.

0
3

Views Datasource is very popular module to return json, xml or any other type of data.

Views Datasource is a set of plugins for Drupal Views for rendering content in a number of shareable, reusable formats based on XML, JSON and XHTML.

Install the module. Create the view that return your node. then under format select data source as json. You can only select the field you want to render in field settings.

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.