javascript function after successful login - fbconnect

I'm using django-allauth, so when i call "/accounts/facebook/login" from my homepage, the FB dialog box opens up, and the redirect URL is the homepage only.
Now, I want to run a Javascript function when the user is redirected to the homepage after successful login. The sad part is I have no idea how to do do this. The function is to open a popup, but thats a different thing.

if user is loged in, you can send that condition to your template with context like so..
context = {
'hede': 'hodo', etc.....
'user_loged_in': True
}
than, at your template
{% if user_loged_in %}
<script>alert('stuff');</script>
{% endif %}

Related

Drupal 8 Twig User ID?

I'm new to Drupal & Twig and all I need is in my custom theme a twig expression to output the current user's ID. I can't find anything in the template comments, only if a user is logged in true / false.
Is there a simple way to get the ID of the current user? I'm not sure how to implement custom methods in a theme.
thanks!
Hello bobomoreno,
I would suggest you use the module Bamboo Twig.
The Bamboo Twig module provides some Twig extensions with some useful functions and filters aimed to improve the development experience.
You could then enable the sub-module Bamboo Twig - Loaders:
drush en bamboo_twig_loader -y
Finally, you will be able to use the Twig function bamboo_load_currentuser:
<!-- Get Current User -->
{% set user = bamboo_load_currentuser() %}
<div>{{ user.name.value }}</div>
<div>{{ user.uid.value }}</div>
You can find the complete official documentation there.
In your theme find file yourthemename.theme and add following code:
function yourthemename_preprocess(&$vars, $hook)
{
$vars['uid'] = \Drupal::currentUser()->id();
}
now if you edit your twig template for html, page, region, block, field, form element... you can use 'uid' token in your twig. It works for all hooks
If you only need the ID in user.html.twig, it's {{ user.id }}
Here's how D8 now works, in two lines of executable code:
<?php
// This code returns the current user ID.
$account = \Drupal::currentUser();
return $account->id();
The display name is not a field you can configure in {{ content }}. You can get it directly from the user entity:
{{ user.displayname }}
Reference for the php method: AccountInterface::getDisplayName
The Twig Tweak module is very small and yet very powerful. You can get the current user id with drupal_token('current-user:uid') I am using it to pass the current user id to a view like this:
{{ drupal_view('view_name', 'embed_1', drupal_token('current-user:uid')) }}

Grav CMS: how to show/hide parts of the page depending on conditions?

