How to retrieve request or locale within an entity instance?

For some translation purpose, I need to retrieve the request or the locale in my entities.

I tried to do what is written here but it’s not working with entities.

/**
 * AccessoryType constructor.
 *
 * @param RequestStack $requestStack
 */
public function __construct(RequestStack $requestStack) {
    $this->request=$requestStack->getCurrentRequest();
}

I get the following error message (which is to be excepted I guess…)

Too few arguments to function AppEntityMap::__construct(), 0 passed in /var/www/sphere/src/Controller/MapController.php on line 34 and exactly 1 expected

My end goal is to have the right getter called based on the locale, mostly for forms, which use __toString() to generate fields name.

/**
 * @return string
 */
public function __toString() {
    if($this->request->getLocale() != 'en') {
        $call='getName'.ucfirst($this->request->getLocale());

        return strlen($this->$call()) > 0 ? $this->$call() : $this->getName();
    } else {
        return $this->getName();
    }
}

Any suggestion to retrieve the request or locale within an entity?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *