2
votes

I have a Post entity which can have multiple comments. I want to show a link to the comments in the Post list view as a new column.

For that I have defined a route which will be used inside the Post list generator.

admin_post_comments:
    prefix: /admin
    path: /post/{post_id}/comment/list


protected function configureListFields(ListMapper $listMapper)
{
        $listMapper
            ->addIdentifier('title', null, array('label' => 'Title'))
            ->add('comments', 'url',
                array('label' => 'Comments',
                      'route' => array(
                        'name' => 'admin_post_comments',
                        'parameters' => array('post_id' => 'post.id')
                      )));
}

But how do I get the actual "post id" for each row, in order to generate the correct ur? Right now the URL will be "/post/post.id/comment/list" but needs to be "/post/1/comment/list"

When I choose "comments" as the column field type, then the field stays empty. How can I get it to write "view comments"?

Thanks,

Hendrik

1

1 Answers

1
votes

I think that you don't have access to this information in this way. But if you define a field template in this way:

        ->add('comments', 'url', array(
            'label' => 'Comments',
            'template' => ':list:list.template.comments.html.twig'
        ))

In this other way you may define a twig with your link, and also must to include a comment count if you want. Something similar like this:

<td class="sonata-ba-list-field sonata-ba-list-field-{{ field_description.type }}" objectId="{{ admin.id(object) }}">
{{ path(admin_post_comments, {'id': object.id}) }}<span class="badge"">{{ object.comments|length }}</span>

In this TWIG you can access to admin, object (The post), value and many other variables