
Drupal 7 - Implementing RESTFul Web Services
Published: 11/17/2016
Implementing REST was a recent requirement in order to expose our Drupal website's data to external consumers. It felt like an enormous task - and it was! - but doable with a little help.
I found an extensive number of articles and tutorials online, but this video series by Mateu@e0ipso (opens in new tab) was, for me, the best and most comprehensive.
https://www.youtube.com/playlist?list=PLZOQ_ZMpYrZv8_c7jd_CkO_93-DnyVFY5 (opens in new tab)
I followed this series step-by-step, often pausing to recreate the code the Mateu used, which included creating a custom module and then tweaking that module for my own purposes.
My RESTFul API includes a resource for Nodes of a particular content type and a separate resource for a Taxonomy Term that is used in those nodes. As simple as it sounds, using the referenced Taxonomy Term - and displaying the name, rather than the ID - was one of the more challenging steps in my process. In the end, these are the resources that worked for me to generate the JSON I wanted.
NODE RESOURCE:
class Summit_Providers__1_0 extends ResourceNode {
/**
*{@inheritdoc}
*/
protected function publicFields(){
$public_fields = parent::publicFields();
//Rename label
$public_fields['name'] = $public_fields['label'];
unset($public_fields['label']);
//Hide fields
$public_fields['self']['methods'] = array();
$public_fields['links']['methods'] = array();
//Add more fields
$public_fields['standard_specialties'] = array(
'property' => 'field_standard_specialties',
'referencedIdProperty' => 'name'
);
$public_fields['school'] = array('property' => 'field_provider_school');
$public_fields['internship'] = array('property' => 'field_provider_internship');
$public_fields['residency'] = array('property' => 'field_provider_residency');
$public_fields['fellowship'] = array('property' => 'field_provider_fellowship');
$public_fields['language'] = array('property' => 'field_provider_language');
return $public_fields;
}
}
TAXONOMY TERM RESOURCE:
class Standard_Specialties__1_0 extends ResourceNode {
/*
*{@inheritdoc}
*/
protected function publicFields(){
$public_fields = parent::publicFields();
$public_fields['description'] = array('property' => 'description');
return $public_fields;
}
}
