0

I have a node with taxonomy term reference fields. The taxonomy term has an image field. Without installing extra modules (I already have WAY more than I should have) is there a way to display a specific field for the taxonomy instead of the textual? And do it from a Childtheme?

I have the DEVEL module installed and after selecting the specific field I still get no ideas. I have dug around inside the field, looking through the various attributes of that field without any ideas.

Array > 'element' > '#field_name'
Array > 'element' > '#field_type' = 'taxonomy_term_reference'
Array > 'element' > '#items' > 'tid' = 4
Array > 'element' > '#items' > 'taxonomy_term' > Object = Recursion

It feels like I am 'right there' and should be able to grab something like ['element'][0]['field_taxonomy_term_image'] and render, but I can't find anything. I realize, after searching stackexchange, and google, that I could install Views and tweak around with it to get the desired output, but I know I've seen somewhere that you can (without coding) get the output of a taxonomy term to be another field than 'title' but I don't remember where I saw it.

UPDATE: I have found the path, but can't figure out how to output it. I did a DPR versus a KPR and just dug through the text. Here is what I see. Inside of template.php inside of a drupal-based custom function THEMENAME_field__content_type($variables) I dpr'd $variables['element'] and found this object/array/path.

[#object] => stdClass Object
    [field_faction_affiliation] => Array
        [und] => Array
            [0] => Array
                [taxonomy_term] => stdClass Object
                    [field_faction_icon'] => Array
                        ...
                        [uri] => public://Faction - Jinteki.pnp
                        [filemime] => image/png
                        ...

So I feel like... I could theoretically, create a new $variables['faction_icon'] to pass on to my tpl.php using the pieces above to generate an img tag. Any ideas?

UPDATE: After a bit more digging around I got this to dpr and output what I expected.

$variables['element']['#object']->field_faction_affiliation['und'][0]['taxonomy_term']->field_faction_icon['und'][0]

So... now the tough part is figuring out how to generate an image path from a URI... of which, I am fairly clueless, but I will start searching.

UPDATE: After tweaking the function listed above I get this to work.

$faction_icon_url = file_create_url($variables['element']['#object']->field_faction_affiliation['und'][0]['taxonomy_term']->field_faction_icon['und'][0]['uri']);
$output = "<img class='faction_thumb' src='". $faction_icon_url . "' />";
return $output;

Now I have to figure out how to make this image link back to the taxonomy_term like the original output of text did.

0

Ok... so following all of the steps above, I was able to cobble together what I wanted.

In template.php I have a custom function:

THEMENAME_field__CONTENT_TYPE($variables)

Inside this function, I check for $variables['element']['#field_name'] in a switch statement. IF the switch statement equals the field that has the taxonomy reference (i.e., 'field_faction_affiliation') then I do the following:

case 'field_faction_affiliation':
    $base_path = base_path();
    $faction_icon_href = $base_path . $variables['element'][0]['#href'];
    $faction_icon_title = $variables['element'][0]['#title'];
    $faction_icon_url = file_create_url($variables['element']['#object']->field_faction_affiliation['und'][0]['taxonomy_term']->field_faction_icon['und'][0]['uri']);
    $output = "<a href='$faction_icon_href'><img alt='$faction_icon_title' class='faction_thumb' src='$faction_icon_url' /></a>";
    return $output;
    break;

I'm almost 100% certain that the base_path hobble I did is NOT drupal 'Best Practices' so if anyone has a suggestion for correcting that, please let me know. Otherwise. This is how I got what I wanted.

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.