Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: enable custom user profile templates #52

Open
truckee opened this issue Mar 5, 2014 · 0 comments
Open

Feature request: enable custom user profile templates #52

truckee opened this issue Mar 5, 2014 · 0 comments

Comments

@truckee
Copy link

truckee commented Mar 5, 2014

It may be possible to enable custom user profile templates with the simple modification to Configuration.php as in the following excerpt:

->children()
    ->arrayNode('profile')
        ->addDefaultsIfNotSet()
        ->children()
            ->arrayNode('form')
            ->addDefaultsIfNotSet()
                ->children()
                    ->scalarNode('type')->defaultValue(null)->end()
                    ->scalarNode('name')->defaultValue('fos_user_profile_form')->end()
                    ->arrayNode('validation_groups')
                        ->prototype('scalar')->end()
                        ->defaultValue(array('Profile', 'Default'))
                    ->end()
                ->end()
            ->end()
            ->scalarNode('template')->defaultValue(null)->end()
         ->end()
    ->end()
->end()

Custom templates are possible with this modification and extending FOSUserBundle to allow the use of a custom ProfileController like the following:

class ProfileController extends BaseController
{

    /**
     * Edit the user
     */
    public function editAction(Request $request)
    {
        $user = $this->container->get('security.context')->getToken()->getUser();
        if (!is_object($user) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        $discriminator = $this->container->get('pugx_user.manager.user_discriminator');
        $users = $this->container->getParameter('pugx_user_discriminator_users');
        $class = $discriminator->getClass($user);

        foreach ($users as $userType) {
            if ($userType['entity']['class'] == $class) {
                $templateString = $userType['profile']['template'];
                if (false === strpos($templateString, $this->container->getParameter('fos_user.template.engine'))) {
                    $template = 'FOSUserBundle:Profile:edit.html.';
                } else {
                    $l = strrpos($templateString, ".");
                    $template = substr($templateString, 0, $l + 1);
                }
            }
        }

        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->container->get('event_dispatcher');

        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);

        if (null !== $event->getResponse()) {
            return $event->getResponse();
        }

        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->container->get('fos_user.profile.form.factory');

        $form = $formFactory->createForm();
        $form->setData($user);

        if ('POST' === $request->getMethod()) {
            $form->bind($request);

            if ($form->isValid()) {
                /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
                $userManager = $this->container->get('fos_user.user_manager');

                $event = new FormEvent($form, $request);
                $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);

                $userManager->updateUser($user);

                if (null === $response = $event->getResponse()) {
                    $url = $this->container->get('router')->generate('fos_user_profile_show');
                    $response = new RedirectResponse($url);
                }

                $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

                return $response;
            }
        }

        return $this->container->get('templating')->renderResponse(
                        $template . $this->container->getParameter('fos_user.template.engine'), array('form' => $form->createView())
        );
    }

This has all been at the limit of my skill set, so if it's out of place please let me know. But it was fun figuring it out.

Edit: I suspect there is much more to this request than originally thought. Ignore the above if it is deemed dumb.

George

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant