Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8741551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:20:12+00:00 2026-06-13T11:20:12+00:00

I try to set the current authenticated user within a SonataAdminBundle Form: protected function

  • 0

I try to set the current authenticated user within a SonataAdminBundle Form:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
    ->add('title')
    ->add('content')
    ->add('slug')
    //->add('user')
    ;
}

Since I don’t want the user to select/choose it’s own user, I guess I’d like to set the username during the procedure the form was sent by the user (without any user object).
(Note: I’d like to use this in a couple of forms, so I’ll need a general solution.)

What I’ve done now was setting up an EventListener after I’ve read this: http://symfony.com/doc/current/cookbook/service_container/event_listener.html

class PostListener
{
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }


    public function prePersist(LifeCycleEventArgs $args)
    {
        $entity = $args->getEntity();

        //Get user
        $securityContext = $this->container->get('security.context');
        $user = $securityContext->getToken()->getUser();

        //Set authenticated user as autor
        $entity->setUser($user);
    }
}

Including service:

post.listener:
  class: Backender\BlogBundle\Listener\PostListener
  calls:
    - [ setContainer, [ @service_container ] ]
  tags:
    - { name: doctrine.event_listener, event: prePersist }

Not sure now, if this is the right approach, because I wan’t to specify the form where I want to set the user. (This one will want to set user on every form right?)

With some more research it seems like I’ve to use an Event Subscriber like this: http://symfony.com/doc/2.0/cookbook/form/dynamic_form_generation.html
In this example they use FormEvents::PRE_SET_DATA, I guess in my case I have to use POST_SET_DATA.

Here I’ve some problems!:

1: I’m quiet new to SonataAdminBundle and there we use protected function configureFormFields(FormMapper $formMapper)… where ->addEventSubscriber() isn’t available?

2: Is this the right way?, I really didn’t find any examples like what I need here for sonata-admin.

I’m thankful for every help!
Best regards…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:20:13+00:00Added an answer on June 13, 2026 at 11:20 am

    In the end I found 2 possible solutions for my problem. I’d like to describe these possibilities here because there isn’t that much stuff to find about.

    Event listener with query of instance of
    Call as service:

       blog.post.listener:
          class: Acme\BlogBundle\Listener\PostListener
          arguments: ['@service_container']
          tags: 
            - { name: doctrine.event_listener, event: prePersist }
    

    The listener class:

    class PostListener
    {
        protected $container;
    
        public function __construct(ContainerInterface $container)
        {
            $this->container = $container;
        }
    
    
        public function prePersist(LifeCycleEventArgs $args)
        {
            $entity = $args->getEntity();
    
            //Get user
            $securityContext = $this->container->get('security.context');
            $user = $securityContext->getToken()->getUser();
    
            if ($entity instanceof Post) {
    
                //Set authenticated user as autor
                $entity->setUser($user);
    
            }
        }
    }
    

    The second way (and maybe the easier) is to use prePersist in Admin Class of SonataAdminBundle

    Call as service (use service container or security context as argument):

    blog.admin.post:
      class: Acme\BlogBundle\Admin\PostAdmin
      tags:
        - { name: sonata.admin, manager_type: orm, group: Article Handling, label: Posts }
      arguments: [null, Acme\BlogBundle\Entity\Post, SonataAdminBundle:CRUD, @service_container]
    

    PostAdmin:
    (Note, i use container because i need further stuff in the future)

    class PostAdmin extends Admin
    {
        protected $securityContext;
    
        public function __construct($code, $class, $baseControllerName, ContainerInterface $container)
        {
            parent::__construct($code, $class, $baseControllerName);
            $this->container = $container;
        }
    
        protected function configureFormFields(FormMapper $formMapper)
        {
            $formMapper
            ->add('title')
            ->add('content')
            ;
    
        }
    
    
        public function prePersist($post)
        {
            $user = $this->container->get('security.context')->getToken()->getUser();
            $post->setUser($user);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello when I try to set the current progress of a seekbar that is
I am using ui:repeat tag and I have set varstatus=current.I try to display the
try to set host in dsn of pdo like this: <?php /* Connect to
I try to set friendship to method from class GameSimulator on Player class. for
I try to set a timeout on an element, fired with a jQuery plugin.
I try to set an attribute in a XML node using MSXML. IXMLDOMElement alone
I try to set up .hgrc file: [ui] username = MyName MySecondName <mymail@gmail.com> I
Every time try to set the value of any variable in my model object,
I have try to set icon for my App in the xCode. In the
When I try and set the datatable width just the table main section is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.