Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SilverStripe edit gridfield success message on save

Tags:

silverstripe

What's the easiest way to edit the default success message when saving an item in GridField edit view?

The message seems to be in a variable in class GridFieldDetailForm within method doSave.

$message = _t(
        'GridFieldDetailForm.Saved',
        'Saved {name} {link}',
        array(
            'name' => $this->record->i18n_singular_name(),
            'link' => $link
        )
    );
like image 558
Semicolon Avatar asked Jan 28 '26 14:01

Semicolon


2 Answers

Since the message uses the _t() function it will attempt to fetch the value defined in the lang file corresponding to the current user's locale. The default string defined in the function is just a fallback for when no translation could be found within the lang files.

To change the message you can update your site's yml lang file located in mysite/lang/{LANGUAGE_CODE}.yml

For english this would be:

# mysite/lang/en.yml
# remember to flush after editing me :-)
en:
  GridFieldDetailForm:
    Saved: 'My custom message using {name} and here is a link to the object: {link}'

https://docs.silverstripe.org/en/3.4/developer_guides/i18n/

like image 104
jjjjjjjjjjjjjjjjjjjj Avatar answered Jan 31 '26 19:01

jjjjjjjjjjjjjjjjjjjj


Something like this should work for specific implementations

$form = $gridField->getConfig()->getComponentByType('GridFieldDetailForm');
$form->setItemEditFormCallback(function($form, $itemRequest)
{
    // Replace save action with custom method in here
});

For more general implementations, you'll likely want to extend GridFieldDetailForm and override doSave, then replace the GridFieldDetailForm component with your custom class.

like image 40
Joundill Avatar answered Jan 31 '26 20:01

Joundill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!