Change value of array element at specific index - twig

I want to change the value of array element at a specific position but whenever i am trying to so it throws me an error. Can anyone suggest me a way to change the value
{% set explode_field_id = data.field_id|split('-') %}
{% set explode_field_id_length = explode_field_id|length %}
{% set reqValue = explode_field_id_length - 1 %}
{% set explode_field_id[reqValue] = data.field_details[dependent_field] %}
{% set dependent_field_id = explode_field_id|join('-') %}
Here explode_field_id = categoryList-category_list-0-category_list
ERROR - Unexpected token "punctuation" of value "[" ("end of statement block" expected).

Related

How to change value in this array

{% set dataSrc = [{"campgroup0":"far", "total":3},{"campgroup0":"close", "total":4},{"campgroup0":"unknow", "total":8}] %}
I want to change total value of item 0 {"campgroup0":"far", "total":3}
I'm trying:
{% set item = {"campgroup0":"far", "total":0} %} {% set dataSrc = dataSrc|merge(item) %}
but just push another item

Twig strip textarea and create array

I'm trying to strip some text from a value and afterwards create an array from it with the stripped values.
I'm having troubles to strip the text value.
I can only do this on the frontend since I have no access to the backend (SaaS platform)
In below example value.value (originally a textarea) returns the following text:
[185047078]1x something - Type 1
[415533322]1x something - something
[152890667]1x something 500x500 mm
I want to strip the text so I have [185047078], [415533322], [152890667] left or without the brackets.
Normally in JS you would do something like:
hide_ids = txt.match(/[^\]\[]+(?=\])/g)
However it need to be done in Twig.
Afterwards I want to push the values into an array hide_ids.
{% set hide_ids = [] %}
{% if product.custom %}
{% for custom in product.custom %}
{% if 'Some title' in custom.title %}
{% for value in custom.values %}
{% set hide_this_id = value.value %}
{% if hide_this_id matches '{/[^\]\[]+(?=\])/g}' %}
{% set hide_ids = hide_ids | merge([hide_this_id]) %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% set hidden = false %}
{% if id in hide_ids %}
{% set hidden = true %}
{% endif %}
What is the equivalent of match in Twig? I also tried replace but I just can't get that text stripped.
Any help greatly appreciated!
You could go with some simple string functions.
{% for id in ids %}
{{ id | split(']', 2)[0] | replace({'[': '',}) }}
{% endfor %}
demo
split is the explode of twig. This will separate your string in chased based on the ] character. The 2nd parameter (2) ensures there will only be maximum 2 parts in the array.
replace is just str_replace
If you wanted to solve this with regex you would either need to write a function/filter with preg_match or install an extension like this

Set Twig object property name using variable

I need to set an object property name dynamically in Twig:
{% set featureId = feature.id %}
{% set gridEnabled = gridEnabled|merge({featureId: true}) %}
But that sets "featureId" as a property of gridEnabled. Is there a way to tell Twig that featureId is a variable? I'm surprised it interprets that as a string without quotes.
Follow-up question: Here is the full set--I was able to further reduce to "feature.id". Can these lines be combined?
{% set gridEnabled = grid.enabled %}
{% set gridEnabled = gridEnabled|merge({(feature.id): true}) %}
{% set grid = grid|merge({'enabled':gridEnabled}) %}
Very easy actually,
{% set gridEnabled = gridEnabled|merge({(featureId): true}) %}
(edit) follow-up
{% set grid = grid | merge({'enabled' : (grid.enabled | merge({(featureId):1,}))}) %}

TWIG, dynamic associative array key

I need to create a array with a dynamic key.
Example:
{% set key = 'a' %}
{% set value = 'b' %}
{% set array = {key:value} %}
I find a workaround for this problem: if you surround the key with parenthesis the key of array take the value of the variable instead of the name. So try simple this:
{% set array = { (key): value} %}
Here a working solutions

Twig: how to set object variable?

I try to create new variable from exists object variable in Twig template (filter is object):
{% for filter in filters %}
{% if filter.type != 'selectImage' %}
{{ filter.render()|raw }}
{% elseif filter.type == 'selectImage'%}
{% set selectFilter = filter %}
{% endif %}
{% endfor %}
but i get error:
Catchable fatal error: Object of class Filters\Filter could not be converted to string in vendor\twig\twig\lib\Twig\Environment.php(403) : eval()'d code on line 40
on
{% set selectFilter = filter %}
How i can set object to new vairable?
According to the official TWIG documentation, filter is the name of a tag in TWIG language.
You should rename your variable in your code to avoid problems in the generated PHP code:
{% set selectFilter = myFilter %}

Resources