Button handlers -> show button when entry past validation
Created 3 years ago by michiel

Is there possible way to show a button in a table when an entry past some conditions something like this

        'edit',
        'permissions' => [
            'button' => 'info',
            'icon'   => 'lock',
            'href'   => 'admin/users/permissions/{entry.id}',
            'permission' => function($entry){
               if($entry->permission)
                   return true;
                else
                    return false;
            }
        ],
    ];
ryanthompson  —  3 years ago Best Answer

Ok - so most of any UI object builder supports handlers because of this little ditty: https://github.com/anomalylabs/streams-platform/blob/master/src/Ui/Table/Command/SetDefaultParameters.php#L54

Basically anything not skipped that has a setter can be defined as a handler and not only that but can be detected automatically via classname transformation:

FooTableBuilder -> FooTableButtons

Handlers should have a handle method that can accept the original builder instance. Method injection / dependency injection is supported too since it's called from the service container. Just define your handler to set the properties on the builder:

public function handle(FooTableBuilder $builder)
{

    $builder->setButtons() {
        [
            'view'          => [
                'url' => function(EntryInterface $entry) {
                    return '/path/to/custom/url/' . $entry->getId();
                },
                'text' => 'View',
                'icon' => 'fa fa-eye',
                'type' => 'success',
            ],
        ]
    }    
}