GridField custom action

Silverstripe Version: 4.5.2

Question:

I have added a custom action button to a GridField row, as per the documentation: https://docs.silverstripe.org/en/4/developer_guides/forms/how_tos/create_a_gridfield_actionprovider/

This appears to be working fine, but I have 2 questions:

  1. how do I style the button? I can eg. use DBField within a GridField row to create an HTML styled button (green icon on left). Where and how do I apply this styling to the grey custom action button?
  2. how/where do I apply conditional logic to display the button? Eg. I’d like to only display the button over 3 days around the current date and hide it the rest of the time.

22%20AM

Ok, have figured this out…

Both of these issues can be configured via the getColumnContent function. eg.

public function getColumnContent($gridField, $record, $columnName) 
{
    if (!$record->canEdit()) {
        return;
    }

    // only display if trip starting 2 days before/after today
    $daysDiff = $record->dbObject('DateTimeStart')->TimeDiffIn('days');
    if ($daysDiff > 2) {
        return;
    }

    $field = GridField_FormAction::create(
        $gridField,
        'SignIn'.$record->ID,
        false,
        "emailsigninform",
        ['RecordID' => $record->ID]
    )
        // format custom action button
        ->addExtraClass('btn btn--no-text btn--icon-xl font-icon-up-circled')
        ->setAttribute('style', 'transform:rotate(90deg); color:white; background:green; padding:.5em; border-radius: 5px;')
        ->setDescription('Sign in ' . $record->Title);

    return $field->Field();
}
1 Like