SonataFormatterBundle twig extension: Template "..." is not defined - symfony

My twig template file
{{ 'Hello *world*' | format_text('markdown') }}
Error:
Template "<p>Hello <em>world</em></p>" is not defined.

There is a BC break in the SonataFormatterBundle. You can revert to version 3.2.0 or apply a patch. As mentioned in this issue.

Related

Symfony 6.2 apply Html Sanitizer on every twig var by default

Is it a way with Symfony 6.2 to apply Html Sanitizer by default on all twig var without having to add |sanitize_html?
A way to make {{ myvar }} work like {{ myvar|sanitize_html }} ?

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 ?

Render field directly in page.html.twig

How can i render a field directly in the page.html.twig?
When i try to render it like:
{{ node.field_my_field.value }}
I get:
Exception: Object of type Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList cannot be printed
When i add a .value, it does still not render right (well i use paragraphs with subfields) but get following error:
User error: "target_id" is an invalid render array key in
Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
User error: "target_revision_id" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
User error: "_loaded" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
User error: "_accessCacheability" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
So how can i render it?
In Drupal 8 & Twig you can render a lot of stuff such:
 Render field value
For example, the node title
{{ node.title.value }}
Render field entity value
For example, a field of taxonomy term categories
{{ node.field_categories.entity.name.value }}
 Render field value of Paragraph (entity)
You can't print a Paragraph entity in Twig, but you can print every fields
{{ node.field_my_field.entity.field_title.value }}
In the previous example, field_title is a field of my Paragraph.
To go further, you could render a display mode using the module Bamboo Twig.
- Project page
- Article about Bamboo Twig
- Official Documentation
Render entity with display mode using Bamboo Twig
After installing the module and enable the submodule bamboo_twig_loader.
{# Render node with nid 1 #}
{{ bamboo_render_entity('node', 1) }}
{# Render the teaser of node with nid 2 #}
{{ bamboo_render_entity('node', 2, 'teaser') }}
Render field using Bamboo Twig
{# Render the title of node 1 #}
{{ bamboo_render_field('title', 'node', 1) }}
Hopes it will help you !
In order to render the field in a twig template, you will need a contrib module; unless you go with the hook_preprocess_page() implementation.
Using Bamboo Twig definitely does your job, as already mentioned in the suggested answer. It seems a bit awkward at first that you have to enable one of its submodules in order to achieve this though.
An alternative and more common solution is to use the Twig Tweak module with which you could do the following (copying from their cheat sheet):
{{ drupal_field('field_image', 'node', 1) }}
{{ drupal_field('field_image', 'node', 1, 'teaser') }}
{{ drupal_field('field_image', 'node', 1, {type: 'image_url', settings: {image_style: 'large'}}) }}

Symfony -- Twig's trans filter not picking up locale

I have verified that if I add the following line to my twig template, it outputs the desired locale:
{{ app.request.locale }}
However, the following is outputting in English:
{{ 'String'|trans }}
If I force the locale of the trans filter:
{{ 'String'|trans({}, 'messages', 'ja') }}
It outputs in the proper translation. Note that I'm setting the locale using an eventListener:
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$locale = $request->cookies->get('_locale') ? $request->cookies->get('_locale') : $request->getPreferredLanguage($this->availableLanguages);
$request->setLocale($locale);
}
I figured out the answer through Symfony Documentation:
Setting the locale using $request->setLocale() in the controller is too late to affect the translator.
Either set the locale
Via a Listener (like above)
Via the URL (see next)
Or call setLocale() directly on the Translator Service.
I ended up fixing it by changing the priority of the service, like the accepted answer in this thread: Symfony 2.1 set locale
Mabey a late reply but I was facing the same problem, after a bit of reading i found a better solution. You can use the trans function instead of the trans filter which seems to be a cleaner solution.
{% trans from "your-trans-domain" into app.user.locale %} trans.key {% endtrans %}
See symfony docs:
Docs v2.7 for translations

Global Params in FOSUserBundle

I try to retrieve the global param that i define in parameters.yml and config.yml
in FOSUserBundle.en.yml under registration, in message i try to pass %myparam% and in my email.txt i try pass %myparam%:param like this
{{ 'registration.email.message'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl,'%myparam%':myparam}, 'FOSUserBundle') }}
but it dosen't works.
and can i insert html inside yml and new line?
thanks
In your config.yml under fosuserbundle configuration.
confirmation:
enabled: true
template: MYMailBundle:Registration:email.html.twig
By doing this you are creating a twig template where you can render your variables and format your html how you want.
If you want to create a newline in your yml file I think you could do it by adding a \n.

Resources