Longtime Drupal user, still getting used to Backdrop...

I've created three taxonomy vocabularies on the site I'm currently working on, and added an image field to each of them, for an icon representing each term. And I have a content type that includes a term reference field for each of the three vocabularies.

What I would like to do is have the content type display the icon for the chosen term, rather than the name of the term. I'm guessing I need to create a field template for this? But I haven't done that in Backdrop before. Is it basically the same method as for Drupal, in terms of naming conventions etc.? Or is there a different way? Or an easier way to do this without a custom template?

Accepted answer

For the layout, try the path to be node/% and the visibility condition to be Node type = Game.

Comments

Not having much luck with this so far... I made a field template called field--field-type-taxonomy-term-reference.tpl.php, but I can't get it to do anything — is the template name not correct? field-type-taxonomy-term-reference is the class used for that field type... I also tried renaming it with the hyphens after the first double one changed to underscores (field--field_type_taxonomy_term_reference.tpl.php) but that didn't work either.

Also, I have not been able to figure out how to reference the image for the taxonomy term. I tried creating a new display mode called "icon" for the taxonomy vocabularies, and set it to only display the image for the term — I was hoping maybe I could then just set the field to use that display mode for the field in the display settings for the content type, and maybe not even have to use a field template, but it doesn't show up there.

And I'm not even sure if I can access the image field for the terms from the field template even if I can get it to recognize the template — are the fields of a vocabulary other than the name included in the $items array? I've been trying to output the contents of that array with print_r to see, but since I can't get it to recognize the field template at all, that has obviously not been very successful...

It probably doesn't help that I haven't been using even Drupal as intensively during the past year as previously, so I may be a little rusty... :(

I don't know the answer to all your questions, but first things first is getting the template to show up. If you install Devel, you can turn on "Theme debug" in the devel settings, then view the source of the page near where the field is output. It should give you a bunch of direction in terms of file names you could use.

I did that, but unfortunately it doesn't show any comments for field templates, just the page and node templates.

I also, on impulse, tried posting  this same question in the Drupal forums, hoping maybe if someone there had an answer, it might also work in Backdrop (that may or may not have been a reasonable thing to hope!). Someone replied that it should be able to be done by choosing "Rendered Entity" as the display mode for the fields, but that option doesn't seem to be available in Backdrop - the only options available are Link, Plain Text and RSS Category.

I was curious about this so I searched and saw your post on d.o: https://www.drupal.org/forum/support/theme-development/2018-08-24/displa...

I think the thing the person left out is that in Drupal you have to use an entity reference field to get the "Rendered Entity" option, and not the regular term reference field.

In Backdrop, there's an entity reference module but there's no official release yet. The hope was to try to simplify the number of modules and merge all of them: term reference, node reference, user reference (in References module), entity reference. But hasn't happened yet -- maybe because most people are happy enough References.

Though you make a good case for either making entity reference official or port https://www.drupal.org/project/taxonomy_image. Though there's a doc showing how to use entity reference and taxonomy instead of the aforementioned module https://www.drupal.org/node/1224916

Actually there is another way to do this with Views and Layout. I tested it and it works. Create a view of taxonomy terms, block display, add a relationship to the content using the terms, and add a contextual filter with NID. Then add the image field from the vocabulary here. Then add this block to the layout. You can either set the visibility conditions here or create a layout just for the content type and add it there.

That sounds like a great alternative! And I was pretty easily able to set up a view that show the icons - only thing is, I had to create a new layout for the content type this will be used on, in order to be able to place the view in the right spot, and now I can't get that to take effect!

The content type is called game, and the paths to any of the game nodes that follow the pattern "games/(game title)", so I set the path in the layout configuration to games/%, and under that in the context section, set it to "Content (node)". That should be correct, shouldn't it? For applying that layout to any node with a path of "games/(something)"?

But when I view any of the pages in the games section, they're still using the default layout, and the view block I added is not showing up. Did I configure the layout settings wrong?  I made sure there were no typos, cleared caches, all the usual things you check...

For the layout, try the path to be node/% and the visibility condition to be Node type = Game.

I also tried adding a visibility condition of Node type = Game, but that just got me the error message "The condition 'Node is "Game"' does not have the required contexts at this path. Remove the condition to save the layout." Even though context is set to Content (node), so I'm not sure what the problem is...