Symfony2: 3d to 2d. Display tree navigation menu in a select dropdown in SonataAdmin

Keys: SonataAdmin, Gedmo Tree nested type, Select Dropdown, use EntityManager in configureFormFields Admin page

I've lost 2 days bumping my head on a simple task - I needed to display simple dropdown combo box that will display Nested Gedmo Tree in a Sonata Admin form.

I've installed and get working http://knpbundles.com/roomthirteen/Room13NavigationBundle - It's simple implementation of Gedmo\Tree type="nested" - exactly what I need for a simple menu navigation + it has ready Sonataadmin page to edit nodes.
The nice thing about this exact bundle is that it uses @Translatable, @Blameable, @Timestampable and this is all what I need - to be able to translate my menu, to see when and who updated the records.

After installing it I've noticed that 'path' is missing/empty event getting undefined notice.
I've dug around and found that I have to implement getpath() myself.
I did and created custom repository. I wasn't able to use childrenHierarchy directly in the Entity.


namespace Room13\NavigationBundle\Entity\Repository;
class NavigationNodeRepository extends \Gedmo\Tree\Entity\Repository\NestedTreeRepository{
function getFlatNodes($startNode = null, $options = null) {
if (is_null($options)) {
$options = array(
'decorate' => false,
'rootOpen' => '
    ',
    'rootClose' => '
',
'childOpen' => '
  • ',
    'childClose' => '
  • ',
    'nodeDecorator' => function($node) {
    return ''.$node['title'].'';
    }
    );
    }
    $htmlTree = $this->childrenHierarchy(
    $startNode, / starting from root nodes /
    false, / load all children, not only direct /
    $options
    );
    return $this->ToFlat($htmlTree, ' » ');
    }

    function ToFlat($node, $sep = ' > ', $path = '') {
    $els = array();
    foreach ($node as $id => $opts) {
    $els[$opts['id']] = $path . $opts['title'];
    if (isset($opts['__children']) && is_array($opts['__children']) && sizeof($opts['__children'])) {
    $r = $this->ToFlat($opts['__children'], $sep, ($path . $opts['title'] . $sep));
    foreach($r as $id => $title) {
    $els[$id] = $title;
    }
    }
    }
    return $els;
    }
    }


    After implementing it I've had to find a way so I can display result of this for root node in a flat select box in SonataAdminPage so user can select from a dropdown where the content should show.
    Well.. it turned out that entity type is impossible to be used because it can't call the method from CustomRepo, just the native Entity methods.
    I ended up using simple 'select' type like this:


    $em = $this->modelManager->getEntityManager('Room13NavigationBundle:NavigationNode');
    $tree = $em->getRepository('Room13NavigationBundle:NavigationNode')->getFlatNodes();
    $formMapper
    ->add('name')
    ->add('menu', 'choice', array(
    'label' => 'Place in menu',
    'empty_value' => 'Select menu',
    'choices' => $tree,
    )
    )
    ......
    ;

    Trackbacks

    Trackback specific URI for this entry

    This link is not meant to be clicked. It contains the trackback URI for this entry. You can use this URI to send ping- & trackbacks from your own blog to this entry. To copy the link, right click and select "Copy Shortcut" in Internet Explorer or "Copy Link Location" in Mozilla.

    No Trackbacks

    Comments

    Display comments as Linear | Threaded

    No comments

    Add Comment

    You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
    Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
    Standard emoticons like :-) and ;-) are converted to images.
    E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
    To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

    To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
    CAPTCHA