Skip to content

Commit

Permalink
use symfony form to render the comment form
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Aug 1, 2019
1 parent e9a00b0 commit 11ec5a0
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 31 deletions.
52 changes: 37 additions & 15 deletions Controller/WebsiteCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use FOS\RestBundle\Controller\Annotations\NamePrefix;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Routing\ClassResourceInterface;
use Sulu\Bundle\CommentBundle\Entity\CommentInterface;
use Sulu\Bundle\CommentBundle\Entity\CommentRepositoryInterface;
use Sulu\Bundle\CommentBundle\Form\Type\CommentType;
use Sulu\Bundle\CommentBundle\Manager\CommentManagerInterface;
use Sulu\Component\Rest\RestController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -59,9 +63,20 @@ public function cgetCommentsAction($threadId, Request $request)
$response->setMaxAge(0);
$response->setSharedMaxAge(0);

$form = $this->createForm(
CommentType::class,
null,
[
'data_class' => $this->getParameter('sulu.model.comment.class'),
'threadId' => $threadId,
'referrer' => $referrer,
]
);

return $this->render(
$this->getTemplate($type, 'comments'),
[
'form' => $form->createView(),
'template' => $this->getTemplate($type, 'comment'),
'formTemplate' => $this->getTemplate($type, 'form'),
'comments' => $comments,
Expand All @@ -83,25 +98,32 @@ public function cgetCommentsAction($threadId, Request $request)
*/
public function postCommentsAction($threadId, Request $request)
{
$data = $request->request->all();
if (array_key_exists('created', $data)
|| array_key_exists('creator', $data)
|| array_key_exists('changed', $data)
|| array_key_exists('changer', $data)
) {
return new Response(null, 400);
}

list($type, $entityId) = $this->getThreadIdParts($threadId);

// deserialize comment
$serializer = $this->get('serializer');
$comment = $serializer->deserialize(
json_encode($data),
$this->getParameter('sulu.model.comment.class'),
'json'
/** @var CommentRepositoryInterface $repository */
$repository = $this->get('sulu.repository.comment');

/** @var CommentInterface $comment */
$comment = $repository->createNew();

$form = $this->createForm(
CommentType::class,
$comment,
[
'data_class' => $this->getParameter('sulu.model.comment.class'),
'threadId' => $threadId,
]
);

$form->handleRequest($request);

if (!$form->isSubmitted() || !$form->isValid()) {
return new Response(null, 400);
}

$comment = $form->getData();

/** @var CommentManagerInterface $commentManager */
$commentManager = $this->get('sulu_comment.manager');
$commentManager->addComment($type, $entityId, $comment, $request->get('threadTitle'));

Expand Down
58 changes: 58 additions & 0 deletions Form/Type/CommentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommentBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

class CommentType extends AbstractType
{
/**
* @var RouterInterface
*/
private $router;

public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$attributes = ['threadId' => $options['threadId']];
if ($options['referrer']) {
$attributes['referrer'] = $options['referrer'];
}

$builder->setAction($this->router->generate('sulu_comment.post_thread_comments', $attributes));
$builder->add('message', TextType::class);
$builder->add('threadTitle', TextType::class, ['mapped' => false]);
$builder->add('submit', SubmitType::class);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('threadId');
$resolver->setDefault('referrer', null);
$resolver->setDefault('csrf_protection', false);
}

public function getBlockPrefix()
{
return '';
}
}
6 changes: 6 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@
<argument type="service" id="sulu.repository.comment"/>
<argument type="service" id="event_dispatcher"/>
</service>

<service id="Sulu\Bundle\CommentBundle\Form\Type\CommentType">
<argument type="service" id="router"/>

<tag name="form.type"/>
</service>
</services>
</container>
7 changes: 4 additions & 3 deletions Resources/doc/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ sulu_comment:
default_templates:
comments: 'SuluCommentBundle:WebsiteComment:comments.html.twig'
comment: 'SuluCommentBundle:WebsiteComment:comment.html.twig'
form: 'SuluCommentBundle:WebsiteComment:form.html.twig'
```
If you only want to customize the templates for the example `page` type:
Expand All @@ -35,7 +34,9 @@ sulu_comment:
types:
page:
templates:
comment: 'SuluCommentBundle:WebsiteComment:comment.html.twig'
comments: 'SuluCommentBundle:WebsiteComment:comments.html.twig'
form: 'SuluCommentBundle:WebsiteComment:form.html.twig'
comment: 'SuluCommentBundle:WebsiteComment:comment.html.twig'
```

To extend the form you can provide a [FormExtension](https://symfony.com/doc/current/form/create_form_type_extension.html)
for `Sulu\Bundle\CommentBundle\Form\Type\CommentType`.
2 changes: 0 additions & 2 deletions Resources/doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ sulu_comment:
default_templates:
comments: 'SuluCommentBundle:WebsiteComment:comments.html.twig'
comment: 'SuluCommentBundle:WebsiteComment:comment.html.twig'
form: 'SuluCommentBundle:WebsiteComment:form.html.twig'
types:
<type>:
templates:
comments: 'SuluCommentBundle:WebsiteComment:comments.html.twig'
comment: 'SuluCommentBundle:WebsiteComment:comment.html.twig'
form: 'SuluCommentBundle:WebsiteComment:form.html.twig'
objects:
comment:
model: Sulu\Bundle\CommentBundle\Entity\Comment
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/WebsiteComment/comments.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
{% include template with {threadId: threadId, comment: comment} %}
{% endfor %}

{% include formTemplate with {threadId: threadId, referrer: referrer} %}
{{ form(form) }}
10 changes: 0 additions & 10 deletions Resources/views/WebsiteComment/form.html.twig

This file was deleted.

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"sulu/document-manager": "@dev",
"symfony/browser-kit": "^3.4 || ^4.0",
"symfony/dotenv": "^3.4 || ^4.0",
"symfony/form": "^3.4 || ^4.0",
"symfony/monolog-bundle": "^3.1",
"symfony/security-bundle": "^3.4 || ^4.0",
"symfony/stopwatch": "^3.4 || ^4.0",
Expand Down

0 comments on commit 11ec5a0

Please sign in to comment.