Value from query string not seen in twig output - symfony

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") }}

Related

Timber how to implement get_current_url wp

Ive been using Wordpress for more than a year now. But I was stuck with the implementation of Timber twig framework get the current URL. I tried these codes below codes but no luck,.
{{ site.url.current }}
{{ app.request.getRequestUri() }}
Twig templates engine: get current url
Have you tried:
URLHelper::get_current_url()
Doc: https://timber.github.io/docs/reference/timber-urlhelper/#get_current_url
So, you should be able to feed this as a variable into your template.
Or if you want to get a step further and extend Timber's Twig i.e. creating a filter or function like:
$twig->addFilter(new \Twig_SimpleFilter('is_current_url', function ($link) {
return (URLHelper::get_current_url() == $link) ? true : false;
}));
Which should bring things down to:
{{ 'http://example.org/2015/08/my-blog-post' | is_current_url }}
BTW: Internally, get_current_url() returns: $_SERVER['HTTP_HOST']/$_SERVER['SERVER_NAME'] + $_SERVER["REQUEST_URI"]
Adding ['current_url'] in functions.php under add_to_context function worked for me:
public function add_to_context($context)
{
// $context['foo'] = 'bar';
$context['current_url'] = Timber\URLHelper::get_current_url();
$context['site'] = $this;
return $context;
}
You then will be able to use it globally in your twig templates:
<pre>
Current Url: {{ dump(current_url) }}
</pre>

call twig function from custom twig function

I created a custom twig function in AppExtension Class. I need to call form_label() from this new function. ¿Is it posible? I tried but does not work:
from template I call:
{{ myFunc(form.someField) }}
public function myFunc( $field )
{
$html = form_label($field);
}
The idea is to render each form field in a different order/way than the form_widget(form) twig function. The "form_label()" function it's not reconized.
Thx for any suggestion.
I feel like this is the wrong approach to handle this. Extensions are for transforming data not really to manipulate the form definition itself.
First of all the order is defined as in the form type, so you can swap those around. To render the fields differently you can use form themes, or even rendering a custom form type.
Alternatively if its a one time thing (you could also create a macro for this) you can also instead of form_widget(form) order them in the way you like.
{{ form_start(form) }}
{{ form_row(form.field3) }}
{{ form_row(form.field1, { attr: { class: 'im-different' } }) }}
{{ form_row(form.field2) }}
{{ form_end(form) }}
Or even go deeper.
{{ form_start(form) }}
{{ form_row(form.field3) }}
<div>
{{ form_label(form.field1) }}
{{ form_widget(form.field1) }}
{{ form_errors(form.field1) }}
</div>
{{ form_row(form.field2) }}
{{ form_end(form) }}
To see these functions and how they all rendered by default you can look at form_div_layout.html.twig.
I agree with Jenne van der Meer and Nico Haase that your approach isn't particularly optimal. If I had the choice, I would go a different route: Instead of rendering in your function, render in twig, then pass the result to the function (like {{ myFunc(form_label(form), form) }}). Since you omit what your function actually needs and/or does, it's hard to provide further advice. However, I'm absolutely sure, that rendering can be done in twig before or after entering your function, via a macro/block, maybe even a form theme).
However, if you really really require your function to render the form field ... the following will possibly help you. I strongly advise against doing this, there's probably a better suited solution.
The form_label function is slightly more complex than a simple function. Instead, it uses twig's compile mechanisms to generate specific php code. It will eventually call:
FormRenderer::searchAndRenderBlock(FormView $view, string $blockNameSuffix, array $variables = [])
Diving deep into the compiler, the template call form_label(form, options) would be turned into:
$this->env->getRuntime('Symfony\Component\Form\FormRenderer')->searchAndRenderBlock(
$form, 'label', $options
);
where the $this->env seems to be the twig environment. That means, to call this in your twig extension you need to have access to the proper Twig environment, and then it should already work with the recipe I just provided. Especially if you can omit the options argument, I didn't take a deeper look into how that one's assembled (but it's probably just straight forward).
So your twig function must be defined via:
public function getFunctions(): array
{
return [
new TwigFunction('myFunc', [&$this, 'myFunc'], [
'needs_environment' => true, // <--- this!
'is_safe' => ['html'],
]),
];
}
public function myFunc(\Twig\Environment $env, $field) {
// other stuff
$html = $env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(
$field, 'label', $options
);
return $html;
}

Cannot render text filtered with trans in twig template using Sandbox Security Policy

I am a little bit confused. First of all, look at my code, I guess.
public function renderTemplate($templateType, $data)
{
$layoutName = "$templateType.layout.html.twig";
$policy = new \Twig_Sandbox_SecurityPolicy(
['if', 'for', 'block', 'set', 'extends'],
['escape', 'format', 'dateformat', 'trans', 'raw', 'striptags'],
self::$allowedMethods,
self::$allowedProperties,
['gettext']
);
$sandboxExt = new \Twig_Extension_Sandbox($policy);
$intlExt = new \Twig_Extensions_Extension_Intl();
$i18nExt = new \Twig_Extensions_Extension_I18n();
$twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__ . "/../Resources/views/Something/", "__main__"));
$sandboxExt->enableSandbox();
$twig->addExtension($sandboxExt);
$twig->addExtension($intlExt);
$twig->addExtension($i18nExt);
try {
$result = $twig->render($layoutName, $data);
} catch (\Exception $e) {
\Doctrine\Common\Util\Debug::dump($e);die();
}
return $result;
}
Here it is the template I want to render
{% extends 'layout.html.twig' %}
{% block title %}{{ entity.id }}{% endblock %}
{% block bodyTitle %}
{{ entity.id }} {{ 'translation_key.created_at'|trans({}, 'entities', locale) }} {{ entity.createdAt|dateformat(null, locale) }}
{% endblock %}
Here, as you can see, I want to render a template according to its type. The problem is: half of the template renders just fine, and then, when it tries to render the translated string, it throws an error.
Fatal error: Call to undefined function gettext() in /home/dev/vhosts/my-project/vendor/twig/twig/lib/Twig/Environment.php(403) : eval()'d code on line 69
I checked if this function existed right before trying to call render method, and it was really undefined. Basically, I have 2 questions here:
Question 1
How does it work in other parts of my project, but not in this specific handler? See "Important update" below.
Question 2
Can I solve my problem in another way? For example, without using Sandbox or using Sandbox with some kind of a flag "everythingAllowed=true"?
ATTENTION! IMPORTANT UPDATE
Previously, I misunderstood my own question. I thought the error was thrown while rendering the variable, but I re-checked the situation (When Alain Tiemblo asked me for twig template code here in comments) and now 100% sure it's thrown while trying to translate smth. Also, I have translations all over my project, and it works just fine, but in this specific situation it's not. I think it worth to mention, that I also tried to render the template without using Sandbox. I tried to render it directly from Twig Engine like this
return $this->templating->render($layoutName, $data);
//$this->templating is injected in the constructor via services.yml like this
//arguments:
// - "#templating"
The result - not properly translated text. When I dumped "locale" - I got one specific language, but the text was translated in another. But at least using this method - I didn't get any errors.. Can anybody clarify this for me? Because I really don't understand how the Intl/i18n extension works and why it doesn't want to work in Sandbox or not in Sanbox?
P.S. My guess, why it doesn't work directly from Twig Engine - probably I should inject not like "#templating" or it injects just right, but the Intl or I18n extensions are not enabled? How to enable. And I have no clues why it doesn't work with Sandbox

