Adding response values to Thermal API

I’ve been asked to implement a very Web 2.0 dropdown menu that will display page content without leaving the front page. I’ve set to use Thermal API for the project and after a really painless installation I was able to make my first GET request for a page or post.

Lacking detail

Thermal will give me all the details I need in a neat JSON-formatted response but the one detail I need about a page, the template that page is using, is missing. A full response for the iconic Sample Page will read like

{
    "author": {
        "avatar": [{
            "height": 96,
            "url": "http://1.gravatar.com/avatar/d393fcb75e0801c42fe5559ae1e846e6?s=96&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&r=G",
            "width": 96
        }],
        "display_name": "theAdmin",
        "id": 1,
        "id_str": "1",
        "meta": {
            "description": "",
            "first_name": "",
            "last_name": "",
            "nickname": "theAdmin"
        },
        "nicename": "theadmin",
        "posts_url": "http://example.com/author/theadmin/",
        "user_url": ""
    },
    "comment_count": 0,
    "comment_status": "open",
    "content": "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes).",
    "date": "2014-03-17T07:46:58+00:00",
    "excerpt": "",
    "excerpt_display": "<p>This is an example page. It&#8217;s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this: Hi there! I&#8217;m a bike messenger [&hellip;]</p>\n",
    "id": 2,
    "id_str": "2",
    "media": [],
    "menu_order": 0,
    "meta": {},
    "mime_type": "",
    "modified": "2014-04-09T05:57:45+00:00",
    "name": "sample-page",
    "parent": 0,
    "parent_str": "0",
    "permalink": "http://example.com/sample-page/",
    "status": "publish",
    "taxonomies": {},
    "title": "Sample Page",
    "type": "page"
}

Can I hook?

In a drill learned working with WordPress itself and Headway I' ve gone looking for an hook in the API to modify the response and, needless to say, it’s there.
The api/v1/controllers/Post.php file contains the format method and, after filling up the response details, the code exposes the thermal_post_entity hook.
From there it’s just a question of returning the modified data to see it printed on the page. The whole menial plugin reads like

// ... the usual plugin header here...

function thermal_template_add_template($data, $post){
    // slug will be '' for default page template
    // false if the post is not a page
    // a string otherwise
    $slug = get_page_template_slug($post->ID);
    if ($slug or $slug == '') {
        // data is an object, cast to array
        $data = (array)$data;
        if ($slug == '') {
            $slug = 'default';
        } else {
            // do some cleaning of the slug to keep the template name alone
            $slug = preg_replace("/^(.*)([\\w-_]*)(.php)$/uiUm", "$2", $slug);
            // replace hyphens with underscores
            $slug = str_replace('-', '_', $slug);
        }
        $data['template'] = $slug;
    }
    return (object)$data;
}

// hook into the filter to add the template
if (function_exists('add_filter')) {
    // the filter defined in the api/v1/controllers/Posts.php file

    // add the template data
    $tag = 'thermal_post_entity';
    $function = 'thermal_template_add_template';
    $accepted_args = 2;
    // hook late to allow the Thermal API plugin to load
    $priority =  1000;
    add_filter( $tag, $function, $priority, $accepted_args );
}

and that’s enough to have a request for this page [caption id=“attachment_873” align=“aligncenter” width=“1024”]Back-end side of the page Back-end side of the page[/caption] have this response [caption id=“attachment_874” align=“aligncenter” width=“1024”]Thermal API modified response Thermal API modified response[/caption]