PHP code example of azaw / easyadmin-az-fields

1. Go to this page and download the library: Download azaw/easyadmin-az-fields library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

azaw / easyadmin-az-fields example snippets


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

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);
    }
}

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',
                'TINGS => new CropperSettingsDto(),
            CropType::OPTION_DATA_TRANSFORMER => $this->fileDataTransformer,
        ])->setAllowedTypes(
            CropType::OPTION_CROPPER_SETTINGS,
            [
                CropperSettingsDto::class
            ]
        )->setAllowedTypes(
            CropType::OPTION_DATA_TRANSFORMER,
            [
                DataTransformerInterface::class
            ]
        );
    }
}

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

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::new('content', "HTML content");