0
votes

I'm using Sonata admin in my Symfony project. Users are stored in User entity and I have provided provision for users to manage their user details as their profile. When the profile is updated, the flash message looks like,

enter image description here

but the message looks odd to me. How can I change this message to something like Profile updated successfully? Is it possible to handle User related flash messages from UserAdmin class?

Any suggestion/solution will be helpful. Thanks in advance!!

2

2 Answers

0
votes

As you can see in CrudController, when the update action is successful, for every entity, this code is run : https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Controller/CRUDController.php#L357 :

                $this->addFlash(
                    'sonata_flash_success',
                    $this->trans(
                        'flash_edit_success',
                        ['%name%' => $this->escapeHtml($this->admin->toString($existingObject))],
                        'SonataAdminBundle'
                    )
                );

So if you want to overide it :

Solution 1 : You change de translation for every entities...

Solution 2: You overide this flash message in using your own controller specific to UserEntity...

If I was you, I'll create a baseController, in order to make this translation customizable, and every entity will herit from this....

UPDATE :

In the baseController, you override each actions (create, delete, edit). After you've got the choise, the first : you create a custom action, the second, you create a protected variable for translation message ...

class OwnBaseController extends BaseController {
  ....

  protected $successMessage = "flash_edit_success";
  protected $errorMessage = "flash_edit_error";

  ....

  /** Exemple with edit action **/

                try {
                    $existingObject = $this->admin->update($submittedObject);
                    if ($this->isXmlHttpRequest()) {
                        return $this->renderJson([
                            'result' => 'ok',
                            'objectId' => $objectId,
                            'objectName' => $this->escapeHtml($this->admin->toString($existingObject)),
                        ], 200, []);
                    }
                    $this->addFlash(
                        'sonata_flash_success',
                        $this->successMessage ,    // <----- LOOK HERE
                            'messages'
                        )
                    );
                    // redirect to edit mode
                    return $this->redirectTo($existingObject);
                } catch (ModelManagerException $e) {
                    $this->handleModelManagerException($e);
                    $isFormValid = false;
                } catch (LockException $e) {
                    $this->addFlash('sonata_flash_error', $this->trans($this->errorMessage, [  // <----- LOOK HERE
                        '%name%' => $this->escapeHtml($this->admin->toString($existingObject)),
                        '%link_start%' => '<a href="'.$this->admin->generateObjectUrl('edit', $existingObject).'">',
                        '%link_end%' => '</a>',
                    ], 'SonataAdminBundle'));
                }


class UserController extends OwnBaseController {
   protected $successMessage = "user_edit_success_custom_message";
   protected $errorMessage = "user_edit_error_custom_message";

}
0
votes

I have overrided the translation file to change the flash messages which is common to all entity actions.

I just copied SonataAdminBundle.en.xliff file from

/vendor/sonata-project/admin-bundle/src/Resources/translations/

and placed it in either src/Resources/SonataAdminBundle/translations or /translations and edit the messages as you wish.