I'm using Drupal 8 and I have a taxonomy field in a content type. I have created a node and added the term reference value to this node. Now the node shows up the link to the term.

I want to show value of the text field added in that taxonomy instead of link to the term.

I want to use this hook and set the value of the term field via code.

function hook_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) { }
  • 1
    You can choose how to display the entity reference field in your node's display settings. One of the options available is "Rendered entity". This will show the taxonomy entity. Additionally you can select a display mode for the rendered entity. So the only thing left is adding an according display mode to your taxonomy vocabulary. No need to use any node view hooks in this case. Dec 19, 2017 at 13:50
  • So far, you described what you want to achieve, but you didn't say what you don't understand, or what problems you find in implementing what you describe. May you add an explicit, not vague, question to the question body? So far, we can just say Yes, it's the right hook. or No, that is the completely wrong hook to use for that purpose.
    – apaderno
    Dec 19, 2017 at 18:39
  • @MarioSteinitz : I know that...The point is I have a text field in taxonomy entity called "caption_text" and instead of the entire rendered entity of the taxonomy, I just want the field "caption_text" to be displayed using the above mentioned hook. My entire taxonomy entity has various fields like title, image, description and caption text. When I display the term field in a page type node, I need to display the caption text field and not the link to term or the entire rendered entity. And I want to use this hook for this purpose. Dec 20, 2017 at 5:34
  • That is what I was talking of. I wasn't at my desk yesterday when writing the comment, so I could not write a more detailed answer. Did so now. Please check my answer below. Dec 20, 2017 at 5:55

2 Answers 2


You don't have to use the node view hook. It can be achieved using a taxonomy display as well.

Here is how to do it:

  • Navigate to "Administration / Structure / Display modes / View modes"
  • Scroll down to "Taxonomy term"
  • Add a new Taxonomy term view mode by clicking the according link and giving it a name
  • Now head to the display settings of your Taxonomy at "Administration / Structure / Taxonomy / [Your vocabulary] / Manage display"
  • Find the "Custom display settings" tab, tick your newly created view mode and save
  • A new tab with the name of your view mode will appear, where you can just add the field you like to display and disable all others; save again
  • Head to the manage display settings of your content type that features the entity reference field to this taxonomy at "Administration / Structure / Content types / [Your content type] / Manage display"
  • Find your entity reference field and switch "Format" to "Rendered entity"
  • Using the cogwheel at the very right, you can now choose your above created view mode for how to render the referenced taxonomy entity

Save and you have the rendered field of your taxonomy shown at the node page.

PS.: The above procedure can be used for many other referenced entities as well and does not require any custom programming.

  • Thanks for the reply :)...I am well aware of this way of doing it, but I specifically am looking to achieve it by hook_node_view as I am exploring it. Dec 20, 2017 at 9:58

What I was trying to do is that change the entire value of the term field (field_tax) with the field value of a field (field_tax_text). But I am not able to do that.

The max I could change is the value of 'field_tax_text', but for that I have to render the value of term field 'field_tax' as rendered entity instead of link to term in display settings of content type.

I have done by two ways programmatically

1st Way:

function template_preprocess_node(&$variables) {
$variables['elements']['field_tax'][0]["#taxonomy_term"]->set('field_tax_text','new value');

2nd Way:

function module_node_view(array &$build,\Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
if ($entity->getType() === 'page') {

  $entity->get('field_tax')->referencedEntities()[0]->set('field_tax_text', 'new value');


But instead of doing all this I would go with @Mario Steinitz solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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