How to generate routes ignoring extra passed parameters

Lets say that i have the following route annotations for a controller action:
* #Route("/post/{post_id}/", name="post.view")
* #Route("/post/", name="post.current_view")
And I want to use twig to generate the url for this:
{{ url(basePath~'view', {'post_id':post.postId}) }}
//basePath will either be "post." or "post.current_"
What i currently get is:
domain.com/post/1/
domain.com/post/?post_id=1
What i want though is for the second route to be generated ignoring any "EXTRA" parameters passed to it so that i would only get:
domain.com/post/
Does anyone know if this is something that can be natively accomplished? I know i could right a custom twig function that uses the router and then i can generate the routes and strip the query string but i want to avoid that if there is an easy toggle somewhere that i have missed.
Solution #1 you could just add an if clause
{% if BasePath == 'post.' %}
{{ url(BasePath~'view', {'post_id':post.postId}) }}
{% elseif BasePath == 'post.current_' %}
{{ url(BasePath~'view') }}
{% endif %}
maybe not the most elegant but should work.
Solution #2
spliting url with question marks and getting the first string
{% set myUrl = url(basePath~'view', {'post_id':post.postId}) %}
{{ myUrl|split("?")|first }}
Solution #3 Or you can override the url function by extending RoutingExtension class of twig.
Symfony\Bridge\Twig\Extension\RoutingExtension
can find an example here with path but url should be the same.
You should override this function
public function getUrl($name, $parameters = array(), $schemeRelative = false)
{
return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
}
your function could look like this:
public function getUrl($name, $parameters = array(), $schemeRelative = false)
{
$yourUrl = parent::getUrl($name, $parameters = array(), $schemeRelative = false);
return strstr($yourUrl, '?' , true);
}
what id oes it removes everything afther the question mark.
To override the default class you have to add to the parameters
twig.extension.routing.class: MyNamespace\MyRoutingExtension
I guess not, you need a preg_replace filter and this isn't natively defined

SonataAdminBundle dump template variable

I tried dump variables in block template (block_core_children_pages.html.twig) by {{ dump() }} but it turn into blank page. Anybody have same problem? and I have some questions also:
How to pass a variable from function: {{ sonata_page_render_container('footer', 'global' ) }} to template ?
What are variables is passed by default to block template?
Thanksss alot.
The sonata_page_render_container function takes the following arguments:
public function renderContainer($name, $page = null, array $options = array())
So you have the third argument to specify some options/settings to add to your block, like this:
{{ sonata_page_render_container('footer', 'global', {mysetting: myvalue}) }}
After that, you can modify your execute() BlockService's method to use the settings you passed.

Resources