Add an attribute to an input element from a twig file - symfony

I have a form which is generated from inside a controller.
Then in twig file I need to add an attribute (placeholder) to a field (textarea).
What is the best way to do it in twig file?

{{ form_row(form.yourField, { 'attr':{'placeholder':'sample value'} }) }}

Related

passing value from custom preprocess theme hook function to a template in drupal 9

Situation: I want to write a proprocess PHP function [theme]_preprocess_example__p1 that adds variables to a page template file example--p1.html.twig but the variable cannot display properly if I inlcude the template to page--front.html.twig.
page--front.html.twig
I added the following code in page--front.html.twig
{% include '#[theme]/directory/example--p1.html.twig' %}
example--p1.html.twig
this file has one variable {{ text }}. to display the text message.
[theme].theme
I added the following code in [theme].theme
[theme]_preprocess_example__p1(array &$variables, $hook){
$variables['name'] = 'message';
$variables['text'] = 'example p1 message';
$variables['data'] = 'm1';
}
but I cannot display text variable to the template example--p1.html.twig when I added include {% include '#[theme]/directory/example--p1.html.twig' %} in page--front.html.twig.
Someone suggest creating all the variables to ExampleController.php and use example.module to define hook_theme() function and function [theme]_preprocess_example__p1() but it is failed to display the variables when I include the template example--p1.html.twig for duplicated code to page--front.html.twig template. How can I solve it?

Symfony - display file stored outside public

Is there a possibility to display file in twig template when the the file is not stored inside public directory? Like call controller which would return binary response for requested file?
If you want to embed a controller response into a twig template you can use the render function.
{{ render(path('latest_articles', {max: 3})) }}
or
{{ render(controller('App\\Controller\\BlogController::recentArticles', {max: 3})) }}
Ref: https://symfony.com/doc/current/templates.html#embedding-controllers

How to change template of controller rendered in twig file (Symfony 3)?

I know that we can render the content of a controller in twig file like this:
{{ render(controller('FOSUserBundle:Security:login',{"baseTemplate": true})) }}
However, I don't know if we can pass the new template so that the controller will use it instead of the default. Anyone tried to override template in this way?
I don't really understand the issue here
If you do
{{ render(controller('FOSUserBundle:Security:login',{"baseTemplate": true})) }}
You could aswell do:
{{ render(controller('FOSUserBundle:Security:login',{"template": "your_template.html.twig"})) }}
Or
{{ render(controller('FOSUserBundle:Security:login',{"templateNumber": "4"})) }}
Where templateNumber is used in a condition inside your controller ?

Passing all parameters as array and changing one of them

I am passing an array of get arguments from controller to twig template, and then create a link:
{{ url('route_name', array_of_get_parameters) }}
It works, but what if I want to pass all BUT ONE of those parameters unchanged? Something like:
{{ url('route_name', array_of_get_parameters, {'param1': 'value'}) }}
The example above doesn't work of course...is there a way to do this?
Use twig merge filter like this:
{{ url('route_name', array_of_get_parameters|merge({'param1': 'value'})) }}
You cannot do this.
Instead override the value in the controller (<- better) or in the twig template, before the url generation.

How to get current bundle in Symfony 2?

How can I detect in which bundle am I?
for exemple, when I'm in web.com/participants/list, I want to read "participants".
In order to get the bundle name in the controller:
// Display "AcmeHelloBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
And inside a Twig template:
{{ app.request.get('_template').get('bundle') }}
In order to get the controller name in the controller:
// Display "Default"
echo $this->getRequest()->attributes->get('_template')->get('controller');
And inside a Twig template:
{{ app.request.get('_template').get('controller') }}
In order to get the action name in the controller:
// Displays "index"
echo $this->getRequest()->attributes->get('_template')->get('name');
And inside a Twig template:
{{ app.request.get('_template').get('name') }}
AFAIK it's not yet possible (at least in a easy way). You should use reflection. I wrote a quick and dirty service to do get bundle name ang guess entity/repository/form names based on my conventions. Can be buggy, take a look at: http://pastebin.com/BzeXAduH
It works only when you pass a class that inherits from Controller (Symfony2). Usage:
entity_management_guesser:
class: Acme\HelloBundle\Service\EntityManagementGuesser
In your controller:
$guesser = $this->get('entity_management_guesser')->inizialize($this);
$bundleName = $guesser->getBundleName(); // Acme/HelloBundle
$bundleShort = $guesser->getBundleShortName(); // AcmeHelloBundle
Another possibility would be using kernel to get all bundles: Get a bundle name from an entity
Well you can get the controller of the current route by,
$request->attributes->get('_controller');
You can parse the bundle name from it.
You can get the bundle name in the controller simply like that:
// Display "SybioCoreBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
And inside a Twig template:
{{ app.request.get('_template').get('bundle') }}

Resources