Retrieve a value inside a field using a custom twig / Select a column by it's block name / Sonata - twig

I'll try to be clear as possible, as my question may not be simple.
I have a Sonata GridPanel, modelled (simplified) like this:
~~~~~~~~~~~~~~~
Name | Type | Blob
~~~~~~~~~~~~~~~
Boby | Aaaa | Bbbb
John | Cccc | Xxxx
Let assume the column Blob is a custom column I made which you can click on.
It is controlled by a custom twig.
My custom twig is like this
{% extends stuff %}
{% block field%}
<div class="opener">
<img
src="{{ asset('bundles/bobby/images/map_magnify.png') }}"
width="30"
height="30"
style="cursor:pointer;"
onclick="createPopUp('{{ admin.id(object) }}');"
>
</div>
{% endblock %}
As you can see, I pass the {{ admin.id(object) }} as a parameters of the function, so the function in my JS file will be aware of the value returned by the parameters.
For example, according to my grid there and assuming the ID of my object is the column Name, if I click on Bbbb, the {{ admin.id(object) }} will return Boby as a value.
This is working.
Now, I want to do quite the same thing, but not returns the ID of a whole object, but a specific value of an object.
Still according to my example, if I click on Bbbb, I would like to retrieve the value contained in the column Type for this object. The value should be then Aaaa.
But I don't know how to do this.
I tried to pass this {{ block('field') }}, but it does not target a specific column. How can I select the block by it's name.
I'm not familiar enough with twig and I'm quite lost looking inside all the Sonata's twig files.
How could I do this ?
You'll have to know to that in my case, the value I want to retrieve is "controlled" by a Sonata's Twig, and not by my custom twig.

I think you can do this using object var from the template:
{% extends stuff %}
{% block field%}
<div class="opener">
<img
src="{{ asset('bundles/bobby/images/map_magnify.png') }}"
width="30"
height="30"
style="cursor:pointer;"
onclick="createPopUp('{{ object.type }}');"
>
</div>
{% endblock %}
Take a look at row templates from sonata admin documentation for more informations

Related

Concatenate in an element acf fields or an objects

I'm trying to concatenate a Twig variable and an ACF options field but it doesn't work.
I have two footers and I want to select the correct one according to the correct page.
{% if post.slug == "page1" %}
{% set pageType = "pages" %}
{% elseif post.slug == "page2" %}
{% set pageType = "otherspages" %}
{% endif %}
<footer id="footer">
<h2>{{ options.footer_~pageType~_titre }}</h2>
<div>{{ options.footer_~pageType~_container }}<div>
</div>
The ACF fields are called footer_page_titre or footer_otherpage_titre depending on the footer I want to display
Thank you
Try constructing the field name first, for example with the twig format filter which formats a given string by replacing the placeholders, similar to sprintf, and then access the field value by in the options data array.
Construct the field name:
{% set footer_title = "footer_%s_title"|format(pageType) %}
Access the value by array key
<h2>{{ options[footer_title] }}</h2>

Set a property value in an existing twig object

I would like to define a twig object that contains two properties :
The first one is a short text
The second one is a large text (so, i need to use {% set %}{% endset %} to keep readability of the template)
I first tried like this :
{% block principal %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': ''} %}
{% set a_form_help.help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
<p>And several lines of tips for this field</p>
{% endset %}
{% endblock %}
But Twig complains about the use of the dot in the second {% set %} tag (for setting the large text content in the help_content property).
I've found this message (Twig - Why does it not allow us to set object / array values?) and then done this workaround by using a temporary variable:
{% block principal %}
{% set tmp_help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
{% endset %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': tmp_help_content} %}
{{ dump(a_form_help) }}
{% endblock %}
I've used a a temporary variable since using the merge() filter would give the same result.
Is this workaroud a good practice or is there a better/cleaner way ?
Thanks in advance for any tip|advice ! :-)
There is no clean way to do it in twig.
Is this workaroud a good practice or is there a better/cleaner way ?
It is not a good practice. As twig is a templating language its responsability is only to display data, not to structure it. Your View Object should have been created elsewhere

Dynamic variable in Twig, example?

I don't quite understand how the attribute function in Twig works. Can somebody help me with an example?
I have a field in a SQL that is named dynamic. I could be eg "field27", but I don't know the number, the number is saved in radio.id. I would like to do someting like this:
{% for radio in gruppeType.radios %}
<td><!-- value of "field" + radio.id--></td>
{% endfor %}
How can I use field + radio.id as the name of the twig-variable?
You can build the field name with a variable, then use it in the attribute function to access the data within the object/array. As example:
{% set fieldName = "field" ~ radio.id %}
{{ attribute(gruppeType, fieldName) }}
A working example can be seen in this twigfiddle
Hope this helps.

Twig translate dynamic value/string from database

Explanation:
I pull these values from my local database and try to display them on the front-end. The issue is, that I have 2 languages that I need to cater to.
Example:
{% if activeLocale == "si" %}
{{ record.estate_type_SI|raw }}
{% elseif activeLocale == "en" %}
{{ record.estate_type_EN|raw }}
{% endif %}
This works, but when I have multiple items it gets gruesome because I have to write everything down two times. What this does is that depending on the language a value from a different column in the database is pulled.
I am wondering if I can do something similar to this:
{{ record.estate_type_{{"SI"|trans}}|raw }}
I will gladly buy you a beer if you can help me out with this.
Cheers!
EDIT: Variables
Using attribute , you can access a property of an object in a dynamic way. Then you just have to use upper filter to match what you need.
{{ attribute(record, 'estate_type_'~ activeLocale|upper)|raw }}

Twig Access Array Index?

Is it possible to directly access an array index from within a Twig template?
Here's my setup, using Silex:
return $app['twig']->render('template', array('numbers' => array('one', 'two', 'three')));
so can I do something like this?
{{numbers[0]}}
Just before posting this I realized, that's exactly what you can do, but as I didn't find the answer anywhere in the docs or google (correct me if I'm wrong), I've posted this anyway.
{{numbers[0]}}
The answer of Adam, is correct, only to make it clear and improve,
you can have access directly to array index
{{ myArray[0] }}
if you need to access in a loop
{% set arrayOfItems = ['ZERO', 'ONE'] %}
{% set myArray = ['APPLE', 'ORANGE'] %}
{% for oneItem in arrayOfItems %}
<p>{{ oneItem }} equals {{ myArray[loop.index0] }}</p>
{% endfor %}
in this example I used an array inside a non related loop so the result is:
ZERO equals APPLE
ONE equals ORANGE
Thats actually something what doesnt work for me when using Twig with shopware 6.
I try to access an object like
{{ page.cart.lineItems.elements[0].quantity }}
what will lead into a parsing error of the Twig Template
I can use
{{ page.cart.lineItems.elements | first }}
to get the first Element, but dont know how i can then access a property of this first element

Resources