I have a Form in my twig template.
One of the form fields is a dropdownlist that is created from an Entity in my form builder.
I would like to set the default value in my dropdown list, to be the id 28 for example.
I don't know how to do this in twig.
I tryed this:
{{ form_widget(form.type, {value: 28 } ) }}
But nothing changed, I still have the firs value that is by default.
I know that I can set a default value in the FormType class where I am creating my form builder, but I am looking for a twig way, it seems to me more elegant than to create the object in the formType class.
Ok, found my mistake, the code should be:
{{ form_widget(form.type, {value: "28" } ) }}
I forgot the quotes for the id. And also, there is a need to CTRL + F5 to refresh the page and see the difference.
{{ form_widget(form.type, {value: twig_var ~ "" } ) }}
or
{{ form_row(form.type, {value: twig_var ~ "" } ) }}
Related
I am getting a url variable via GET on my controller and want to set it as value of my textarea. Here's my textarea line:
{{ form.myTextArea is defined ? form_row(form.myTextArea) }}
{{ form_row(form.myTextArea, {'value': 'your text'}) }}
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 ?
Try
{{ elements.elements.example.options.data }}
in your twig template
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.
I have a working Drupal 8 site with the following in THEME.theme:
function THEME_preprocess_node(&$variables) {
$vars['get']['vguid'] = \Drupal\Component\Utility\XSS::filter($_GET['vguid']);
}
If I add the same line to THEME_preprocess_page() I get "Undefined index: in 'THEME_preprocess_page' when I attempt page loads.
In the the twig I have {{ get.vguid }} and have also tried {{ app.request.parameter.get("vguid") }}
In the twigs (after cache is cleared) I get no value when page is accessed like /mobile-video?vguid=15991b1f-2ad2-11e7-8da8-22000aeb1f8b
How do I get a sanitized query string value into my twig?
I did it like this and I had no problems:
function THEME_preprocess_TEMPLATE(&$variables)
{
$variables['query__param'] = XSS::filter($_GET['keys']);
}
In your function you are using $vars and defining $variables as a parameter, maybe you should change that? $vars['get']['vguid'] => $variables['get']['vguid']
{{ app.request.query.get("vguid") }}
Is it possible to translate the value of twig variables in a template with the 'trans' tag?
Say for instance I am passing a product to my template. This product has a definition with a trans tag e.g {{ product.definition|trans }}. This definition could either be in EN or DE or some other language. How could I translate the definition.
What are you trying to do is not a good way, It would look like this:
messages.en.yml
product:
definition:
some_value1: Some value 1
some_value2: Some value 2
and in template, you would do something like this:
{% set definition_value = product.definition %}
{% set trans_definition = 'product.definition.' ~ definition_value %}
{{ trans_definition|trans }}
it'll work, if it finds the key. What if it cant find it?
That's why you should use DoctrineBehaviors from KnpLabs, which handles all the dynamic translations for you..
If {{ product.definition }} equals 'cellphone' the following should work.
message.language.yml:
'cellphone': This will work!
However if you want to map it with the 'product' key in your message file like this:
product:
'cellphone': This also works
add the key to the twig template like so:
{{('product.'~product.definition)|trans }}
In my database, I have a field containing the following data : 000010 (the type is integer)
In my twig tpl, I want to display it, then I do : {{ spending.documentName }}
But the browser displays "10". As if Twig was automaticcaly performing a trim on my data.
I tried {{ spending.documentName|raw }} but it doesn't work. I dont find anything on Google about how to keep leading 0 with Twig.
Does anyone know how to proceed ?
I think you must force the format (as your type is integer).
You can use the format filter :
{{ "%06d"|format(spending.documentName) }}
Or better create a twig extension (http://symfony.com/doc/current/cookbook/templating/twig_extension.html):
public function strpad($number, $pad_length, $pad_string) {
return str_pad($number, $pad_length, $pad_string, STR_PAD_LEFT);
}
It's clearer in your template :
{{ spending.documentName | strpad(6,'0') }}
You need to use the format filter.
Since placeholders follows the sprintf() notation, you should be able to convert sprintf('%06d', $integer); in PHP to {{ '%06d'|format($integer) }} in Twig.
Kinda late here, but ran into the same.
With twig 3.x you can use:
{{your.number | format_number({min_integer_digit:'2'})}}