Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to set the thread title #51

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ Tests/Application/.env.test.local
# php-cs-fixer
.php-cs-fixer.cache
php-cs-fixer

.php-version
13 changes: 7 additions & 6 deletions Controller/WebsiteCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function cgetCommentsAction(string $threadId, Request $request): Response
$limit = $request->query->getInt('limit', 10);
$offset = $request->query->getInt('offset', 0);

/** @var null|int $pageSize */
/** @var int|null $pageSize */
$pageSize = $request->get('pageSize');
if ($pageSize) {
@trigger_deprecation('sulu/comment-bundle', '2.x', 'The usage of the "pageSize" parameter is deprecated.
Expand Down Expand Up @@ -170,6 +170,7 @@ public function cgetCommentsAction(string $threadId, Request $request): Response
'data_class' => $this->commentClass,
'threadId' => $threadId,
'referrer' => $referrer,
'threadTitle' => $request->query->get('threadTitle'),
]
);

Expand Down Expand Up @@ -245,7 +246,7 @@ public function postCommentsAction(string $threadId, Request $request): Response
/** @var CommentInterface $comment */
$comment = $this->commentRepository->createNew();

/** @var null|int $parent */
/** @var int|null $parent */
$parent = $request->get('parent');
if ($parent) {
$comment->setParent($this->commentRepository->findCommentById($parent));
Expand All @@ -270,7 +271,7 @@ public function postCommentsAction(string $threadId, Request $request): Response
$comment = $form->getData();

/** @var string $threadTitle */
$threadTitle = $request->get('threadTitle');
$threadTitle = $request->request->get('threadTitle', '');
$this->commentManager->addComment($type, $entityId, $comment, $threadTitle);
$this->entityManager->flush();

Expand Down Expand Up @@ -330,7 +331,7 @@ public function putCommentAction(string $threadId, string $commentId, Request $r
public function deleteCommentAction(string $threadId, string $commentId, Request $request): Response
{
/** @var Comment $comment */
$comment = $this->commentRepository->findCommentById(\intval($commentId));
$comment = $this->commentRepository->findCommentById((int) $commentId);

$this->entityManager->remove($comment);
$this->entityManager->flush();
Expand All @@ -347,8 +348,8 @@ public function deleteCommentAction(string $threadId, string $commentId, Request
}

/**
* @param null|mixed $data
* @param null|string $statusCode
* @param mixed|null $data
* @param string|null $statusCode
* @param mixed[] $headers
*
* @return View
Expand Down
3 changes: 3 additions & 0 deletions Entity/CommentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Doctrine\ORM\NoResultException;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;

/**
* @extends NestedTreeRepository<CommentInterface>
*/
class CommentRepository extends NestedTreeRepository implements CommentRepositoryInterface
{
public function findComments(string $type, string $entityId, int $limit = 10, int $offset = 0): array
Expand Down
3 changes: 2 additions & 1 deletion Form/Type/CommentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

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

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('threadId');
$resolver->setDefault('threadTitle', '');
$resolver->setDefault('referrer', null);
$resolver->setDefault('parent', null);
$resolver->setDefault('csrf_protection', false);
Expand Down
3 changes: 2 additions & 1 deletion Resources/doc/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Following example works for a page:
```twig
{{ render(path('sulu_comment.get_threads_comments', {
threadId: 'page-' ~ uuid,
referrer: app.request.uri,
referrer: app.request.uri,
threadTitle: 'This is my thread title',
_format: 'html'
})) }}
```
Expand Down
4 changes: 2 additions & 2 deletions Tests/Application/config/templates/pages/overview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

<key>overview</key>

<view>::overview</view>
<controller>SuluWebsiteBundle:Default:index</controller>
<view>overview</view>
<controller>Sulu\Bundle\WebsiteBundle\Controller\DefaultController::indexAction</controller>
<cacheLifetime>2400</cacheLifetime>

<properties>
Expand Down
13 changes: 13 additions & 0 deletions Tests/Application/templates/overview.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'master.html.twig' %}


{% block content %}

{{ render(path('sulu_comment.get_threads_comments', {
threadId: 'page-' ~ uuid,
referrer: app.request.uri,
threadTitle: 'This is my title',
_format: 'html'
})) }}

{% endblock %}
15 changes: 15 additions & 0 deletions Tests/Functional/Controller/WebsiteCommentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected function setUp(): void
{
$this->client = $this->createAuthenticatedWebsiteClient();
$this->purgeDatabase();
$this->initPhpcr();
}

public function providePostData()
Expand Down Expand Up @@ -292,6 +293,20 @@ public function testGetComments($type = 'blog', $entityId = '1')
$this->assertEquals('Sulu is awesome', $response[1]['message']);
}

public function testGetCommentsHtmlThreadTitle()
{
$crawler = $this->client->request(
'GET',
'/'
);

$input = $crawler->filter('input[type="hidden"]');

$this->assertSame('threadTitle', $input->attr('id'));
$this->assertSame('hidden', $input->attr('type'));
$this->assertSame('This is my title', $input->attr('value'));
}

private function postComment(
$type = 'blog',
$entityId = '1',
Expand Down
9 changes: 7 additions & 2 deletions Twig/CommentFormFactoryTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ public function getFunctions()
];
}

public function createCommentForm(string $threadId, ?string $referrer = null, ?int $parent = null): FormView
{
public function createCommentForm(
string $threadId,
?string $referrer = null,
?int $parent = null,
?string $threadTitle = null
): FormView {
$form = $this->formFactory->create(
CommentType::class,
null,
Expand All @@ -52,6 +56,7 @@ public function createCommentForm(string $threadId, ?string $referrer = null, ?i
'threadId' => $threadId,
'referrer' => $referrer,
'parent' => $parent,
'threadTitle' => $threadTitle,
]
);

Expand Down
Loading