/** * @Route("/blog/post/edit/{id}", name="drafterbit_blog_post_edit") * @Template() * @Security("is_granted('ROLE_POST_EDIT')") */ public function editAction($id) { $pageTitle = 'Edit Post'; $em = $this->getDoctrine()->getManager(); $post = $em->getRepository('DrafterbitBlogBundle:Post')->find($id); if (!$post and $id != 'new') { throw $this->createNotFoundException(); } if (!$post) { $post = new Post(); $pageTitle = 'New Post'; } // @todo use object voter // $this->denyAccessUnlessGranted('post.edit', $post); $tags = $em->getRepository('DrafterbitBlogBundle:Tag')->findAll(); $tagOptions = array_map(function ($item) { return $item->getLabel(); }, $tags); $postTags = array_map(function ($item) { return $item->getLabel(); }, $post->getTags()->toArray()); // @todo merge form creation with one defined in saveAction $form = $this->createForm(new PostType(), $post); $form->get('id')->setData($id); $form->get('published_at')->setData($post->getPublishedAt()->format('Y-m-d H:i')); $revisions = $this->getRevisions($id); $categories = $em->getRepository('DrafterbitBlogBundle:Category')->findAll(); return ['categories' => $categories, 'tag_options' => json_encode($tagOptions), 'tags' => json_encode($postTags), 'form' => $form->createView(), 'post_id' => $id, 'view_id' => 'post-edit', 'revisions' => $revisions, 'action' => $this->generateUrl('drafterbit_blog_post_save'), 'page_title' => $this->get('translator')->trans($pageTitle)]; }