How to display dateTime relative to the user location? - symfony

I am searching how to display the dateTime related to the user timezone using TWIG.
I really want to understand how it works.
Thank you.

There is no easy solution to get the user location in twig templates.
I would :
Try to get the user's timezone with Javascript
Store this value in a session
Then catch it with twig fron the {{ app.session }} object
Good luck ;)

Related

I can't verify the role in twig by using is_granted

When I use the function
{%if is_granted(CLINET_ROLE) %}
By the way whene I check the base I find in colone role this
a:1:{i:0;s:11:"CLIENT_ROLE";}
You have a typo, CLINET_ROLE instead of CLIENT_ROLE
There is typo error. Correct spelling of client in twig file.

Bolt modifying an record from the web page. Using Twig

Im trying to modify records from my wep page using bolt, for lets say an comment section. If someone posts an answer underneath an news item how do i put that info into an record. And if they want to edit their reaction how would I update it.
So i was trying something at this point with the status. My base status is always published.
{{ record.status == draft }} <-- tried this way
{% set record.status == record.status = draft %} <-- tried SET
{% set record.status == record.status = "draft" %} <-- tried adding ""
After my try and error period I went to all the documentation on bolt and twig. But I cant seem to find out how to do this.
So I hope someone knows how to do this, thank you in advance.
short anwser is - you can't modify database records directly from twig.
Twig it's template system (idea is to show data with it) .
To dwhat you want you need to write all this "stuff" by yourself . It can be done (best way is to learn about creating custom modules - but you need to be quite good with OOPHP , symfony components etc. )

Get cookie from Twig template

I'm trying to access cookies I set in my Drupal website.
I created two cookies on a form submission :
with the Drupal funtion = user_cookie_save(['myfirstcookie' => 'myfirstdata'])
with the classic PHP function = setcookie('mysecondcookie', 'myseconddata', time() + (86400 * 30), "/")
My cookies are set, no problem.
But, I didn't find how to get them (or one of them) from my Twig templates. The app.request.cookies of Symfony seems to not exist.
Do you have any idea ?
Twig has the global app helper context, via which you can access the cookies (among other things). Try this:
{{ dump(app.request.cookies) }}
And ultimately:
{{ app.request.cookies.get('MY_COOKIE_NAME') }}
Remember, cookies is an instance of ParameterBag (API), so you have to access it via get() call.
Hope this helps...
Past Cookie variable from the controller and check it. It works to me.
My code in controller is:
$data['cookie'] = $_COOKIE['postcode'];
postcode is my cookie name and in my twig templete, i write this line of code
{{ cookie }}
and show my cookie value. Thanks.

How to get country language in Symfony

I'm customizing my error pages and I was wandering if it is possible to get the language of the country user directly in my template, without the controller.
You can use :
{{ app.request.locale }}
Edit
Ah i didn't understood the question but this is what you are looking for :
$request->getPreferredLanguage($anArrayOfYourSupportedLanguages);
Then pass it to your twig.
fabpot answer :
https://groups.google.com/forum/#!topic/symfony-devs/-2AwAQhlLLQ

Check if user agreed to terms , set cookie

I'm a Drupal nub. I would like to check on every page if user (anonymouse) agreed to somekind of terms. I suppose i should write small custom module ?
Where will this condition be written
if(!$_COOKIE('confirm')){
//jQuery show confirmation form
//Set cookie for 1hour
}
maybee in page.tpl.php ? Please, give me some tips ..
If you don't want to store the info for a long time, you should use $_SESSION variable. Then in preprocess page you could check if the user has accepted and set a variable that you can use in your page.tpl.php.
user_save() accepts an array argument which you can put custom data into. This will then be loaded with your $user object and you can use in any template file.
Check out these modules:
http://drupal.org/project/legal
http://drupal.org/project/terms_of_use
The Legal and TOS modules are good if you need a login. If working with anonymous users, however, you'll need to use the rules module with https://www.drupal.org/project/rules_session_vars.

Resources