Question

I'd like to provide a direct link to related entities of my current entity-listing.

I've tried to use addIdentifier for the related entities column but it redirects me to my current entity.

I found nothing in the doc explaining how to achieve this.

Here is an example of what I'm doing:

enter image description here

I'm currently listing my "data profile" entities. They have related entities such as "Entrainement", "Niveau", or "Programme". I would like to be redirected to the "Programme" list if I click on "Se defouler", and similarly for each of the columns which display related entities.

Here is my configureListFields implementation from my current admin class:

/**
  * @param ListMapper $listMapper
  */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier( 'type.titre' , null, array ( 'label' => 'Type') )
        ->add('valeur', null, array ( 'label' => 'Valeur' ) )
        ->add('bioprofile.titre', null, array ( 'label' => 'Bio Profile' ) )
        ->add( 'exercicesData.entrainement.niveau.titre', null, array( 'label' => 'Niveau' , 'route' => array( 'name' => 'edit') ) )
        ->add( 'exercicesData.entrainement.titre' , many_to_one, array ( 'label' => 'Entrainement' ) )
        ->add( 'exercicesData.entrainement.niveau.programme.titre' , null , array ( 'label' => 'Programme' ) )
        ->add('_action', 'actions', array(
            'actions' => array(
                'show' => array(),
                'edit' => array(),
                'delete' => array(),
            )
        ))
    ;
}

I tried different ways:

  • one with the option "route" which contains an array where the name of the action of the related entity redirects ( according to the official doc )
  • another one with the "many_to_one" list type , which, according the official doc, redirects to the aimed entity.

Neither of them works.

PS: In the screenshot, there are some links on "Niveau 2", "Seance 1" and "se defouler". They were actually displayed using addIdentifier.

Was it helpful?

Solution

Sonata add a link to the related entity only when you add the entity, and not when you add a property of the entity.

The following example will add a link to Entity2:

/**
 * @param ListMapper $listMapper
 */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ...
        ->add('Entity2')
        ...
    ;
}

Whereas this example will not add a link to the entity2:

/**
 * @param ListMapper $listMapper
 */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ...
        ->add('Entity2.name')
        ...
    ;
}

If you want to display the name property in the link, you have to add or change the __toString() method in Entity2 :

class Entity2
{
    ...
    public function __toString()
    {
        return (string) $this->name;
    }
}

Your admin class should be like this:

/**
 * @param ListMapper $listMapper
 */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier( 'type.titre' , null, array ( 'label' => 'Type') )
        ->add('valeur', null, array ( 'label' => 'Valeur' ) )
        ->add('bioprofile.titre', null, array ( 'label' => 'Bio Profile' ) )
        ->add( 'exercicesData.entrainement.niveau', null, array( 'label' => 'Niveau' , 'route' => array( 'name' => 'edit') ) )
        ->add( 'exercicesData.entrainement' , many_to_one, array ( 'label' => 'Entrainement' ) )
        ->add( 'exercicesData.entrainement.niveau.programme' , null , array ( 'label' => 'Programme' ) )
        ->add('_action', 'actions', array(
            'actions' => array(
                'show' => array(),
                'edit' => array(),
                'delete' => array(),
            )
        ))
    ;
}

And you should change the following entities:

Entity Entrainement:

class Entrainement
{
    ...
    public function __toString()
    {
        return (string) $this->titre;
    }
}

Entity Niveau:

class Niveau
{
    ...
    public function __toString()
    {
        return (string) $this->titre;
    }
}

Entity Programme:

class Programme
{
    ...
    public function __toString()
    {
        return (string) $this->titre;
    }
}

Hope this helps

OTHER TIPS

The default route is edit, if you disable the edit route in the target entity sonataAdmin service, you should use ->add('{yourRelationAttribute}', null, ['label' => 'label', 'route' => ['name' => 'show']])

I know that is not the problem for you but I was stuck too and I think it can help someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top