2

I have a show page and I want to add a custom value.

I have tried doing what I did in other actions which is to add an array to the third parameter with the data key like so:

protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->add('name')                      
        ->add('dateEnd')
        ->add('example', null,
            array('data' => 'example value')
        )
    ;
}

In the configureListFields action, this works. I have injected custom values with the data attribute.

But still I am not able to access key example in the show.html.twig file. It gives me this error

Variable "example" does not exist.

What should I do to access this custom variable in the twig file ?

Kris Peeling
  • 1,025
  • 8
  • 18
Sandeep Gajera
  • 229
  • 4
  • 14

2 Answers2

1

Try

{{ elements.elements.example.options.data }} 

in your twig template

Jim Panse
  • 2,220
  • 12
  • 34
1

I used this solution. In the configureShowFields() method of an Admin class:

$showMapper
        ->with('Tab Name')
        ->add(
            'any_name',
            null,
            [
                'template' => 'Admin/Custom/any_name_show_template.html.twig',
                'customData' => $this->someRepository->getSomeEntityBy($field),
                'anotherCustomData' => $this->someService->getSomeDataBy($value),
            ]
        )
    ;

In the custom template, you can access custom data by field_description.options.<customFieldName>, so for provided example data accessors would be {{ field_description.options.customData }} and {{ field_description.options.anotherCustomData }}

For the shorter field name in the Twig template, you can do like this:

{% set customData = field_description.options.customData %}

and access the custom data like {{ customData }}

Hope this helps and saves time.

Serhii Smirnov
  • 1,338
  • 1
  • 16
  • 24