Sonataadminbundle: Add possibility to create a dynamic action/route for identifiers in grid

Created on 4 Jul 2016  路  15Comments  路  Source: sonata-project/SonataAdminBundle

We have list of entities and some of them should go to edit and some to show (depends on isGranted call). Currently it is impossible because we set route when configuring list fields.

feature pending author stale

Most helpful comment

@Soullivaneuh it would probably be something like this:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper->addIdentifier('id', null, [
        'route_name' => function ($object) {
            return $this->isGranted('YOUR_ROLE', $object) ? 'role_route' : 'another_route';
        },
    ]);
}

the closure being executed for each row and the return value used as the route name.

All 15 comments

Actually you can do that:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper->addIdentifier('id', null, [
        'route_name' => $this->isGranted('YOUR_ROLE') ? 'role_route' : 'another_route',
    ]);
}

Is this what you're looking for?

I changed the issue title 馃槃

@Soullivaneuh I should call $this->isGranted('YOUR_ROLE', $object) for each item in grid.

@Koc I don't think it should be the role of the ListMapper.

BTW, we are trying to reduce code size on the admin class and your request will do the reverse IMHO.

cc @greg0ire

I should call $this->isGranted('YOUR_ROLE', $object) for each item in grid.

BTW, if you have to add this check on multiple field, in this case just store the result on a variable before.

BTW, if you have to add this check on multiple field, in this case just store the result on a variable before.

$object won't be the same between 2 iterations, so no.

BTW, we are trying to reduce code size on the admin class and your request will do the reverse IMHO.

Will it ? @Koc didn't mention the admin class at all for the moment, did he ?

$object won't be the same between 2 iterations, so no.

What do you mean? How the object could change on configureListFields?

Will it ? @Koc didn't mention the admin class at all for the moment, did he ?

Indeed. The admin object is already on the ListMapper.

Still think it's not the role of this class IMHO.

What do you mean? How the object could change on configureListFields?

IIUC he has a list, and wants to show an edit link for object in iteration 1, and a show link for object in iteration 2, which is not the same, right ?

@Soullivaneuh it would probably be something like this:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper->addIdentifier('id', null, [
        'route_name' => function ($object) {
            return $this->isGranted('YOUR_ROLE', $object) ? 'role_route' : 'another_route';
        },
    ]);
}

the closure being executed for each row and the return value used as the route name.

@jvasseur And what is the $this placed on the anonymous function?

After that yeah, it's an idea. :wink:

But we have to be sur it will be useful.

If for most of case it's depends on the isGranted method, a ternary could be enough, could not be?

IIUC he has a list, and wants to show an edit link for object in iteration 1, and a show link for object in iteration 2, which is not the same, right ?

Oh ok, $this can't change, I understand the misunderstanding now.

@Soullivaneuh $this is the admin class.

A closure is the best solution, it allows the most flexibility (you could need to do the check on an a property of $object instead of an isGranted call).

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Was this page helpful?
0 / 5 - 0 ratings