定制 azaw/easyadmin-az-fields 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

azaw/easyadmin-az-fields

最新稳定版本:v1.4.2

Composer 安装命令:

composer require azaw/easyadmin-az-fields

包简介

Custom EasyAdmin fields by AZ

README 文档

README

Available fields

CropField

Used for cropping images. If not image file provided, it will be left alone but still uploadable.

Field usage in CRUD

CropField::new('backgroundImage', "")
    ->setRequired(false)
    ->setFormType(FileForm::class)
    ->setFormTypeOption(
        CropType::OPTION_CROPPER_SETTINGS,
        new CropperSettingsDto()
            ->setAspectRatio(16 / 10)
    ),

Example CropDataTransformer:

namespace App\DataTransformer;

use App\Entity\File;
use App\Interfaces\FileManagerInterface;
use EasyAdminAzFields\Contracts\CropDataTransformerInterface;
use EasyAdminAzFields\Dto\CropperValueDto;
use RuntimeException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class FileDataTransformer implements CropDataTransformerInterface
{
    public function __construct(
        private readonly FileManagerInterface $fileManager
    )
    {
    }

    public function transform(mixed $value): CropperValueDto
    {
        $dto = new CropperValueDto();

        if (!$value instanceof File) {
            return $dto->setOldImage($value);
        }

        return $dto
            ->setOldImage($value->getUrl());
    }

    public function reverseTransform(mixed $value): ?string
    {
        if (!$value instanceof CropperValueDto) {
            return null;
        }

        if (!$value->getImage()) {
            return $value->getOldImage();
        }

        $tempPath = $value->getImage();
        $uploaded = new UploadedFile($tempPath, 'tempfile', null, null, true);
        $extension = $uploaded->guessExtension();
        // Your file name
        $randomFileName = "random" . rand();

        // Full path where you want to upload the file
        $uploadPath = "/{$randomFileName}.{$extension}";

        // Saves file
        $uploaded = $this->fileManager->save($uploadPath, $uploaded->getContent());
        if (!$uploaded) {
            throw new RuntimeException("Failed to upload the file");
        }

        // The image path which will be passed to entity
        // You can return whatever you want it will be set into your entity
        return $this->fileManager->getFullPath($uploadPath);
    }
}

Need more help how to use it?

Need more data than just single field? Enjoy my form for entity "File".
The entity has 3 fields: name, alt and url.

namespace App\Form;

use App\DataTransformer\FileDataTransformer;
use App\Entity\File;
use Doctrine\ORM\EntityManagerInterface;
use EasyAdminAzFields\Dto\CropperSettingsDto;
use EasyAdminAzFields\Form\CropType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolv

class FileForm extends AbstractType
{
    public function __construct(
        private readonly EntityManagerInterface $em,
        private readonly FileDataTransformer    $fileDataTransformer,
    )
    {
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Name',
            ])
            ->add('alt', TextType::class, [
                'label' => 'Alt',
                'required' => false,
            ])
            ->add('url', CropType::class, [
                'label' => 'File',
                CropType::OPTION_DATA_TRANSFORMER => $options[CropType::OPTION_DATA_TRANSFORMER],
                CropType::OPTION_CROPPER_SETTINGS => $options[CropType::OPTION_CROPPER_SETTINGS],
            ])->addEventListener(
                FormEvents::SUBMIT,
                function (FormEvent $event) {
                    $data = $event->getData();

                    if (!$data instanceof File) {
                        return;
                    }

                    if ($data->getUrl()) {
                        return;
                    }

                    $event->setData(null);
                    $this->em->detach($data);
                }
            );
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'class' => File::class,
            'data_class' => File::class,
            'query_builder' => null,
            CropType::OPTION_CROPPER_SETTINGS => new CropperSettingsDto(),
            CropType::OPTION_DATA_TRANSFORMER => $this->fileDataTransformer,
        ])->setAllowedTypes(
            CropType::OPTION_CROPPER_SETTINGS,
            [
                CropperSettingsDto::class
            ]
        )->setAllowedTypes(
            CropType::OPTION_DATA_TRANSFORMER,
            [
                DataTransformerInterface::class
            ]
        );
    }
}

CoordinatesField

Used for coordinates inside your entity. Values are under {fieldName}_latitude and {fieldName}_longitude.

Field usage in CRUD

CoordinatesField::new('coordinates', "Geo location");

Add coordinates to your entity (YourEntity)

use EasyAdminAzFields\Entity\Coordinates;

#[ORM\Entity(repositoryClass: YourEntityRepository::class)]
class YourEntity
{    
    #[ORM\Embedded(class: Coordinates::class)]
    private ?Coordinates $coordinates;

    public function __construct()
    {
        $this->coordinates = new Coordinates();
    }

    public function getCoordinates(): ?Coordinates
    {
        return $this->coordinates;
    }

    public function setCoordinates(?Coordinates $coordinates): static
    {
        $this->coordinates = $coordinates;

        return $this;
    }
}

RichEditorField

Used as an html editor. TinyMCE in version 5.1.1 is used.
By using this package you automatically selfhost TinyMCE.

Field usage in CRUD

RichEditorField::new('content', "HTML content");

统计信息

  • 总下载量: 127
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固