The Grav's documentation clearly describes how a whole page or a folder could be hidden from unregistered users. It also describes how a whole page could be seen only by particular user groups.
But what about pieces of a page, let's say, some links or a private info I want to show on some conditions?
Ok, for registered users I found a snippet at Login plugin docs:
{% if grav.user.authenticated %}
content for registered users goes here
{% endif %}
But going wider - how can I show/hide pieces of a particular page depending on some custom logic in PHP code, i.e. not necessarily user related?
I'm thinking about a twig/shortcode plugin, something like:
{% if some.custom.condition.or.PHP.function %}
hidden content goes here
{% endif %}
or
[hidden_if_something] hidden content goes here [/hidden_if_something]
But not sure how exactly this should be implemented. So working examples would be appreciated. Thanks.
There is a recipe in the Grav documentation here. This provides an example of how to render the output of a PHP code result in a twig template.
In the example they create a plugin, and implement a twig extension providing access to a php function. They can then simply call that php function like in a twig template.
{{ example() }}
Following that example, you can implement whatever logic you would like in php, and call the function in a twig if statement.
{% if example() == true %}
your conditional output
{% endif %

Redirection from localhost:3000 to localhost:3000/home

I have issues to understand the Apostrophe CMS. I follow the tutorial in [Creating your first project][1] and stop just before the point where things become interesting.
I can open my basic/default website with localhost:3000 and see the content of the home.html file, while localhost:3000/home throws an error. Here is my home.html file:
my-project\lib\modules\apostrophe-pages\views\pages\home.html
{#
This is an example home page template. It inherits and extends a layout template
that lives in lib/modules/apostrophe-templates/views/outerLayout.html
#}
{% extends data.outerLayout %}
{% block title %}{{ super() }} | Home{% endblock %}
{% block main %}
<div class="main-content">
<h3>Hello world!
{% if not data.user %}
<a class="login-link" href="/login">Login</a>
{% endif %}
</h3>
<p>This is a very barebones Apostrophe project.
Now, get to work and make a real website!</p>
</div>
{% endblock %}
Where is the setting that apostrophe knows that home.html is the first (start) page which needs to be rendered?
And vice versa, I have seen a page when I call localhost:3000, apostrophe redirects directly to localhost:3000/home. Where is the setting that I tell apostrophe move automatically from localhost:3000 to localhost:3000/home?
I'm the lead developer of Apostrophe at P'unk Avenue.
Generally speaking, on a web site the "home page" is so named because it appears at "/" (at the root), not because it actually has the URL "/home". In Apostrophe the home page is always at "/" (*). Generally speaking this looks more professional than giving the home page a longer URL.
But if you really need "/home" to work for some reason, for instance to support legacy URLs and keep links from breaking, you can use the apostrophe-redirects module to add a redirect from /home and any other legacy URLs.
You could also make a subpage of the home page and set its slug to /home, but that would be a strange solution because it wouldn't be... well, it wouldn't be the home page. (The slug of the home page itself cannot be edited in Apostrophe.)
A ticket could be opened to add the ability for the home page to not have the slug "/", and for "/" to redirect to whatever the slug of the home page has been set to, but a good case would have to be made for why the feature is necessary.
(*) The only exception would be when using the apostrophe-workflow module, in which case you might have separate home pages for different languages, but you still wouldn't have "/home" in this scenario.

Symfony2: How to hide link in Twig based on permissions

My application shows a list of projects, project detail pages and forms to edit these projects. These are the routes:
/ - list of projects
/project/42 - view project (project detail page)
/project/42/edit - edit project
Only its owner may edit a project.
I have implemented a Voter to prevent access to /project/42/edit for non-owners.
Now, I also want to hide the link "edit project" from the project detail page. What would be the way to do this? Ideally, in Twig, I would like to do something like
{% if may_access(path('project_edit', { 'id': project.id })) %}
edit project
{% endif %}
I can implement this function as a Twig extension, but maybe a similar functionality already exists.
The function is_granted() actually has a second parameter that allows me to do just what I need:
{% if is_granted("MAY_EDIT", project) %}
edit project
{% endif %}
I use this in combination with a check in the controller action:
public function editAction(Project $project)
{
if (!$this->get('security.context')->isGranted('MAY_EDIT', $project)) {
$this->flash('You are not allowed to edit this project');
return $this->show($project);
}
// ...
}
This is actually very similar to the approach that nifr used in his answer to Sonata User - Security on custom field. I was hoping to find a way to have the voter be called automatically and avoid the call to isGranted().
If you want to have a look at the complete code, it is in the tutorial project I have published in github.

How do I display a block in Twig if there is data passed to it?

I have a page in a project I'm working on where I'm having a bit of a problem with.
The page displays two sets of results. The first set of results displays the information for the a project that has just been created. The second set of results displays all the reviews and revisions for this project. This is where I'm having the problem.
When the project is created, there are no reviews created. However, because I have set the page to display reviews related to the selected project, it is expecting to retrieve reviews for the selected project. As there are no reviews to retrieve (because they haven't been added yet) the page fails and gives an ambiguous 500 error.
Now, I have seen somewhere that Twig allows if statements.But, I can't seem to work out how to make this work in my project.
Does anyone know how you would use an if statement in Twig in conjunction with a MySQL result?
I've "Twigged" it (I'm here all week)
{% if reviews == null %}
Stuff to display if the result is null
{% else %}
Stuff to display if the result isn't null
{% endif %}
The loop is then inserted inside this if statement.

Resources