Home > Mobile >  How do I sort events lists depending on the status of the user who created it?
How do I sort events lists depending on the status of the user who created it?

Time:10-07

I am trying to display a list of events. So far I haven't had any problems. But I would be interested in dividing the list into two parts : events that are created by organizers and event that are created by members. I wrote my conditions saying that : "if some event exist" and "if the organizer who created the event is an organizer (or regular member)" (organizer and member depend on the status of the organizer). From the moment I wrote the second condition, I had this error :

Unexpected token "name" of value "events" ("end of statement block" expected). 

I noticed that when I added dd($events) in my controller file, organizer.id was informed but not his status which might be the problem.

events.html.twig file :

{% extends 'base.html.twig' %}

{% block title %}Liste des activités{% endblock %}

{% block main %}

<div >

    <div ></div>

    <div >

        <div >

            <h2 >
                <img src="{{ asset('img/titres/zpeak-sorties.svg') }}" alt="Les Zpeak Sorties !">
            </h2>

            <ul >

                {% if events and if events.organizer.status is 'organizer' %}
                    
                    {% for event in events %}

                    <a >
                        <img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" > {{ event.title }}
                    </a>

                    {% endfor %}

                {% else %}

                    <p>Il n'y a pas de zpeak sortie organisée.</p>

                {% endif %}

            </ul>

        </div>

        <div >

            <h2 >
                <img src="{{ asset('img/titres/zpeak-idees.svg') }}" alt="Les Zpeak Idées !">
            </h2>

            <ul >

            {% if events and if events.organizer.status is 'member' %}
                    
                {% for event in events %}
                    
                <a >
                    <img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" > {{ event.title }}
                </a>

                {% endfor %}

            {% else %}

                <p>Il n'y a pas de zpeak idée proposée.</p>

            {% endif %}

            </ul>
            
        </div>
    
    </div>

</div>

{% endblock %}

EventsController.php file

<?php

namespace App\Controller\Front;

use App\Form\SearchType;
use App\Repository\EventsRepository;
use App\Repository\CategoriesRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class EventsController extends AbstractController
{  
    #[Route('/events', name: 'events')]
    public function events(
        EventsRepository $eventsRepository, 
        CategoriesRepository $categoriesRepository
    ){
        $events = $eventsRepository->findAll();
        /* dd($events); */
        $categories = $categoriesRepository->findAll();
        return $this->render("front/events.html.twig", ['events' => $events, 'categories' => $categories]);
    }
}

CodePudding user response:

Bonjour Emilie,

I suggest to remove the second if of each conditions and use == or is same as() rather than is only.

{% if events and events.organizer.status == 'organizer' %}
  • Related