1

My goal is to colour code vocabulary terms based on a field_topic_colour which I've added to the vocabulary. There are other vocabularies which do not have this field. So, I need to check and see if it exists for a certain term and then fetch the value so I can create my classes and get the buttons to have the correct color.

With kint I can see the value, but I cannot figure out how to drill down to it in twig or via preprocessing. All the Qs i've found deal with vocab terms in nodes, not in the terms themselves.

Here is my kint screen shot: enter image description here

I'm trying to get to "primary" (which is the keyword to tell my Bootstrap subtheme what color to use) under the field_topic_colour.

What exactly must I write in the preprocessing function?

function MYTHEME_preprocess_field__entity_reference($variable) {
  //I need code to return a string like this (I think) where "primary"
  //is the value from my custom field in the term.
  $color = ????? (primary)
  $mytag = 'class="btn- . $color . ">TERM-NAME...TERM_URL...
}

I can clean the php on my own, didn't worry about it in the above example. I just need to get the value for my field...

I've checked the cheatsheets here: https://wizzlern.nl/sites/wizzlern.nl/files/artikel/drupal-content-entity-8.0.pdf but it seems i really need some specific examples and EXPLANATIONS of why something works, so I can hopefully start to logically figure it out next time.

Razeem Ahmad
  • 391
  • 1
  • 11
Rillieux
  • 587
  • 9
  • 23

2 Answers2

0

You can access the variables like this $term->field_topic_colour->value Since it is in an array it should be accessible like this $term->field_topic_colour[0]->value

function MYTHEME_preprocess_field__entity_reference($variable) {
  $term = \Drupal::routeMatch()->getParameter('taxonomy_term');
  $color = NULL;
  if(isset($term->field_topic_colour[0]->value) {
    $color = $term->field_topic_colour[0]->value;
  }
  $mytag = 'class="btn- . $color . ">TERM-NAME...TERM_URL...
}
Razeem Ahmad
  • 391
  • 1
  • 11
  • Thanks, RA. This works when showing the vocabulary item as a node, but I guess I should explain my needs more clearly. I have a page showing content type "Exhibit" and each exhibit node has one or more entity reference fields to vocabulary. So, I'm trying to preprocess this field so that it pulls individual vocabulary terms and colors them according to the topic_colour field. Here you can see the difference. I've got the colours hard coded for now, but the 'primary' value only shows when the "Analysis" page is open, not on the content type page. https://i.imgur.com/MKwvH0T.png – Rillieux May 02 '18 at 18:42
  • Also, the term "Analysis" has the colour tag "primary", but the other tags do not and should not show anything next to them, except floor, which should show *its* tag of "danger". As you can see above, "primary" is showing next to each tag on the "Analysis" page (you see the results for a teaser of the content type tagged with "Analysis" whereas on that content page's node nothing is showing at all. – Rillieux May 02 '18 at 19:03
  • Adding the answer now. My final code in the field--entity-reference.html.twig file: `{% for item in items %} {% set mylabel %} {{ item.content }} {% endset %} {% set myclass %} {{ item.content['#options'].entity.vid.0.value['target_id'] }} {% endset %} {% set myclass = myclass|replace({'_':'-'}) %} {% endfor %}` – Rillieux May 18 '18 at 06:36
0

Adding the answer now. My final code in the field--entity-reference.html.twig file:

{% for item in items %}
  {% set mylabel %}
    {{ item.content }}
  {% endset %}
  {% set myclass %}
    {{ item.content['#options'].entity.vid.0.value['target_id'] }}
  {% endset %}
  {% set myclass = myclass|replace({'_':'-'}) %}
    <div{{ item.attributes.addClass('taxonomy--item') }}>
      <a class="btn-small btn-primary tag-{{ myclass|trim }}" href="{{ item.content['#url'] }}" role="button">{{ mylabel|striptags }}</a>
    </div>
{% endfor %}

HERE is the code needed in a node to access the parent vocabulary of the taxonomy terms in the node. (That is, individual tags on a content type node).

item.content['#options'].entity.vid.0.value['target_id']

NOTE: This is on Drupal 8.5.3 and none of my "tags" have more than one parent.

Rillieux
  • 587
  • 9
  • 23