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

StackOverflow https://stackoverflow.com/questions/21260898

Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top