Symfony CMF menu translation - php

I try to set up a simple CMS on our web application using the Symfony CMF.
I can successfully load the fixtures in multiple languages.
$parent = $dm->find(null, '/cms/pages');
$rootPage = new Page(array('add_locale_pattern' => true));
$rootPage->setTitle('main');
$rootPage->setParentDocument($parent);
$rootPage->setName('main');
$rootPage->setBody('');
$dm->persist($rootPage);
$aboutPage = new Page(array('add_locale_pattern' => true));
$aboutPage->setTitle('About');
$aboutPage->setParentDocument($rootPage);
$aboutPage->setBody('About us DE');
$aboutPage->setName('about');
$aboutPage->setLabel('About');
$dm->persist($aboutPage);
$dm->bindTranslation($aboutPage, 'de');
$aboutPage->setBody('About us FR');
$aboutPage->setLabel('About FR');
$dm->bindTranslation($aboutPage, 'fr');
I can also display them in the right language (current locale) on the front page.
This is my controller action:
public function pageAction(Request $request, $contentDocument) {
return $this->render(':Frontend/CMS:index.html.twig', ['page' => $contentDocument]);
}
And this is my working twig File:
{{ page.body }}
Screenshot of the working page
But as soon as I try to render a menu on my page, it will show the text in the default language.
{{ knp_menu_render('main') }}
{{ page.body }}
Screenshot of the non working page
The menu is configured as follow:
cmf_menu:
persistence:
phpcr:
menu_basepath: /cms/pages
The output of app.request.locale is always fr. No matter if I include the menu or not.
Does anyone have an idea what could cause this problem?

Related

Symfony4 translation in twig

As for Symfony4 translation, thanks to this article. It works well in Controller.
public function index(TranslatorInterface $translator)
{
$translated = $translator->trans('test');// it works
print $translated;exit;
in messages.en.yaml
test: englishtest
However I can't translate message in twig.
<br>
{{ test|trans }}
<br>
It shows the error Variable "test" does not exist.
I need to do something in advance for translation in twig???
The method signature looks like this:
{{ message|trans(arguments = [], domain = null, locale = null) }}
See https://symfony.com/doc/current/reference/twig_reference.html#trans
So if test is not a variable, then {{ 'test'|trans }} should work (as zalex already pointed out).

OctoberCMS global page properties?

Is it possible to set a series of global properties (such as social media usernames) that are available to all page views in OctoberCMS rather than having them associated to one CMS page or Static Page at a time?
For example, being able to use {{ twitter_username }} in any template, but it wouldn't show up as a field in any page form on the backend.
UPDATE: this can be achieved by registering a Twig function using registerMarkupTags in your plugin:
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public function registerMarkupTags()
{
return [
'functions' => [
'globals' => function($var) {
switch ($var) {
case 'twitter_username':
return 'mytwitterusername';
}
return null;
},
],
];
}
}
In this case, calling {{ globals('twitter_username') }} from any template prints mytwitterusername.
Hmm yes better you need to add code to life-cycle method in layouts, so now page which are using that layout will have this info already loaded.
In layout code block you can use something like this
use RainLab\Pages\Classes\Page as StaticPage;
function onStart() {
$pageName = 'static-test'; // this will be static page name/filename/title
$staticPage = StaticPage::load($this->controller->getTheme(), $pageName);
$this['my_title'] = $staticPage->viewBag['title'];
$this['twitter_username'] = $staticPage->viewBag['twitter_username'];
}
now inside your cms page you can use this variable
<h1>{{ my_title }} </h1>
<h3>{{ twitter_username }} </h3>
let me know if it you find any issues
You could also use theme config file which gives you more flexibility rather than hardcoding the values in to the code block.
https://octobercms.com/docs/themes/development#customization

Symfony2 getting the url of request in subrequests in twig

