如何在sonata admin listmapper中添加图像的缩略图

时间:2016-11-08 16:44:06

标签: symfony sonata-admin symfony-sonata

我不知道如何在实体的admin类中的listmapper中预览小图像。我只看到图片编辑表单的链接,但我想看到图片的缩略图。

这是我的实体:

<?php

namespace ExampleBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;
use Application\Sonata\MediaBundle\Entity\Media;

/**
 * example
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class example
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

  /**
 * @var string
 * @var \Application\Sonata\MediaBundle\Entity\Media
 * @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"}, fetch="LAZY")
 */
private $imagenEs;

  /**
 * @var string
 * @var \Application\Sonata\MediaBundle\Entity\Media
 * @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"}, fetch="LAZY")
 */
private $imagenEn;


 /**
 * @var string
 * @Gedmo\Translatable
 * @ORM\Column(name="titulo", type="string", length=255)
 */
private $titulo;



 /**
 * @var string
 */
private $tituloEn;


 /**
 * @var string
 * @Gedmo\Translatable
 * @ORM\Column(name="texto", type="text")
 */
private $texto;

 /**
 * @var string
 */
private $textoEn;

    /**
 * @var boolean
 *
 * @ORM\Column(name="activo", type="boolean")
 */
private $activo;

/**
 * @var integer
 *
 * @ORM\Column(name="ranking", type="integer")
 */
private $ranking;


 /**
 * @var string
 * @Gedmo\Locale 
 */
private $locale;



/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}


/**
 * Set imagen
 *
 * @param string $imagen
 * @return BannerHome
 */
public function setImagenEs(Media $imagenEs)
{
    $this->imagenEs = $imagenEs;

    return $this;
}

/**
 * Get imagen
 *
 * @return string 
 */
public function getImagenEs()
{
    return $this->imagenEs;
}


 /**
 * Set imagen
 *
 * @param string $imagen
 * @return BannerHome
 */
public function setImagenEn(Media $imagenEn)
{
    $this->imagenEn = $imagenEn;

    return $this;
}

/**
 * Get imagen
 *
 * @return string 
 */
public function getImagenEn()
{
    return $this->imagenEn;
}

/**
 * Set titulo
 *
 * @param string $titulo
 * @return BannerHome
 */
public function setTitulo($titulo)
{
    $this->titulo = $titulo;

    return $this;
}

/**
 * Get titulo
 *
 * @return string 
 */
public function getTitulo()
{
    return $this->titulo;
}

/**
 * Set titulo
 *
 * @param string $titulo
 * @return BannerHome
 */
public function setTituloEn($tituloEn)
{
    $this->tituloEn = $tituloEn;

    return $this;
}

/**
 * Get titulo
 *
 * @return string 
 */
public function getTituloEn()
{
    return $this->tituloEn;
}

/**
 * Set texto
 *
 * @param string $texto
 * @return BannerHome
 */
public function setTexto($texto)
{
    $this->texto = $texto;

    return $this;
}

/**
 * Get texto
 *
 * @return string 
 */
public function getTexto()
{
    return $this->texto;
}



/**
 * Set texto
 *
 * @param string $texto
 * @return BannerHome
 */
public function setTextoEn($textoEn)
{
    $this->textoEn = $textoEn;

    return $this;
}

/**
 * Get texto
 *
 * @return string 
 */
public function getTextoEn()
{
    return $this->textoEn;
}

  /**
 * Set activoBoolean
 *
 * @param string $activoBoolean
 * @return BannerHome
 */
public function setActivo($activo)
{
    $this->activo = $activo;

    return $this;
}

/**
 * Get activoBoolean
 *
 * @return string 
 */
public function getActivo()
{
    return $this->activo;
}

/**
 * Set ranking
 *
 * @param integer $ranking
 * @return BannerHome
 */
public function setRanking($ranking)
{
    $this->ranking = $ranking;

    return $this;
}

/**
 * Get ranking
 *
 * @return integer 
 */
public function getRanking()
{
    return $this->ranking;
}

public function setTranslatableLocale($locale) { 


    $this->locale = $locale;


}

}?>

这是我的管理类

<?php

namespace ExampleBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;

class PibeAdmin extends Admin
{
/**
 * @param DatagridMapper $datagridMapper
 */
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('id')
        ->add('titulo')
        ->add('texto')
        ->add('activo')
        ->add('ranking')
    ;
}

