Problem/Motivation

Hi there, I'm trying to convert the values returned from a field into another text to display...

field returns ids: 1, 2, 3 and 4

So I need to replace:

if {{1}} then "apple";
if {{2}} then "banana";
if {{3}} then "kiwi";
if {{4}} then "orange";

So in a table would show like

banana blahblah
kiwi blahblah
apple blahblah
orange blahblah
kiwi blahblah
orange blahblah

etc....

How can I achieve that change, thanks a lot!

Comments

Andrés Chandía created an issue. See original summary.

andrés chandía’s picture

Issue summary: View changes
viren18febs’s picture

Assigned: Unassigned » viren18febs

Working on it!

andrés chandía’s picture

Thanks a lot!!!!

viren18febs’s picture

Assigned: viren18febs » Unassigned
Status: Active » Needs review

HI @Andrés Chandía

Please refer this code sample, As per the twig template, we need to apply the for loop first & then add these
conditions into the loop.

{% for item in items %}
<tr>
<td>
{% if item.field_name.0.value == '1' %}
apple
{% endif %}

{% if item.field_name.0.value == '2' %}
banana
{% endif %}

{% if item.field_name.0.value == '3' %}
kiwi
{% endif %}

{% if item.field_name.0.value == '4' %}
orange
{% endif %}
</td>
<td>
  blahblah
</td>
</tr>
{% endfor %}
andrés chandía’s picture

Thanks a lot viren18febS, and sorry for my ignorance...
but can you explain me in detail what you mean by

Please refer this code sample, As per the twig template, we need to apply the for loop first

viren18febs’s picture

You need to create the twig file first & then use above code into the twig file for replacing the values.

smustgrave’s picture

Status: Needs review » Postponed (maintainer needs more info)

@Andrés Chandía does that answer your question?

andrés chandía’s picture

Sorry, I'm learning about twig thing to be able to carry out the solution given by viren18febS
Thanks for asking...

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Fixed

No worries going to mark this answered in the meantime

Here's more links that may help
https://www.drupal.org/docs/develop/theming-drupal/twig-in-drupal

andrés chandía’s picture

Tnx!

andrés chandía’s picture

Hi viren18febS, I'm sorry but I don't seem to be able to achieve this, due to my lack of experience of course...
I could find that the twig file managing the cell I need to modify is:
views-view-field.html.twig
So I found this file and copied it to themes/d8w3css/templates/views/ where I edited it by adding the following html code along with the code you developed as shown below, whatever I put in the div has effects for all the fields in the table, but modifying the 'vid' value does not, I'm sure I'm doing something wrong, so I'm asking for a little help on this:

<td headers="view-vid-table-column" class="views-field views-field-description__value">
<div class="w3-text-pink">
{{ output -}}
{% for item in items %}

{% if item.vid.value == 'augusta_f_de_2016a_' %}
aug2016mapcas
{% endif %}

{% if item.vid.value == 'augusta_f_de_2016b_' %}
aug2016casmap
{% endif %}

{% if item.vid.value == 'febr1882mapcas' %}
feb1765mapcas
{% endif %}

{% if item.vid.value == 'febr1765casmap' %}
feb1765casmap
{% endif %}

{% if item.vid.value == 'febr1846mapcas' %}
feb1846mapcas
{% endif %}

{% if item.vid.value == 'febr1846casmap' %}
feb1846casmap
{% endif %}

{% if item.vid.value == 'smeets_ineke_2008_' %}
sme2008ingmap
{% endif %}

{% if item.vid.value == 'vald1606mapcas' %}
val1606mapcas
{% endif %}

{% endfor %}
</div>
</td>

Thanks a lot!!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.