I added a new field to the Oro account (* level * field) and some authorization rules for it. When I'm updating an account, with a user with a role without access, the field level is not displayed, but the value of account level is cleared when I submet the form.
So I’d like to add the level every time I update an account. I tried to extend the account controller and change the update method with the following code:
protected function update(Account $entity = null)
{
if (!$entity) {
$entity = $this->getManager()->createEntity();
}
$authorizationChecker = $this->get('security.authorization_checker');
if (!$authorizationChecker->isGranted('EDIT', new FieldVote($entity, 'level'))) {
$entityFromDB = $this->getManager()->find($entity->getId());
$levelFromDB = $entityFromDB->getLevel();
$entity->setLevel($levelFromDB);
}
return $this->get('oro_form.model.update_handler')->update(
$entity,
$this->get('oro_account.form.account'),
$this->get('translator')->trans('oro.account.controller.account.saved.message'),
$this->get('oro_account.form.handler.account')
);
}
However, when saving, the account level is changed to blank.
Can anyone give me some help? I'm using OroCRM 2.6.19
Thanks guys!
Looks like you have assigned the value too early and it is overridden when the form is submitted.
I recommend you to move the logic to the doctrine even listener to preUpdate
event, so it would be executed after the form is submitted and will override the data from the form.