Created
April 17, 2013 18:58
-
-
Save oste/5406806 to your computer and use it in GitHub Desktop.
Steps to modify FOSCommentBundle form and modify it based on thread id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CommentBundle\Event; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use CommentBundle\Event\CommentFormEvent; | |
class CommentFormSubscriber implements EventSubscriberInterface | |
{ | |
public function form(CommentFormEvent $event) | |
{ | |
if($event->getId() == 'test') | |
$event->getForm()->remove('rating'); | |
} | |
public static function getSubscribedEvents() | |
{ | |
return array( | |
'fos_comment.comment.form' => array('form') | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommentType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('body', 'textarea'); | |
$builder->add('rating', 'choice', array( | |
'choices' => array( | |
'5' => '5', | |
'4' => '4', | |
'3' => '3', | |
'2' => '2', | |
'1' => '1', | |
), | |
'multiple' => false, | |
'expanded' => true | |
)); | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
parent::setDefaultOptions($resolver); | |
$resolver->setDefaults(array( | |
'data_class' => 'App\Entity\Comment', | |
)); | |
} | |
public function getName() | |
{ | |
return "fos_comment_comment"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fos_comment_thread_api: | |
type: rest | |
resource: CommentBundle\Controller\ThreadController | |
name_prefix: fos_comment_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
fos_comment.form_type.comment.default: | |
class: App\Form\CommentType | |
tags: [{ name: form.type, alias: fos_comment_comment }] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CommentBundle\Controller; | |
use FOS\CommentBundle\Model\ThreadInterface; | |
use FOS\RestBundle\View\View; | |
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
use CommentBundle\Event\CommentFormEvent; | |
use CommentBundle\Event\CommentFormSubscriber; | |
use FOS\CommentBundle\Controller\ThreadController as BaseController; | |
class ThreadController extends BaseController | |
{ | |
/** | |
* Presents the form to use to create a new Comment for a Thread. | |
* | |
* @param string $id | |
* | |
* @return View | |
*/ | |
public function newThreadCommentsAction($id) | |
{ | |
$thread = $this->container->get('fos_comment.manager.thread')->findThreadById($id); | |
if (!$thread) { | |
throw new NotFoundHttpException(sprintf('Thread with identifier of "%s" does not exist', $id)); | |
} | |
$comment = $this->container->get('fos_comment.manager.comment')->createComment($thread); | |
$parent = $this->getValidCommentParent($thread, $this->getRequest()->query->get('parentId')); | |
$form = $this->container->get('fos_comment.form_factory.comment')->createForm(); | |
$dispatcher = $this->container->get('event_dispatcher'); | |
$dispatcher->addSubscriber(new CommentFormSubscriber()); | |
$dispatcher->dispatch('fos_comment.comment.form', new CommentFormEvent($form, $id)); | |
$form->setData($comment); | |
$view = View::create() | |
->setData(array( | |
'form' => $form->createView(), | |
'first' => 0 === $thread->getNumComments(), | |
'thread' => $thread, | |
'parent' => $parent, | |
'id' => $id, | |
)) | |
->setTemplate(new TemplateReference('FOSCommentBundle', 'Thread', 'comment_new')); | |
return $this->getViewHandler()->handle($view); | |
} | |
/** | |
* Checks if a comment belongs to a thread. Returns the comment if it does. | |
* | |
* @param ThreadInterface $thread Thread object | |
* @param mixed $commentId Id of the comment. | |
* | |
* @return CommentInterface|null The comment. | |
*/ | |
protected function getValidCommentParent(ThreadInterface $thread, $commentId) | |
{ | |
{ | |
return parent::getValidCommentParent($thread, $commentId); | |
} | |
/** | |
* @return \FOS\RestBundle\View\ViewHandler | |
*/ | |
protected function getViewHandler() | |
{ | |
return $this->container->get('fos_rest.view_handler'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment