Here is the best way to use services in sonata admin classes:
Just inject the needed service using setter injection. Constructor injections are not allowed in this case, because you would have to override the constructor of the parent class. The constructor of the parent class accepts only 3 parameters, so you can not add another one.
The solution is:
<!-- file: services.xml -->
<service id="skonsoft.znata.admin.keyword" class="%skonsoft.znata.admin.keyword.class%">
<tag name="sonata.admin" manager_type="orm" group="Keyword" label="Keyword"/>
<argument />
<argument>%skonsoft.znata.admin.keyword.entity.class%</argument>
<argument>SonataAdminBundle:CRUD</argument>
<call method="setTranslationDomain">
<argument>SkonsoftZnataBundle</argument>
</call>
<!-- here you inject needed services or parameters -->
<call method="setEnabledLocales">
<argument>%skonsoft_znata.locales%</argument>
</call>
</service>
Then, just add a public method in your admin class called setEnabledLocales
.
/* file: MyclassAdmin.php */
public function setEnabledLocales($locales){
$this->enabedLocales = $locales;
}
Take a look at:
Service Container documentation
getContainer()
of the Admin Pool has been deprecated since Sonata Admin 3.77.0 and has been removed in 4.x. See the other answers for alternative (= the correct) methods. – Vacillate