Symfony CMF嵌套嵌入式表单

时间:2015-07-06 09:18:20

标签: symfony-forms symfony-cmf

我有以下问题:

文章页面包含 - >多个ArticleUnit块包含 - >多媒体块

编辑:请参阅下面的评论,了解我要缩进的内容摘要。)

我使用buttom-up方法单独构建表单,我目前处于工作状态的是:

  1. 3个文件中的每一个单独正常工作
  2. ArticleUnit Block嵌入Media Block正常工作
  3. 文章页面嵌入了StandUnone ArticleUnit Block(没有嵌入Media Block)正常工作
  4. 但是当文章页面嵌入嵌入媒体块的ArticleUnit Block时,它无法正常工作。点击“添加新内容”后点击文章页面中的按钮我得到的ajax响应是一个超链接'添加新的'这不会显示在文章页面上。
  5. Ajax响应

    <div id="field_container_s559a3c68b30e7_children" class="field-container">
        <span id="field_widget_s559a3c68b30e7_children" >
    <div>
    </div>
    
    </span>
    
    <span id="field_actions_s559a3c68b30e7_children" >
        <a
            href="/Proximity_Projects/xx/refonte_2015/www/trunk/web/admin/xx/block/articleunitblock/create?uniqid=s559a3c8bf24d1&amp;code=xx.block.admin.article_unit_block_admin&amp;pcode=xx.article.admin.article.news&amp;puniqid=s559a3c68b30e7"
            onclick="return start_field_retrieve_s559a3c68b30e7_children(this);"
            class="btn btn-success btn-sm btn-outline sonata-ba-action"
            title="Add new"
            >
            <i class="icon-plus fa fa-plus-circle"></i>
            Add new
        </a>
    </span>
    
    <!-- edit one association -->
    
    <script type="text/javascript">
    
        // handle the add link
        var field_add_s559a3c68b30e7_children = function(event) {
    
            event.preventDefault();
            event.stopPropagation();
    
            var form = jQuery(this).closest('form');
    
            // the ajax post
            jQuery(form).ajaxSubmit({
                url: 'http://localhost:8080/Proximity_Projects/xx/refonte_2015/www/trunk/web/admin/core/append-form-field-element?code=xx.article.admin.article.news&elementId=s559a3c68b30e7_children&uniqid=s559a3c68b30e7',
                type: "POST",
                dataType: 'html',
                data: { _xml_http_request: true },
                success: function(html) {
                    jQuery('#field_container_s559a3c68b30e7_children').replaceWith(html); // replace the html
                    if(jQuery('input[type="file"]', form).length > 0) {
                        jQuery(form).attr('enctype', 'multipart/form-data');
                        jQuery(form).attr('encoding', 'multipart/form-data');
                    }
                    jQuery('#sonata-ba-field-container-s559a3c68b30e7_children').trigger('sonata.add_element');
                    jQuery('#field_container_s559a3c68b30e7_children').trigger('sonata.add_element');
                }
            });
    
            return false;
        };
    
        var field_widget_s559a3c68b30e7_children = false;
    
        // this function initialize the popup
        // this can be only done this way has popup can be cascaded
        function start_field_retrieve_s559a3c68b30e7_children(link) {
    
            link.onclick = null;
    
            // initialize component
            field_widget_s559a3c68b30e7_children = jQuery("#field_widget_s559a3c68b30e7_children");
    
            // add the jQuery event to the a element
            jQuery(link)
                .click(field_add_s559a3c68b30e7_children)
                .trigger('click')
            ;
    
            return false;
        }
    </script>
    
    <!-- / edit one association -->
    
    </div>
    

    我的代码:

    文章页面/新闻管理员(静态内容管理员)

    /**
     * Service name of the sonata_type_collection service to embed
     * @var string
     */
    protected $embeddedArticleUnit;
    
    /**
     * Configure the service name (admin_code) of the admin service for the embedded units
     *
     * @param string $adminCode
     */
    public function setEmbeddedArticleUnitAdmin($adminCode)
    {
        $this->embeddedArticleUnit = $adminCode;
    }
    
    protected function configureFormFields(FormMapper $formMapper)
    {
        parent::configureFormFields($formMapper);
    
        $formMapper
            ->with('form.group_general', ['name' => 'Details'])
            ->add('body', 'hidden', ['data' => 'default'])
            ->add('colour', 'choice', [
                'choices' => [
                    'red' => 'Red',
                    'blue' => 'Blue',
                    'yellow' => 'Yellow',
                ],
            ])
            ->end()
            ->with('Items')
            ->add('children', 'sonata_type_collection',
                ['label' => 'Text'],
                [
                    'edit' => 'inline',
                    'inline' => 'standard',
                    'admin_code' => $this->embeddedArticleUnit,
                ])
            ->end();
    }
    

    文章新闻服务

    xx.article.admin.article.news:
        class: xx\ArticleBundle\Admin\NewsAdmin
        arguments:
            - ''
            - xx\ArticleBundle\Document\News
            - 'SonataAdminBundle:CRUD'
        tags:
            - { name: sonata.admin, manager_type: doctrine_phpcr, group: 'xx Dashboard', label: 'News' }
        calls:
            - [setRouteBuilder, ['@sonata.admin.route.path_info_slashes']]
            - [setRootPath, ['%cmf_base_page_article_news%']]
            - [setContainer,[@service_container]]
            - [setEmbeddedArticleUnitAdmin, ['xx.block.admin.article_unit_block_admin']]
            - [setEmbeddedMediaBlockAdmin, ['xx.block.admin.media_block_admin_header_image']]
    

    ArticleUnit Block Admin(容器块的管理员)

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('form.group_general', ['name' => 'General'])
                ->add('body', 'textarea', ['required' => false])
                ->add('mediaPosition', 'choice', [
                    'choices' => [
                        'bottom' => 'Bottom',
                        'right' => 'Right',
                        'left' => 'Left',
                    ],
                ])
                ->add('parentDocument', 'doctrine_phpcr_odm_tree', ['root_node' => $this->getRootPath(), 'choice_list' => [], 'select_root_node' => true])
                ->add('position', 'hidden', array('mapped' => false))
            ->end()
            ->with('Items')
                ->add('children', 'sonata_type_collection',
                    ['label' => 'Image(s)'],
                    [
                        'edit' => 'inline',
                        'inline' => 'table',
                        'admin_code' => $this->embeddedAdminCode,
                        'sortable' => 'position',
                    ])
            ->end()
        ;
    }
    

    ArticleUnit Block服务

    xx.block.admin.article_unit_block_admin:
        class: xx\BlockBundle\Admin\ArticleUnitBlockAdmin
        arguments:
            - ''
            - xx\BlockBundle\Document\ArticleUnitBlock
            - 'SonataAdminBundle:CRUD'
        tags:
            - { name: sonata.admin, manager_type: doctrine_phpcr, group: 'xx Dashboard', label: 'Article Unit Block' }
        calls:
            - [setRouteBuilder, ['@sonata.admin.route.path_info_slashes']]
            - [setEmbeddedMediaAdmin, ['xx.block.admin.media_block_admin']]
            - [setRootPath, ['%cmf_base_block%']]
    

    Media Block Admin(Imagine块的管理员)

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        parent::configureFormFields($formMapper);
    
        $formMapper
            ->with('form.group_general')
            ->remove('filter')
            ->remove('name')
            ->remove('position')
            ->add('imageMobile', 'cmf_media_image', ['required' => false])
            ->add('imageTablet', 'cmf_media_image', ['required' => false])
            ->add('label', 'text', ['label' => 'Label (Alt)'])
            ->add('position', 'hidden', array('mapped' => false))
            ->end();
    }
    

    媒体块服务

    xx.block.admin.media_block_admin:
        class: xx\BlockBundle\Admin\MediaBlockAdmin
        arguments:
            - ''
            - xx\BlockBundle\Document\MediaBlock
            - 'SonataAdminBundle:CRUD'
        tags:
            - { name: sonata.admin, manager_type: doctrine_phpcr, group: 'xx Dashboard', label: 'Media Block' }
        calls:
            - [setRouteBuilder, ['@sonata.admin.route.path_info_slashes']]
            - [setRootPath, ['%cmf_base_block%']]
    

    感谢阅读。有什么想法/建议吗? :)

0 个答案:

没有答案