Skip to content

Instantly share code, notes, and snippets.

@ice423cube
Created March 12, 2014 21:08
Show Gist options
  • Save ice423cube/9516375 to your computer and use it in GitHub Desktop.
Save ice423cube/9516375 to your computer and use it in GitHub Desktop.
Drupalgap Add Taxonomy Name Drupal 7 Module.
<?php
// $Id$
/**
* @file
* Swaps Taxonomy TID to the actual Name of the file in Services REST results
*
*/
/**
* Implements hook_services_request_postprocess_alter().
*/
function tax_tid_services_request_postprocess_alter($controller, $args, &$result) {
if ($controller['callback'] === '_node_resource_retrieve') {
foreach ((array) $result as $key => $value) {
//***START Add Taxonomy Field Full Name - Duplicate for multiple fields
//field_series is a taxonomy field machine name that I want to grab the tid from to get the full name and add it into the results
//I am sure the are a couple of functions that could easly get added into this to help with code.
$current_field_name = 'field_series';
if ($key == $current_field_name) {
foreach ((array) $value as $level2) {
foreach ((array) $level2 as $level3) {
foreach ($level3 as $field) {
//get the tid from the field
$tax_tid = $field;
//name of new field
$new_field_name = $current_field_name.'_tax_full_name';
//creat varible that calls Function pass tid and get returns the taxonomy name
$tax_full_name = tax_tid_taxonomy_id_to_name($tax_tid);
//Add it to the stdClass Object
$result -> $new_field_name = $tax_full_name;
}
}
}
}
//***END Add Taxonomy Field Full Name
}
}
}
function tax_tid_taxonomy_id_to_name($tax_tid){
//get taxonomy name from tid
$tax_term = taxonomy_term_load($tax_tid);
$tax_name = $tax_term->name;
return $tax_name;
}
@ice423cube
Copy link
Author

I also added the below code into my custom Drupalgap module so i could replace the tid with the taxonomy name. I added this inside of this function hook_entity_post_render_content(entity) {}.

if (entity.field_series[language_default()][0]['tid'].length != 0) {
if (entity.field_series[language_default()][0]['tid'] != null) {

            var tax_id = entity.field_series[language_default()][0]['tid'];
            var tax_full_name = entity.field_series_tax_full_name;

            $('div a:contains(' + tax_id + ')').text(tax_full_name);

        }
    }

@signalpoint
Copy link

Just a reminder that this is now available in DrupalGap core (only for nodes though, support for this hasn't been added to the user entity type though).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment