I use this bundle https://github.com/FriendsOfSymfony/FOSCommentBundle/blob/master/Resources/doc/index.md
but need first comments display in html - not assync. its almost done i create controller extends ThreadController
add function to get limited posts
public function getThreadCommentsWithLimitAction(Request $request, $id, $limit=false)
{
$displayDepth = $request->query->get('displayDepth');
$sorter = $request->query->get('sorter');
$thread = $this->container->get('fos_comment.manager.thread')->findThreadById($id);
// We're now sure it is no duplicate id, so create the thread
if (null === $thread) {
// Decode the permalink for cleaner storage (it is encoded on the client side)
$permalink = urldecode($request->query->get('permalink'));
$thread = $this->container->get('fos_comment.manager.thread')
->createThread();
$thread->setId($id);
$thread->setPermalink($permalink);
// Validate the entity
$validator = $this->get('validator');
$errors = $validator->validate($thread, 'NewThread');
if (count($errors) > 0) {
$view = View::create()
->setStatusCode(Codes::HTTP_BAD_REQUEST)
->setData(array('errors' => $errors))
->setTemplate(new TemplateReference('FOSCommentBundle', 'Thread', 'errors'));
return $this->getViewHandler()->handle($view);
}
// Add the thread
$this->container->get('fos_comment.manager.thread')->saveThread($thread);
}
$viewMode = $request->query->get('view', 'tree');
switch ($viewMode) {
case self::VIEW_FLAT:
$comments = $this->container->get('fos_comment.manager.comment')->findCommentsByThread($thread, $displayDepth, $sorter);
// We need nodes for the api to return a consistent response, not an array of comments
$comments = array_map(function($comment) {
return array('comment' => $comment, 'children' => array());
},
$comments
);
break;
case self::VIEW_TREE:
default:
$comments = $this->container->get('fos_comment.manager.comment')->findCommentTreeByThread($thread, $sorter, $displayDepth);
break;
}
$view = View::create()
->setData(array(
'comments' => $comments,
'displayDepth' => $displayDepth,
'limit' => $limit,
'sorter' => 'date',
'thread' => $thread,
'view' => $viewMode,
))
->setTemplate(new TemplateReference('FOSCommentBundle', 'Thread', 'comments'));
// Register a special handler for RSS. Only available on this route.
if ('rss' === $request->getRequestFormat()) {
$templatingHandler = function($handler, $view, $request) {
$view->setTemplate(new TemplateReference('FOSCommentBundle', 'Thread', 'thread_xml_feed'));
return new Response($handler->renderTemplate($view, 'rss'), Codes::HTTP_OK, $view->getHeaders());
};
$this->get('fos_rest.view_handler')->registerHandler('rss', $templatingHandler);
}
return $this->getViewHandler()->handle($view);
}
override template comments.html.twig
{% set depth = depth|default(0) %}
{% set view = view|default('tree') %}
{% if depth == 0 %}
{% if fos_comment_can_comment_thread(thread) %}
{% render url("fos_comment_new_thread_comments", {"id": thread.id}) %}
{% endif %}
{% if fos_comment_can_edit_thread(thread) %}
<div class="fos_comment_thread_commentable">
<button data-url="{{ url('fos_comment_edit_thread_commentable', {'id': thread.id, 'value': not thread.commentable}) }}" class="fos_comment_thread_commentable_action">
{{ (thread.commentable ? 'fos_comment_thread_close' : 'fos_comment_thread_open') | trans({}, 'FOSCommentBundle') }}
</button>
</div>
{% endif %}
{% set count = thread.numComments %}
<h3>{% transchoice count with {'%count%': count} from "FOSCommentBundle" %}fos_comment_thread_comment_count{% endtranschoice %}</h3>
{% endif %}
{% if limit is defined and limit>0%}
{% for commentinfo in comments|slice(0, limit) %}
{% include "FOSCommentBundle:Thread:comment.html.twig" with { "children": commentinfo.children, "comment": commentinfo.comment, "depth": depth, "view": view } %}
{% endfor %}
{% if comments|length > limit %}
<p><a href="{{ path('place_one_commentsAll',{
threadId: comments.0.comment.thread.id
}) }}">zobacz wszystkie wypowiedzi ({{ comments|length }})</a></p>
{% endif %}
{% else %}
{% for commentinfo in comments %}
{% include "FOSCommentBundle:Thread:comment.html.twig" with { "children": commentinfo.children, "comment": commentinfo.comment, "depth": depth, "view": view } %}
{% endfor %}
{% endif %}
I know for now i limit in twig is not finish yet but have ask fo js actions
Commens show coretly but actions (reply, add vote) not work.
I include manualy comments.js from fos https://github.com/FriendsOfSymfony/FOSCommentBundle/blob/master/Resources/assets/js/comments.js
and manually setup
<script type="text/javascript">
var fos_comment_thread_id = 'place{{ place.id }}';
var fos_comment_thread_api_base_url = '{{ path('fos_comment_get_threads') }}';
</script>
How i can initialize js actions ?
i try FOS_COMMENT.initializeListeners();
VM229:2 Uncaught ReferenceError: FOS_COMMENT is not defined(…)
js is loaded.
When i initialize in this way
<script type="text/javascript">
var fos_comment_thread_id = 'place{{ place.id }}';
var fos_comment_thread_api_base_url = '{{ path('fos_comment_get_threads') }}';
</script>
<script src="{{ asset('bundles/app/js/comments.js') }}"></script>
i get this error
Uncaught TypeError: FOS_COMMENT.thread_container.trigger is not a function