0

Lets say I have a taxonomy called bands, and a taxonomy called band_members.

Band_members have an entity reference field relating each band member to a band.

When I view the bands taxonomy term page the band_members will not be listed.

I can modify the taxonomy term template and manually add them in there, however the bands need to be shown in popups, and the content for each popup is returned via AJAX using drupal_render(taxonomy_term_view($term)). This means any template changes won't show up. What I need to do is use a hook like hook_taxonomy_term_view_alter to add the band_members for that band into the renderable array.

However that will be quite a lot of work, and I though it seems like a fairly standard task to need to displayed referencing entities, so is there an easier way to do this?

In this example I've used taxonomies as an example, however this could apply to nodes, users or any other entities that reference each other.

0

Seems custom code was the easiest thing here after a bit of research...

function HOOK_taxonomy_term_view_alter(&$build) {
    if($build['#term']->vocabulary_machine_name=='bands') {
        $units = load_bands_members($build['#term']->tid);
        $build['units'] = array(
            '#markup' => theme('bands_members', array('members' => $members))
        );

    }
}

The function load_band_members returns an array of taxonomy terms and looks like this:

function load_band_members($tid) {
    $tids = db_query("SELECT tid
        FROM taxonomy_term_data td
        INNER JOIN field_data_field_band b ON b.entity_id = td.tid
        WHERE td.vid = 6 AND b.field_band_tid = :tid", array(':tid'=>$tid))->fetchCol();
    return $tids ? taxonomy_term_load_multiple($tids) : false;
}

I'm sure it would somehow be possible to get Drupal to render my band members for me, like it does on their own taxonomy term page, as opposed to me having to call my own custom render function but that will do for now...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.