creode/laravel-nova-assets

Takes the existing Laravel Assets module and adds functionality to make it work in Nova.

2.6.0 2024-11-08 09:12 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Takes the existing Laravel Assets module and adds hookable functionality to make it work in Nova.

Installation

You can install the package via composer:

composer require creode/laravel-nova-assets

You can publish the config file with:

php artisan vendor:publish --tag="nova-assets-config"

This is the contents of the published config file:

// config for Creode/LaravelNovaAssets
return [
    /*
    |--------------------------------------------------------------------------
    | Default Upload Accepted Mime Types
    |--------------------------------------------------------------------------
    |
    | This value contains the default bulk fields which will be rendered when
    | doing a bulk upload. You can add or remove fields as you wish.
    |
    */

    'default_upload_accepted_mime_types' => [
        'image/*',
        'application/zip',
        'zip',
        'application/pdf',
    ],

    /*
    |--------------------------------------------------------------------------
    | Allowed Zip File Extensions
    |--------------------------------------------------------------------------
    |
    | This value contains an array of allowed file extensions which will be
    | pulled when extracting zip files during a bulk upload action.
    |
    */

    'accepted_zip_file_extensions' => [
        'png',
        'jpeg',
        'jpg',
        'webp',
        'pdf',
    ],

    /*
    |--------------------------------------------------------------------------
    | Asset Policy
    |--------------------------------------------------------------------------
    |
    | This value contains the policy which will be used when verifying if a
    | user can perform an action on an asset. You can change this to your own
    | policy if you wish.
    |
    */

    'asset_policy' => \Creode\LaravelNovaAssets\Policies\AssetPolicy::class,

    /*
    |--------------------------------------------------------------------------
    | Image Driver
    |--------------------------------------------------------------------------
    |
    | This value contains the image driver which will be used when generating
    | thumbnails. You can change this to your own driver if you wish. Drivers
    | must implement the Intervention\Image\Interfaces\DriverInterface
    | interface.
    |
    | This can be one of the following:
    | Intervention\Image\Drivers\Gd\Driver::class
    | Intervention\Image\Drivers\Imagick\Driver::class
    |
    */
    'image_driver' => Intervention\Image\Drivers\Gd\Driver::class,

    /*
    |--------------------------------------------------------------------------
    | Traffic Cop
    |--------------------------------------------------------------------------
    |
    | Indicates whether Nova should check for modifications between viewing
    | and updating a resource.
    |
    */
    'traffic_cop' => true,

    /*
    |--------------------------------------------------------------------------
    | Show Max Upload Size
    |--------------------------------------------------------------------------
    |
    | Indicates whether to show the max upload size in the asset resource.
    |
    */
    'show_max_upload_size' => false,
];

Usage

The purpose of this module is to use the existing assets model class and wrap it into a Nova resource. This allows us to use the existing functionality of the assets module in Nova. We use some hookable functionality which is documented below that gives any child modules the ability to add custom fields and actions to the resource.

Registering Custom Resource Fields

You can register custom resource fields to appear in both the standard Nova pages (detail, edit, create) and the Bulk Asset Upload action. To do this you need to listen for the DefineAssetFieldsEvent event and add your fields to the $fields array. For a full list of Nova fields please look at the Nova documentation.

Event::listen(function (DefineAssetFieldsEvent $event) {
    $event->fields[] = Text::make('Folder', 'folder_id');
});

Registering Custom Resource Actions

You can register custom resource actions to appear on the Asset resource. To do this you need to listen for the DefineAssetActionsEvent event and add your actions to the $actions array. These just use Nova's standard Actions functionality. For details about defining actions please look at the Nova documentation.

Event::listen(function (DefineAssetActionsEvent $event) {
    $event->actions[] = TestActionClass::make();
});

Registering Custom Bulk Asset Fields

You can register custom bulk asset fields to appear on the Bulk Asset Upload action. To do this you need to listen for the DefineBulkAssetFieldsEvent event and add your fields to the $fields array. For a full list of Nova fields please look at the Nova documentation.

Event::listen(function (DefineBulkAssetFieldsEvent $event) {
    $event->fields[] = Text::make('Folder', 'folder_id');
});

Insert a field after another one

As of version 1.4.0 of this module, you can now run a helper function to add an attribute in after another one. This can be done by running the following:

Event::listen(function (DefineAssetFieldsEvent $event) {
    $event->addFieldAfter('name', Text::make('Folder', 'folder_id'));
});

This will then inject the field directly after the other. This is useful if you want to add a field after a specific field but don't want to have to worry about the order of the fields in the array. The only caveat here is that if the field with the provided first attribute doesn't exist, it will not add the field.

Permissions

This module exposes a new permission seeder class which will need to be published to your application in order to grant permissions to the new resource. To do this you need to run the following command:

php artisan vendor:publish --tag="nova-assets-seeders"

This will create a new AssetRoleAndPermissionSeeder.php file within your database/seeders directory. This will need to be run in order to grant permissions to the new resource. You can run this by running the following command:

php artisan db:seed --class=AssetRoleAndPermissionSeeder

You should now see in your database a collection of permissions and a new role called asset-manager. This role will have all the permissions required to manage assets. Before running this, it requires the setup of any tables for the spatie/laravel-permissions package. Please see the documentation for more information.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.