I have a notification icon. When user clicks on it, a dropdown appears and shows a list. I want to filter list to only show what's related to the connected user.
{% if app.user.roles[0] == "ROLE_DEVELOPPER"%}
<!-- dropdown notification message -->
<li class="dropdown">
<a title="Notifications" href="#fakelink" class="dropdown-toggle" data-toggle="dropdown">
<strong> <i class="fa fa-bell"></i></strong>
</a>
{% if notif_conges|length > 0 %}
<ul class="dropdown-menu animated half flipInX">
//this section
{% for notif_c in notif_conges %}
{% if notif_c.etat == "Acceptée" %}
<li><a>Votre demande : "{{notif_c.motif}}" Envoyée le "{{notif_c.CreatedAt|date('Y-m-d H:i:s')}}" a était traitée </a></li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
</li>
{% endif %}
You want to filter notifications. Only notifications concerning connected users have to be displayed. You have two solutions:
The (very) bad way: Add a test in view
<ul class="dropdown-menu animated half flipInX">
//this section
{% for notif_c in notif_conges %}
{% if notif_c.etat == "Acceptée" and notif_c.user = app.user %}
<li><a>Votre demande : "{{notif_c.motif}}" Envoyée le "{{notif_c.CreatedAt|date('Y-m-d H:i:s')}}" a était traitée </a></li>
{% endif %}
{% endfor %}
</ul>
This is a bad way because you will forward all notifications to the view
The bad way: Add a filter in your controller
class YourController
{
public yourAction()
{
/** ... your code to handle notifications **/
foreach ($notifications as $notification) {
if ($notification->getUser()->getId() == $this->getUser()->getId)) {
$filteredNotification[] = $notification;
}
}
return $this->render('your_template_file', [
'notif_c' => $filteredNotifications
]);
}
}
This is still a bad way because you are retrieving all notifications from database.
The good way: Ask only the necessaries notification to your repository
Assuming, your notifications are retrieving with Doctrine, repository can filter data to only retrieve pertinent data.
class YourController
{
public yourAction()
{
/** ... your code is certainly something lke this: **/
$notificationRepository = $this->entityManager->getRepository('AppBundle:Notification');
$notifications = $notificationRepository->findAll();
render
return $this->render('your_template_file', [
'notif_c' => $notifications
]);
}
}
Replace findAll() by findBy(array $criteria) and add a criteria as first parameter:
class YourController
{
public yourAction()
{
$notificationRepository = $this->entityManager->getRepository('AppBundle:Notification');
$notifications = $notificationRepository->findBy([
'etat' => 'Acceptée',
'user' => $this->getUser(), //this could be a little different, I do not remember how to handle user in Sf2.8
]);
render
return $this->render('your_template_file', [
'notif_c' => $notifications
]);
}
}
Yo must use is_granted twig filter.
You can specify the user role that you one but IS_AUTHENTICATED_REMEMBERED role is given to all users who are logged in.
{% is_granted('IS_AUTHENTICATED_REMEMBERED') %}
<!-- dropdown notification message -->
...
{% endif %}
Related
I created a global Action :
public function export(
Admin $user,
StructureRepository $structureRepository,
ExportCsvEntity $exportCsvEntity,
string $entityClass
): HttpFoundationResponse {
$zipcodes = $structureRepository->getZipcodesByStructureFromAdmin($user->getId());
$exportCsvEntity->export(
$zipcodes,
$entityClass
);
return $exportCsvEntity->download($entityClass);
}
public function exportButton(): Action
{
return Action::new('export', 'admin.crud.user.field.activities_tracking.button.export')
->linkToCrudAction('export')
->displayAsLink()
->setCssClass('btn btn-primary')
->createAsGlobalAction()
;
}
Then in my Crud Controller I call it :
if ($this->getUser() instanceof Admin) {
$export = $this->exportAction->exportButton();
$actions->add(Crud::PAGE_INDEX, $export);
}
In the doc its written => Global actions are displayed above the listed entries.
But in my case the button is underneath the table
Have a look here
My template is extending '#!EasyAdmin/crud/index.html.twig'then I override the global_actions block :
{% block global_actions %}
{{ parent() }}
{% endblock global_actions %}
Now my button is above the table but also underneath :
Have a look here
What do I do wrong ?
You are correct when trying to do this by overriding the index template.
An easy way to do this considering how the template is organized is to modify the global_actions block by filtering actions you do not want to show above the list. For example by using a css class to not show a global action above the list.
{% block global_actions %}
<div class="global-actions">
{% for action in global_actions|filter(a => 'under-list' not in a.cssClass) %}
{{ include(action.templatePath, { action: action }, with_context = false) }}
{% endfor %}
</div>
{% endblock global_actions %}
And in your crud controller:
Action::new('customAction', $label, $icon)
->addCssClass('under-list')
->createAsGlobalAction()
->linkToCrudAction('customAction');
And you need to add your new list of actions under your list by overriding the main block.
{% block main %}
{{ parent() }}
{% block under_list_global_actions %}
<div class="under-list-global-actions">
{% for action in global_actions|filter(a => 'under-list' in a.cssClass) %}
{{ include(action.templatePath, { action: action }, with_context = false) }}
{% endfor %}
</div>
{% endblock under_list_global_actions %}
{% endblock main %}
And you should get your custom global action (with the css class under-list) under your list.
I have written a Twig custom function (in a Twig custom extension). I notice the template does not read the function and keeps throwing me the "Method Exists" error.
Was wondering if you have faced this before. Any ideas ?
The custom extension file; $post and $list are both objects:
<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class ShowContentExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction('ShowList', [$this, 'ShowList']),
new TwigFunction('ShowPost', [$this, 'ShowPost'])
];
}
public function ShowList($list) {
foreach ($list->post as $post) {
$this->ShowPost($post);
}
}
public function ShowPost($post) {
return !((count(array_keys($post->documents))) < 2 && array_key_exists('header', $post->documents));
}
}
and this is where they are being called:
{% if ShowList(list) %}
<h2 >{{ title }}</h2>
<div>
<div>
{% for post in list %}
{% if post|length > 0 %}
{% include ./_links.html.twig' with { 'list': list} %}
{% endif %}
{% endfor %}
</div>
</div>
{% endif %}
and :
{% if ShowPost(post) %}
<div>
<div>
<a href="{{ post.link }}">
<span>{{ post.title }}</span>
</a>
</div>
</div>
{% endif %}
and this is the screenshot of the error :
error screenshot
I am beginner in symfony 2. I have a project and I want to create a dynamic tree using css, this tree will display my categories levels.
I already tried to do it by myself, I was able to display the first 3 levels of my categories using 3 loops, but other levels are not displaying.
What I want is to know if I can use recursion or an other way to display all categories as a tree.
this the action method i used :
public function afficherArborescenceAction(){
$em = $this->getDoctrine()->getManager();
$categories = $em->getRepository('PortfolioBundle:Categorie')->findByCategorieParent();
$sousCategories = $em->getRepository('PortfolioBundle:Categorie')->findSousCategories();
return $this->render('AdministrateurBundle:NouvelleCategorie:showCategories.html.twig',array('categories'=>$categories,'sousCategories'=>$sousCategories));
}
This my html code :
<div style="border:1px #999999 solid;border-radius: 5px;width:350px;margin: 100px auto;padding:10px;">
<p>Les catégories:</p>
{% for categorie in categories %}
<ul class="tree">
<li>{{ categorie.libelle }}
{% for sousCategorie in sousCategories %}
<ul>
{% if sousCategorie.categorieParent == categorie.id %}
<li>
{{ sousCategorie.libelle }}
{% for sous in sousCategories %}
{% if sous.categorieParent == sousCategorie.id %}
<ul>
<li>
{{ sous.libelle }}
</li>
</ul>
{% endif %}
{% endfor %}
</li>
{% endif %}
</ul>
{% endfor %}
</li>
</ul>
{% endfor %}
This is the current tree i get, using the code above :
By using StofDoctrineExtensionBundle it is very simple to do what you want. For example this would generate nested <ul> of your category tree:
$htmlTree = $repository->childrenHierarchy(
null,
false,
[
'decorate' => true,
'nodeDecorator' => function ($node)
{
return "$node[name]";
},
//'rootOpen' => '<ul>', leave it as is
//'rootClose' => '</ul>',
'childOpen' => function ($node)
{
return "<li data-node-id=\"$node[id]\">";
},
'childClose' => '</li>',
],
true
);
$io->writeln($htmlTree);
Here is fully working example, complete process is covered, from configuring to the more advanced queries: Hierarchical Data in Relational Databases with Symfony 4 and Doctrine.
Check there, with the component Tree:
https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/Resources/doc/index.rst
It can do what you need exactly
I want to get the id attribute from an entity using "lazy loading".
This is my controller:
public function indexAction() {
$em = $this->getDoctrine()->getManager();
$topics = $em->getRepository('BackendBundle:Topic')->findAll();
$posts = $em->getRepository('BackendBundle:Post')->findAll();
return $this->render('BackendBundle:Home:home.html.twig',
['topics' => $topics, 'posts' => $posts]
);
}
And here is my template block:
{% block article %}
{% for post in posts %}
<h3>
<a href="{{ path('backend_posts_post',
{ 'topic_id': post.topic.id, 'post_id': post.id }) }}">
{{ post.title }}
</a>
</h3>
<br>
<p>{{ post.text }}</p>
<hr>
{% endfor %}
{% endblock %}
I'm trying to get the topic id. Every post depends on a topic and should be possible to get his identifier.
Solved. I was trying to access a post whitout topic.
My layout.html.twig:
{{ render(controller('AcmeDemoBundle:Page:mainmenu')) }}
The Page controller retrieves all pages from the Doctrine and renders mainmenu.html.twig with a set of pages.
My mainmenu.html.twig:
{% if menu_pages is defined %}
{% for page in menu_pages %}
<li class="{% if app.request.attributes.get('_internal') == '_page_show' and app.request.get('id') == page.id %}active{% endif %}">{{ page.title|e }}</li>
{% endfor %}
{% endif %}
But no active class is displayed. As far as I understand the problem is in internal routing. How to fix that?
Better do not use {{ render(controller('AcmeDemoBundle:Page:mainmenu')) }}. It work more fast and comfortable when you use services instead. You can create a service which will show menu on any page where you include them. And in service you can easy get access to current _route for add active class.
But if you really need to use {{ render(controller('AcmeDemoBundle:Page:mainmenu')) }}, so you need pass to them a current request like:
{{ render(controller('AcmeDemoBundle:Page:mainmenu', {'request': app.request})) }}
and then in action pass request to twig:
public function mainmenuAction($request) {
return $this->render('AcmeDemoBundle:Page:mainmenu.html.twig', array('request' => $request));
}
and in twig use this request:
{% if menu_pages is defined %}
{% for page in menu_pages %}
<li class="{% if request.get('_route') == '_page_show' and request.get('id') == page.id %}active{% endif %}">{{ page.title|e }}</li>
{% endfor %}
{% endif %}