PUGXMultiUserBundle:自定义配置文件模板 [英] PUGXMultiUserBundle: custom profile template

查看:33
本文介绍了PUGXMultiUserBundle:自定义配置文件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启用自定义用户配置文件模板的方法涉及修改供应商的Configuration.php.有没有没有的方法?或者这是一个可行的解决方案?当前方法如下所示:

A method to enable custom user profile templates involves modifying the vendor's Configuration.php. Is there a method that does not? Or is this a viable solution? Current method appears below:

更新:我认为我的包需要负责配置,所以我正处于破译和应用 CompilerPassInterface 的阵痛中.

Update: I'm thinking my bundle needs to take responsibility for the configuration so I'm in the throes of deciphering and applying the CompilerPassInterface.

pugx_multi_user:
  users:
    staff:
        entity: 
          class: Acme\MyBundle\Entity\Staff
#          factory: 
        registration:
          form: 
            type: Acme\UserBundle\Form\RegistrationStaffFormType
            name: fos_user_registration_form
            validation_groups:  [Registration, Default]
          template: AcmeUserBundle:Registration:staff.form.html.twig
        profile:
          form:
            type: Acme\UserBundle\Form\ProfileStaffFormType
            name: fos_user_profile_form
            validation_groups:  [Profile, Default]
# template line added
          template: AcmeUserBundle:Profile:staff.form.html.twig 
         ...

摘自 PUGX\MultiUserBundle\DependencyInjection\Configuration.php

[注意添加->scalarNode('template')->defaultValue(null)->end()]

...
                        ->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()
...

ProfileController(在扩展用户包中)

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())
        );
    }

推荐答案

我已经创建了一个拉取请求 在这里处理自定义配置文件模板.

I have create a pull request here that handles custom profile templates.

这篇关于PUGXMultiUserBundle:自定义配置文件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