How can I get the current url in a subrequest and change parameters with it(for example locale).
I have main action that renders the template.
public function indexAction() {
return $this->render('MpShopBundle:Frontend:index.html.twig', array(
'products'=>$products,
'locale' => $locale,
));
}
In the template I render the navigation like this:
<span class="top">{% render controller("MpShopBundle:Navbar:navbar") %}</span>
Now in this navbar template I am trying to do this:
{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'_locale': 'en'})) }}
I am getting an empty url because this is a subrequest... How can I pass the URL to the subrequest? This is the first time im dealing with them..

Laravel form sort of submitting in debug mode but doesn't work in normal mode

so I have a selection box that gives a dropdown menu to give messages a manager from the dropdown. It takes the input and then changes to a column in the database called manager for it's respective column. When I try to submit the selection menu it gives me the regular error for Laravel. But then when I put ?debug=1 at the end it submits but gives the row's manager column a value of just blank.
Here is what I have in the routes.php
Route::get('foo/{id}', 'fooController#bar');
Route::post('foo/{id}', 'fooController#bar');
This is the form.
{{ Form::open(array('url' => '/admin/foo' . $message->id)) }}
{{ Form::select('handler[]', array('unassigned', 'foo', 'bar'), null, array('style' => 'width: 127px')); }}
{{ Form::submit('Change manager') }}
{{ Form::close() }}
{{ $message->manager }}
and here is what is in the fooController
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler;
$message->save();
return Redirect::action('AdminController#foo_bar');
}
I had a problem like this the other day, I have zero recollection of what I did. I really appreciate any help, thanks! The database is postgresql if that's any help
Try a dd(Input::all()) at the beginning of your controller and make sure you're seeing what you expect.
Also since you're sending an array perhaps you have to do Input::get('handler.0') -- see here right below the Input::only() and Input::except() code block.
It would seem as though because you are naming your select handler[], PHP is grabbing it as part of an array.
When setting up your message model, try this...
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler[0];
$message->save();
return Redirect::action('AdminController#foo_bar');
}
Usually, you'd only use names in your forms post-fixed with [] when you are accepting multiple values like checkboxes/multi-selects etc... Otherwise, it's probably best to stick with not using it because it may cause confusion.
I managed to fix it in a almost frustratingly simple way by just changing the method to PUT.
like this
Form::open(array('url' => 'foo/bar', 'method' => 'put'))

Print a variable that contains html and twig on a twig template

I have a variable suppose that is:
$menustr; this variable contains code html and some twig parts for example:
$menustr .= '<li><a href="{{ path("'. $actual['direccion'] .'") }}" >'. $actual['nombre'] .'</a></li>';
I need that the browser take the code html and the part of twig that in this momen is the
"{{ path(~~~~~) }}"
I make a return where i send the variable called "$menustr" and after use the expresion "raw" for the html code but this dont make effective the twig code.
This is te return:
return $this->render('::menu.html.twig', array('menu' => $menustr));
and here is the template content:
{{ menu | raw }}
Twig can't render strings containing twig. There is not something like an eval function in Twig1..
What you can do is moving the path logic to the PHP stuff. The router service can generate urls, just like the path twig function does. If you are in a controller which extends the base Controller, you can simply use generateUrl:
$menuString .= '<li>'. $actual['nombre'] .'</li>';
return $this->render('::menu.html.twig', array(
'menu' => $menuString,
));
Also, when using menu's in Symfony, I recommend to take a look at the KnpMenuBundle.
EDIT: 1. As pointed by #PepaMartinec there is a function which can do this and it is called template_from_string
You can render Twig template stored in a varible using the template_from_string function.
Check this bundle: https://github.com/LaKrue/TwigstringBundle
This Bundle adds the possibility to render strings instead of files with the Symfony2 native Twig templating engine:
$vars = array('var'=>'x');
// render example string
$vars['test'] = $this->get('twigstring')->render('v {{ var }} {% if var is defined %} y {% endif %} z', $vars);
// output
v x y z
In your case i would be:
return $this->render('::menu.html.twig', array(
'menu' => $this->get('twigstring')->render($menustr, $vars)
));

Categories