/**
 * @param ListMapper $listMapper
 */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('id')
        ->add('titulo')
        ->add('imagenEs') /*With this I see a link to the image, and if I click this link I saw the edit form of the image, but I want a thumbnail*/
        ->add('activo')
        ->add('ranking')
        ->add('_action', 'actions', array(
            'actions' => array(
                'show' => array(),
                'edit' => array(),
                'delete' => array(),
            )
        ))
    ;
}

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $idioma=$this->getRequest()->getLocale();

    if($idioma=='es'){
            $formMapper         
                ->add('imagenEs', 'sonata_type_model_list', array(
                    'required' => false,
                    'label'=>'Imagen Español'

                     ), array(                       
                    'link_parameters' => array(
                    'context' => 'default',
                    'provider' => 'sonata.media.provider.image'

                        )
                    )
                )
              ->add('imagenEn', 'sonata_type_model_list', array(
                    'required' => false,
                    'label'=>'Imagen Ingles',

                     ), array(
                    'link_parameters' => array(
                    'context' => 'default',
                    'provider' => 'sonata.media.provider.image'

                        )
                    )
                )
              ->add('titulo',null,array('label'=>'Titulo Español'))
              ->add('tituloEn','text',array('label'=>'Titulo Ingles','data'=>''))
              ->add('texto','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Texto Español'))
              ->add('textoEn','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Texto Ingles','data'=>''))

                ;
        }else{
            $formMapper 
                ->add('imagenEn', 'sonata_type_model_list', array(
                    'required' => false,
                    'label'=>'Imagen Ingles'

                     ), array(
                    'link_parameters' => array(
                    'context' => 'default',
                    'provider' => 'sonata.media.provider.image'

                        )
                    )
                )           
                ->add('imagenEs', 'sonata_type_model_list', array(
                    'required' => false,
                    'label'=>'Imagen Español'

                     ), array(
                    'link_parameters' => array(
                    'context' => 'default',
                    'provider' => 'sonata.media.provider.image'

                        )
                    )
                )
                ->add('tituloEn','text',array('label'=>'Titulo Ingles','data'=>''))
                ->add('titulo',null,array('label'=>'Titulo Español'))
                ->add('textoEn','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Texto Ingles','data'=>''))
                ->add('texto','textarea', array('attr' => array('class' => 'ckeditor'),'label'=>'Texto Español'))



                ;
    }
    $formMapper
        ->add('activo')
        ->add('ranking')
    ;



    $container = $this->getConfigurationPool()->getContainer();
    $em = $container->get('doctrine.orm.entity_manager');

    if($idioma=='es' && $this->getSubject()->getId()!=null){    


        //$titoriginal=$em->getRepository('HomeBundle:ImagenesHome')->find($this->getSubject()->getId());          
        // $titoriginal->setTranslatableLocale('en');
        //$em->refresh($titoriginal);


       //buscamos su traduccion en la tabal de traducciones.
       $repository = $em->getRepository('Gedmo\Translatable\Entity\Translation');
       $translations = $repository->findTranslations($this->getSubject()); 
       //rellenamos el campo con la traduccion.
       if(count($translations)>0){


           $formMapper->get('tituloEn')->setData($translations['en']['titulo']);
           $formMapper->get('textoEn')->setData($translations['en']['texto']);


       };


    }else if($idioma=='en'  && $this->getSubject()->getId()!=null){

       $titoriginal=$em->getRepository('PibeBundle:Pibe')->find($this->getSubject()->getId());
       //cambiamos el locale a 'es' para coger el texto en español
       $titoriginal->setTranslatableLocale('en');
       $em->refresh($titoriginal);
       //una vez que lo cogemos, volvemos a cambiar el locale a 'en'
       $formMapper->get('tituloEn')->setData($titoriginal->getTitulo());
       $formMapper->get('textoEn')->setData($titoriginal->getTexto());
       $titoriginal->setTranslatableLocale('es');
       $em->refresh($titoriginal);

    }





}

/**
 * @param ShowMapper $showMapper
 */
protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->add('id')
        ->add('titulo')
        ->add('texto')
        ->add('activo')
        ->add('ranking')
    ;
}


public function prePersist($object) {


        $container = $this->getConfigurationPool()->getContainer();
        $entityManager = $container->get('doctrine.orm.entity_manager');


        //insertar en español.
        $object->setTitulo($object->getTitulo());
        $object->setTexto($object->getTexto());
        $object->setTranslatableLocale('es');
        $entityManager->persist($object);
        $entityManager->flush();

        //insertar en ingles.
        $object->setTitulo($object->getTituloEn());
        $object->setTexto($object->getTextoEn());
        $object->setTranslatableLocale('en');
        $entityManager->persist($object);
        $entityManager->flush();


}

public function preUpdate($object) {

        $container = $this->getConfigurationPool()->getContainer();
        $entityManager = $container->get('doctrine.orm.entity_manager');


        //insertar en español.
        $object->setTitulo($object->getTitulo());
        $object->setTexto($object->getTexto());
        $object->setTranslatableLocale('es');
        $entityManager->persist($object);
        $entityManager->flush();

        //insertar en ingles.
        $object->setTitulo($object->getTituloEn());
        $object->setTexto($object->getTextoEn());
        $object->setTranslatableLocale('en');
        $entityManager->persist($object);
        $entityManager->flush();



 }


}

enter image description here

如果它说“Imagen Es”,我想要图像的缩略图。

2 个答案:

答案 0 :(得分:1)

$listMapper
->add('thumb', null, array(
    'template' => "ExampleBundle:Pibe:list_thumb.html.twig")
)

list_thumb.html.twig

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field %}
    {# {{ dump() }} you can dump all variables here #}
    {# {{ value }} will return the value of getImagenEs() #}
    <img class="img-responsive" alt="{{ object.name }}" src="{{ asset('path/to/uploadfolder/' ~ object.filepath) }}" /> {# or you can use a twig filter with your own logic #}
{% endblock field %}

答案 1 :(得分:0)

我相信这是一个更为明确的2日期答案:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('title')
        ->add('image', 'string', ['template' => 'SonataMediaBundle:MediaAdmin:list_image.html.twig'])
        ->add('updatedAt')
        ->add('_action', null, ['actions' =>
            [
                'edit' => [],
                'delete' => []
            ], 'label' => 'Actions']);
}