简体   繁体   中英

Sonata Admin Bundle clickable Field

I'll describe a little bit the architecture of my models to understand my problem: I'm developing a Symfony2 web-app.

And I installed the sonataMongoDB Admin Bundle to create my Admin part. The application is an online Quizzer in fact I have a document User which reference many documents Quizz. when I'm displaying the users list I need that the quiz field become clickable to go inside the quiz and see the results.

Here is the code of the ConfigureListFields function:

    protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
    ->addIdentifier('email')
    ->add('firstName')
    ->add('lastName')
    ->add('quizz', null, array('label' => 'Quiz Passd : Result'))

    ->add('_action', 'actions', array(
        'actions' => array(
            'inscription' => array('template' => 'ATSAdminBundle:CRUD:list__action_inscription.html.twig'),
            'edit' => array(),
            )
        ))
    ;
}

And here how I get my Quiz object:

  public function __toString() 
{
    return $this->getResult() ; 
}

But I want that the Quiz Field become clickable not displaying like a simple String.

I think by default the list view will not link one-to-many objects. You can do so by creating a custom template (just like you did with actions) where you can loop through the quizes and link them eg:

{% block field %}
<div>
    {% foreach object.quizzes as quizz %}
    ....
    {% foreach %}
</div>
{% endblock %}

See https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/list_field_definition.html#custom-template

If a quizz is a single related object you just need another admin class for Quizz and allow the show or the edit rule. By default Sonata will link to the edit rule. So if you don't have the role for editing Quizz nothing will be linked. Maybe that is your main problem.

If that is your case try this piece of code to verfiy it:

->add('quizz', null, array('label' => 'Quiz Passd : Result', 'route' => 'show'))

Last but not least it is more common to link to show routes inside the show view of the parent object. You can then add your Quizz(es) inside the tab menu:

protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
{
    $menu->addChild($this->trans('Quizzes'), array(
        'uri' => $admin->generateUrl('sonata.admin.quizz.list', array('id' => $id)),
    ));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM