I want to use a computed twig element in a webform to give me the value of the last item in a multi value composite field. the name of the field is destinations, and it has a departure_date field and a country field .

if I use {{ data.destination_and_date[data.destination_and_data|length].departure_date }}

it only gives me the first one.


I think you just need to use Twig's last filter.

Below is an example of getting the last value from a multiple select element. This example can also be applied to multiple value composite element.

  '#type': select
  '#title': select
  '#multiple': true
  '#options': days
  '#type': computed_twig
  '#title': computed_twig
  '#template': "Values<br/>\r\n<ul>\r\n{% for value in data.select %}\r\n   <li>{{ value }}</li>\r\n{% endfor %}\r\n</ul>\r\n\r\nLast<br/>\r\n{{ data.select|last }}<br/>"
  '#ajax': true
| improve this answer | |

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