PHP code example of tourze / easy-admin-file-size-field-bundle

1. Go to this page and download the library: Download tourze/easy-admin-file-size-field-bundle 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/ */

    

tourze / easy-admin-file-size-field-bundle example snippets




namespace App\Controller\Admin;

use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Tourze\EasyAdminFileSizeFieldBundle\Field\FileSizeField;
use App\Entity\File;

class FileCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return File::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id'),
            TextField::new('name'),
            FileSizeField::new('size', 'File Size'),
        ];
    }
}

// Basic usage
FileSizeField::new('fileSize')

// With custom label
FileSizeField::new('fileSize', 'Document Size')

// With additional options
FileSizeField::new('fileSize', 'Size')
    ->setHelp('The size of the uploaded file')
    ->hideOnIndex()

FileSizeField::new('fileSize')
    ->setTemplatePath('admin/custom_file_size.html.twig')
    ->addCssClass('file-size-custom')
    ->setFormTypeOptions([
        'precision' => 2,
        'binary' => true, // Use 1024 instead of 1000 as base
    ])

use Tourze\EasyAdminFileSizeFieldBundle\Form\FileSizeType;

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        ->add('maxFileSize', FileSizeType::class, [
            'label' => 'Maximum File Size',
        ])
    ;
}