|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\NewsBundle\Action; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\NewsBundle\Form\Type\CommentType; |
|
15
|
|
|
use Sonata\NewsBundle\Model\CommentManagerInterface; |
|
16
|
|
|
use Sonata\NewsBundle\Model\PostInterface; |
|
17
|
|
|
use Sonata\NewsBundle\Model\PostManagerInterface; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
19
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
20
|
|
|
use Symfony\Component\Form\FormInterface; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
22
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
23
|
|
|
|
|
24
|
|
|
final class CreateCommentFormAction extends Controller |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var RouterInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $router; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var PostManagerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $postManager; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var CommentManagerInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $commentManager; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var FormFactoryInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $formFactory; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct( |
|
47
|
|
|
RouterInterface $router, |
|
48
|
|
|
PostManagerInterface $postManager, |
|
49
|
|
|
CommentManagerInterface $commentManager, |
|
50
|
|
|
FormFactoryInterface $formFactory |
|
51
|
|
|
) { |
|
52
|
|
|
$this->router = $router; |
|
53
|
|
|
$this->postManager = $postManager; |
|
54
|
|
|
$this->commentManager = $commentManager; |
|
55
|
|
|
$this->formFactory = $formFactory; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $postId |
|
60
|
|
|
* @param bool $form |
|
61
|
|
|
* |
|
62
|
|
|
* @return Response |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __invoke($postId, $form = false) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$form) { |
|
67
|
|
|
$post = $this->postManager->findOneBy([ |
|
68
|
|
|
'id' => $postId, |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
$form = $this->getCommentForm($post); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->render('@SonataNews/Post/comment_form.html.twig', [ |
|
75
|
|
|
'form' => $form->createView(), |
|
|
|
|
|
|
76
|
|
|
'post_id' => $postId, |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return FormInterface |
|
82
|
|
|
*/ |
|
83
|
|
|
private function getCommentForm(PostInterface $post) |
|
84
|
|
|
{ |
|
85
|
|
|
$comment = $this->commentManager->create(); |
|
86
|
|
|
$comment->setPost($post); |
|
87
|
|
|
$comment->setStatus($post->getCommentsDefaultStatus()); |
|
88
|
|
|
|
|
89
|
|
|
return $this->formFactory->createNamed('comment', CommentType::class, $comment, [ |
|
90
|
|
|
'action' => $this->router->generate('sonata_news_add_comment', [ |
|
91
|
|
|
'id' => $post->getId(), |
|
92
|
|
|
]), |
|
93
|
|
|
'method' => 'POST', |
|
94
|
|
|
]); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: