0

I have the following issue:

Article Page contains -> Multiple ArticleUnit Blocks contains -> Multiple Media Blocks

(Edit: See in the comments below for a summary of what I indented to do.)

I built the forms separately using buttom-up approach and what I currently have in a working state is:

  1. Individually each of the 3 Documents works correctly
  2. ArticleUnit Block embeds Media Block works correctly
  3. Article Page embeds standlone ArticleUnit Block (without embedding Media Block) works correctly
  4. But when Article Page embeds ArticleUnit Block which embeds Media Block it is NOT working correctly. After clicking on 'Add New' Button in Article Page the ajax response that I get is a hyperlink 'Add New' that is not displayed on the Article Page form.

Ajax response

<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>

My code:

Article Page/News Admin (Admin of a StaticContent)

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

Article News service

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 (Admin of a Container Block)

/**
 * {@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 service

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 (Admin of an Imagine block)

/**
 * {@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();
}

Media Block service

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%']]

Thanks for reading. Any ideas/suggestions? :)

Rishi
  • 667
  • 1
  • 6
  • 13
  • hard to say what is happening here. maybe somebody else can help, but the only way i can think of would be a pull request to the cmf-sandbox with a minimal example of this problem so i can investigate what is happening in the running code. – dbu Jul 07 '15 at 07:34
  • In a nutshell, in Admin section, I tried to embed an Imagine block into a Container block, take the Container block embed into a StaticContent. Example of how it should be displayed to user is in a StaticContent's form there's an 'Add new' button which when used can append one or many Container block(s) to the form, inside each Container block another 'Add new' button that can be used to add one or many Imagine block(s). Hope that helps, we will try to do the pull request. – Rishi Jul 07 '15 at 11:26
  • @dbu Hello my colleague created the pull request: https://github.com/symfony-cmf/cmf-sandbox/pull/293 . Could you please take a look? Thanks in advance. – Rishi Jul 09 '15 at 14:33

0 Answers0