Search code examples
phpsymfonyphp-5.3sonata-adminsonata

Using a custom view page for image preview in Sonata Admin in Symfony 2 project


I have the following admin system setup using Sonata Admin in my SF2 project. When I click "View Image" I want to show either a popup/overlay with the image, or if it's easier, a new page with the image. The route for this is configured as /admin/ayrshireminis/gallery/galleryimage/{id}/view_image

enter image description here

I have this method in my CRUDController which the codepath enters:

/**
 * preview the image
 *
 * @return RedirectResponse
 */
public function viewImageAction()
{
    // work out which image we are approving based on the ID in the URL
    $id = $this->get('request')->get($this->admin->getIdParameter());

    $object = $this->admin->getObject($id);

    // couldn't find the object
    if (!$object) {
        throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
    }

    return $this->render('SonataAdminBundle::empty_layout.html.twig', array('image' => $object));

}

How I can't find any Sonata documentation to work out howto simply display a blank page (within the Sonata Admin layout) with an image in it.


Solution

  • If you want to display a blank page with an image (within the Sonata Admin layout):

    // src/ACME/YourBundle/Resources/views/empty_layout.html.twig
    {% extends "SonataAdminBundle::standard_layout.html.twig" %}
    {% block sonata_page_content %}
    <img src="{{ image.src }}" />
    {% endblock %}
    

    And in your controller:

    return $this->render('ACMEYourBundle::empty_layout.html.twig', array('image' => $object));
    

    If you just want a blank page without the admin layout, same twig template but without extending sonata admin bundle's standard layout.