render('article/index.html.twig', [ 'articles' => $articleRepository->findBy(criteria: [], orderBy: ['publicationDate' => 'DESC']), ]); } #[Route('/new', name: 'app_article_new', methods: ['GET', 'POST'])] public function new( Request $request, EntityManagerInterface $entityManager, #[Target('blog_publishing')] WorkflowInterface $workflow ): Response { $article = new Article($this->getUser()); $article->setContent(''); $workflow->getMarking($article); $transitions = $workflow->getEnabledTransitions($article); $transitionsChoice = []; foreach($transitions as $transition) { $transitionsChoice[$transition->getName()] = $transition->getName(); } $form = $this->createForm(ArticleType::class, $article, [ 'transitions' => $transitionsChoice ]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $publicationChoice = $form->get('publicationChoice')->getData(); if ($publicationChoice !== null) { $workflow->apply($article, $publicationChoice); } $entityManager->persist($article); $entityManager->flush(); return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER); } return $this->render('article/new.html.twig', [ 'article' => $article, 'form' => $form, ]); } #[Route('/{id}', name: 'app_article_show', methods: ['GET'])] #[IsGranted('view', 'article')] public function show(Article $article): Response { return $this->render('article/show.html.twig', [ 'article' => $article, ]); } #[Route('/{id}/edit', name: 'app_article_edit', methods: ['GET', 'POST'])] #[IsGranted('edit', 'article')] public function edit( Request $request, Article $article, EntityManagerInterface $entityManager, #[Target('blog_publishing')] WorkflowInterface $workflow ): Response { $transitions = $workflow->getEnabledTransitions($article); $transitionsChoice = []; foreach($transitions as $transition) { $transitionsChoice[$transition->getName()] = $transition->getName(); } $form = $this->createForm(ArticleType::class, $article, [ 'transitions' => $transitionsChoice ]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $publicationChoice = $form->get('publicationChoice')->getData(); if ($publicationChoice !== null) { $workflow->apply($article, $publicationChoice); } $entityManager->flush(); return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER); } return $this->render('article/edit.html.twig', [ 'article' => $article, 'form' => $form, ]); } #[Route('/{id}', name: 'app_article_delete', methods: ['POST'])] #[IsGranted('edit', 'article')] public function delete(Request $request, Article $article, EntityManagerInterface $entityManager): Response { if ($this->isCsrfTokenValid('delete'.$article->getId(), $request->getPayload()->getString('_token'))) { $entityManager->remove($article); $entityManager->flush(); } return $this->redirectToRoute('app_article_index', [], Response::HTTP_SEE_OTHER); } }