1

I have an entity with a required active user field so I need to add the active user's name in configureFormFields():

class DokumentAdmin extends Admin
{    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
                ->add('email')
                 ...
                ->add('user_name',null,array('required' => true, 'data' => "THIS IS A LOGGED ADMIN NAME"))
        ;
    }

I tried to use a listener,

public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        if ($entity instanceof Dokument) {
         //set user name

        }
    }

but I don't now how use the container object here.

1

3 Answers 3

5

You can use the container as below:

$this->getConfigurationPool()->getContainer();

For example:

protected function configureFormFields(FormMapper $formMapper)
{
    $container = $this->getConfigurationPool()->getContainer();
    ...
    // Using the container here...
}
3

You can inject the container:

Try the following:

class DokumentAdmin extends Admin {

private $container = null;

/**
 * @param string $code
 * @param string $class
 * @param string $baseControllerName
 */
public function __construct($code, $class, $baseControllerName, $container=null)
{
    parent::__construct($code, $class, $baseControllerName);
    $this->container = $container;  
}
....

Enter into your service.yml the @service_container to admin entry.

acme.demo.document:
      class: Acme\DemoBundle\Admin\DocumentAdmin
      tags:
      arguments: [null, Acme\Demobundle\Entity\Document, ApplicationAcmeDemoBundle:Default, @service_container]

That's it. Now you have access to the container in your admin class.

0
0

I have the same issue. I need to parse some parameters from my config.yml but it fails.

As solution, you can inject the container to your admin class, but this is not recommanded. You should inject only services or parameters you need.

How to do ?

you should use the setter injection instead of construct injection.

Here is an example of injecting a parameters in snata Admin Class:

when yu define your admin service, just add a call like this:

<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 your parameter using setter injection -->
        <call method="setEnabledLocales">
            <argument>%skonsoft_znata.locales%</argument>
        </call>
    </service>

After, just add a method called setEnabledLocales inside your admin class that like

public function setEnabledLocales($locales){
    $this->enabedLocales = $locales;
}

Don't forget to add $enabedLocales as property in your admin class.

Finally you can use this property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.