简体   繁体   English

显示图像预览 Sonata Admin Bundle 无法加载类型“文件”

[英]Showing image previews Sonata Admin Bundle Could not load type "file"

I'm trying to show preview image with Sonata Admin Bundle 3 version but I can't do it.我正在尝试使用 Sonata Admin Bundle 3 版本显示预览图像,但我做不到。 I get this error in RecipeAdmin.php: Could not load type "file": class does not exist.我在 RecipeAdmin.php 中收到此错误:无法加载类型“文件”:class 不存在。

RecipeAdmin.php RecipeAdmin.php

<?php

declare(strict_types=1);

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\Form\Type\CollectionType;
use Sonata\AdminBundle\Form\Type\ModelListType;



final class RecipeAdmin extends AbstractAdmin
{

    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        $datagridMapper
        ->add('title',null,['label' =>'Título'])
        ->add('image',null,['label' =>'Imagen'])
        ->add('description',null,['label' =>'Descripción'])
        ->add('score',null,['label' =>'Puntuación'])
        ->add('visible')
            ;
    }

    protected function configureListFields(ListMapper $listMapper): void
    {
        $listMapper
            ->add('id')
            ->add('user', CollectionType::class,['label' =>'Usuario'])
            ->add('title',null,['label' =>'Título'])
            ->add('image',null,['label' =>'Imagen'])
            ->add('description',null,['label' =>'Descripción'])
            ->add('score',null,['label' =>'Puntuación'])
            ->add('visible',null,['label' =>'Visible'])
            ->add('_action', null, [
                'label' => 'Acciones',
                'actions' => [
                    'show' => [],
                    'edit' => [],
                    'delete' => [],
                ],
            ]);
    }

    protected function configureFormFields(FormMapper $formMapper): void
    {
        

            $image = $this->getSubject();

            // use $fileFormOptions so we can add other options to the field
            $fileFormOptions = ['required' => false];
            if ($image && ($webPath = $image->getImage())) {
                // get the request so the full path to the image can be set
                $request = $this->getRequest();
                $fullPath = $request->getBasePath().'/'.$webPath;
    
                // add a 'help' option containing the preview's img tag
                $fileFormOptions['help'] = '<img src="'.$fullPath.'" class="admin-preview"/>';
                $fileFormOptions['help_html'] = true;
            }

            $formMapper
            ->add('title',null,['label' =>'Título'])
            ->add('image', 'file', $fileFormOptions)
            ->add('description',null,['label' =>'Descripción'])
            ->add('score',null,['label' =>'Puntuación'])
            ->add('visible')
            ;
    }

    protected function configureShowFields(ShowMapper $showMapper): void
    {
        $showMapper
            ->add('title',null,['label' =>'Título'])
            ->add('image',null,['label' =>'Imagen'])
            ->add('description',null,['label' =>'Descripción'])
            ->add('user', CollectionType::class)
            ->add('score',null,['label' =>'Puntuaciones'])
            ->add('visible')
            ;
    }
}

Recipe.php Entity配方.php 实体

<?php

namespace App\Entity;

use App\Repository\RecipeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=RecipeRepository::class)
 */
class Recipe
{
    public function __construct() {
        $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $image;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $description;

    /** 
     * @ORM\ManyToOne(targetEntity="User", inversedBy="recipes")
     * @ORM\JoinColumn(nullable=false)
    */
    private $user;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $score;

    /**
     * @ORM\Column(type="boolean")
     */
    private $visible;

    /**
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="recipe", cascade={"persist"})
     * @ORM\JoinTable(name="recipes_categories")
     */
    private $categories;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function setId(int $id)
    {
        $this->id = $id;

        return $this;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getImage(): ?string
    {
        return $this->image;
    }

    public function setImage(string $image): self
    {
        $this->image = $image;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    public function getScore(): ?int
    {
        return $this->score;
    }

    public function setScore(?int $score): self
    {
        $this->score = $score;

        return $this;
    }

    public function getVisible(): ?bool
    {
        return $this->visible;
    }

    public function setVisible(bool $visible): self
    {
        $this->visible = $visible;

        return $this;
    }

    public function getCategories()
    {
        return $this->categories;
    }

    public function setCategories($categories)
    {
        $this->categories = $categories;

        return $this;
    }

    

    public function __toString()
    {
        return $this->getTitle();
    }

    public function addCategory(Category $category): self
    {
        if (!$this->categories->contains($category)) {
            $this->categories[] = $category;
        }

        return $this;
    }

    public function removeCategory(Category $category): self
    {
        $this->categories->removeElement($category);

        return $this;
    }
}

This is the link about how do it: https://symfony.com/doc/current/bundles/SonataAdminBundle/cookbook/recipe_image_previews.html这是有关如何操作的链接: https://symfony.com/doc/current/bundles/SonataAdminBundle/cookbook/recipe_image_previews.html

https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_image_previews.html#showing-image-previews https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_image_previews.html#showing-image-previews

In the documentation explains that I have to use 'file' type fields but when I use it in my proyect doesn't runs.在文档中解释说我必须使用“文件”类型字段但是当我在我的项目中使用它时不会运行。

This is an error in the doc, instead of file you should use FileType::class and adding:这是文档中的错误,而不是file ,您应该使用FileType::class并添加:

use Symfony\Component\Form\Extension\Core\Type\FileType;
    

$formMapper
    ->add('title',null,['label' =>'Título'])
    ->add('image', FileType::class, $fileFormOptions)

You will still have error such as:您仍然会遇到错误,例如:

The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File.

In the cookbook they tell you to create an Image Entity so you should add it and follow all the steps: https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_file_uploads.html在食谱中,他们告诉您创建一个图像实体,因此您应该添加它并按照所有步骤操作: https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_file_uploads.html

I suggest instead of following this cookbook, you should install and use the sonata media , the integration is easier and it have some nice features such as making different formats for your upload.我建议您不要遵循这本食谱,而应该安装和使用奏鸣曲媒体,集成更容易,并且它具有一些不错的功能,例如为您的上传制作不同的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CKEditor未与Sonata Formatter一起显示(Sonata Admin Bundle) - CKEditor not showing with Sonata Formatter (Sonata Admin Bundle) 无法使用Sonata管理项目和Sonata翻译包确定Symfony 3.2。*中属性“翻译”的访问类型 - Could not determine access type for property “translations” in Symfony 3.2.* with Sonata admin project and Sonata translation bundle 扩展具有更多表单字段的Sonata用户捆绑包,获取无法加载类型“ Application \\ Sonata \\ UserBundle \\ Form \\ RegistrationType” - extending sonata user bundle with more form fields, get Could not load type “Application\Sonata\UserBundle\Form\RegistrationType” Sonata管理员捆绑包:无法删除与sonata_type_admin的关系 - Sonata admin bundle : unable to remove relation with sonata_type_admin Symfony 2 Sonata Media捆绑包:保存媒体文件图像而无需Sonata管理员 - Symfony 2 Sonata Media bundle: saving media file image without sonata admin 在Sonata管理员捆绑包中创建新的字段类型 - Create new field type in sonata admin bundle Sonata Admin捆绑类型集合定制 - Sonata Admin Bundle Type Collection Customisation 在Sonata Admin Bundle中处理多个文件上传 - Handling multiple file uploads in Sonata Admin Bundle 使用Sonata-Admin捆绑包上传文件时出错 - Error in file upload with sonata-admin bundle Sonata Admin Bundle翻译 - Sonata Admin Bundle translation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM