Version

Documentation » Doctrine2 PHPCR Admin

4. Filter Field Definition »

« 2. Configuration

3. Document Tree

This admin integrates with the CmfTreeBrowserBundle to provide a tree view of the documents.

3.1. Enable the Bundles

You need to load two additional bundles to use the tree:

// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new FOS\JsRoutingBundle\FOSJsRoutingBundle(),
            new Symfony\Cmf\Bundle\TreeBrowserBundle\CmfTreeBrowserBundle(),
        );

        // ...
    }
}

You also need to load the routing of those bundles:

  • YAML
    # app/config/routing.yml
    
    cmf_tree:
        resource: .
        type: 'cmf_tree'
    
    fos_js_routing:
        resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"
    
  • XML
    <!-- app/config/routing.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing
            http://symfony.com/schema/routing/routing-1.0.xsd">
    
        <import
            resource="."
            type="cmf_tree"
        />
    
        <import
            resource="@FOSJsRoutingBundle/Resources/config/routing/routing.xml"
        />
    
    </routes>
    
  • PHP
    // app/config/routing.php
    use Symfony\Component\Routing\RouteCollection;
    
    $collection = new RouteCollection();
    
    $collection->addCollection($loader->import('.', 'cmf_tree'));
    
    $collection->addCollection($loader->import(
        "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"
    ));
    
    return $collection;
    

3.2. Dashboard Block

This bundle provides a block suitable for showing all documents on the sonata dashboard. We recommend using the ajax_layout and adding the sonata_admin_doctrine_phpcr.tree_block to get a tree view of your PHPCR content:

  • YAML
    #app/config/config.yml
    # ...
    sonata_block:
        blocks:
            # ...
            sonata_admin_doctrine_phpcr.tree_block:
                settings:
                    id: '/cms'
                contexts:   [admin]
    
    sonata_admin:
        dashboard:
            blocks:
                - { position: left, type: sonata_admin_doctrine_phpcr.tree_block }
                - { position: right, type: sonata.admin.block.admin_list }
    
  • XML
    <!-- app/config/config.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services">
    
        <config xmlns="http://sonata-project.org/schema/dic/block">
            <! ... -->
            <block id="sonata_admin_doctrine_phpcr.tree_block">
                <setting id="id">/cms</setting>
                <context>admin</context>
            </block>
        </config>
    
        <config xmlns="http://sonata-project.org/schema/dic/admin">
            <dashboard>
                <block position="left" type="sonata_admin_doctrine_phpcr.tree_block"/>
                <block position="right" type="sonata.admin.block.admin_list"/>
            </dashboard>
        </config>
    
    </container>
    
  • PHP
    // app/config/config.php
    $container->loadFromExtension('sonata_block', array(
        'blocks' => array(
            // ...
            'sonata_admin_doctrine_phpcr.tree_block' => array(
                'settings' => array(
                    'id' => '/cms',
                ),
                'contexts' => array('admin'),
            ),
        ),
    ));
    
    $container->loadFromExtension('sonata_admin', array(
        'dashboard' => array(
            'blocks' => array(
                array('position' => 'left', 'type' => 'sonata_admin_doctrine_phpcr.tree_block'),
                array('position' => 'right', 'type' => 'sonata.admin.block.admin_list'),
            ),
        ),
    ));
    

3.3. Configuring the tree

You have to manually configure what types of documents are allowed in the tree and which class may accept what classes as children to manage the creation and move operations. Documents that have no configuration entry or are not configured as valid child of the respective parent document will not be visible in the tree.

This configuration is global for all your document trees.

  • YAML
    # app/config/config.yml
    sonata_doctrine_phpcr_admin:
        document_tree_defaults: [locale]
        document_tree:
            Doctrine\ODM\PHPCR\Document\Generic:
                valid_children:
                    - all
            Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent:
                valid_children:
                    - Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\SimpleBlock
                    - Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ContainerBlock
                    - Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ReferenceBlock
                    - Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ActionBlock
            Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ReferenceBlock:
                valid_children: []
            # ...
    
  • XML
    <!-- app/config/config.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services">
    
        <config xmlns="http://sonata-project.org/schema/dic/doctrine_phpcr_admin" />
    
            <document-tree-default>locale</document-tree-default>
    
            <document-tree class="Doctrine\ODM\PHPCR\Document\Generic">
                <valid-child>all</valid-child>
            </document-tree>
    
            <document-tree class="Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent">
                <valid-child>Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\SimpleBlock</valid-child>
                <valid-child>Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ContainerBlock</valid-child>
                <valid-child>Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ReferenceBlock</valid-child>
                <valid-child>Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ActionBlock</valid-child>
            </document-tree>
    
            <document-tree class="Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ReferenceBlock" />
    
            <!-- ... -->
        </config>
    </container>
    
  • PHP
    // app/config/config.php
    $container->loadFromExtension('sonata_doctrine_phpcr_admin', array(
        'document_tree_defaults' => array('locale'),
        'document_tree' => array(
            'Doctrine\ODM\PHPCR\Document\Generic' => array(
                'valid_children' => array(
                    'all',
                ),
            ),
            'Symfony\Cmf\Bundle\ContentBundle\Doctrine\Phpcr\StaticContent' => array(
                'valid_children' => array(
                    'Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\SimpleBlock',
                    'Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ContainerBlock',
                    'Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ReferenceBlock',
                    'Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ActionBlock',
                ),
            ),
            'Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ReferenceBlock' => array(
                'valid_children' => array(),
            ),
            // ...
    ));
    

Tip

A real world example can be found in the cmf-sandbox configuration, the section document_tree in sonata_doctrine_phpcr_admin.

3.4. Rendering a Tree Directly

Instead of using the block, you can also use an action to render an admin tree. You can specify the tree root and the selected item, allowing you to have different type of content in your tree. For instance, you could show the tree of menu documents like this:

  • Twig
    {% render(controller(
        'sonata.admin.doctrine_phpcr.tree_controller:treeAction',
         {
            'root':     basePath ~ "/menu",
            'selected': menuNodeId,
            '_locale':  app.request.locale
        }
    )) %}
    
  • PHP
    <?php echo $view['actions']->render(new ControllerReference(
            'sonata.admin.doctrine_phpcr.tree_controller:treeAction',
            array(
                'root'     => $basePath . '/menu',
                'selected' => $menuNodeId,
                '_locale'  => $app->getRequest()->getLocale()
            ),
    )) ?>
    

Found a typo? Something is wrong in this documentation? Just fork and edit it!