Error generating custom url that shows a phpcr document version in SonataAdmin

185 views Asked by At

So i'm trying to extend the SonataAdmin show view to have a list of the document's versions (phpcr checkpoint or checkin).

I've got the list of saved versions showing correctly, and now i need to make them into links that display that version of the content, but i'm getting the following error when trying to add a custom Route:

An exception has been thrown during the rendering of a template 
("Parameter "id" for route "admin_cmsbundle_product_show_version" must 
match "[^/]++" ("mstr/product/product-253562" given) to generate a 
corresponding URL.") in CmsBundle:product:show.html.twig at line 18.

This is my configureRoutes in my admin class:

protected function configureRoutes(RouteCollection $collection)
{
    $collection->add('show_version', $this->getRouterIdParameter() . '/show/{version}');
}

This is my overriden template:

{% block show %}

<ul>
    {% for version in versions %}
        <li><a href="{{  admin.generateObjectUrl('show_version', object, {'version': version.name}) }}">Version: {{ version.name }} </a></li>
    {% endfor %}
</ul>

{{ parent() }}    

{% endblock %}

This is my edited show action (to include the versions list):

public function showAction($id = null)
{ 
    ...
    return $this->render($this->admin->getTemplate('show'), array(
        'action'   => 'show',
        'object'   => $object,
        'elements' => $this->admin->getShow(),
        'versions' => $this->getVersionHistory($object)
    ));
}

And this is my showVersion action in the controller:

public function showVersionAction($id = null, $version = "1.0")
{ 
    ...
    return $this->render($this->admin->getTemplate('show'), array(
        'action'   => 'show',
        'object'   => $this->getVersion($object, $version),
        'elements' => $this->admin->getShow(),
        'versions' => $this->getVersionHistory($object)
    ));
}

Note, generateUrl gives the same error:

<a href="{{  admin.generateUrl('show_version', {'id': object.id, 'version': version.name}) }}">Version: {{ version.name }} </a>

What am i doing wrong?

Any help on fixing this would be greatly appreciated :)

2

There are 2 answers

0
dantelo On BEST ANSWER

Bit of digging around and the answer was simple, just had to override the pattern for id to be .+

protected function configureRoutes(RouteCollection $collection)
{
    $collection->add('show_version', $this->getRouterIdParameter() . '/show/{version}', array(), array('id' => '.+'));
}
2
Grzegorz Krauze On

According to the error message there is some problem with $object. So maybe use generateUrl instead of generateObjectUrl() and pass your id in array:

{% block show %}

<ul>
    {% for version in versions %}
        <li><a href="{{  admin.generateUrl('show_version', {'id':object.id,'version': version.name}) }}">Version: {{ version.name }} </a></li>
    {% endfor %}
</ul>

{{ parent() }}
{% endblock %}