3

I'm trying to create a repository composer package to create a custom form field for Voyager, and I found this example: https://github.com/bnku/extended-bread-form-fields , but this it doesn't work for me.

So, how do I build a custom field form for Voyager? The result would be this:

Example

I tried this repository example.

https://github.com/bnku/extended-bread-form-fields (It didn't work for me)

and this is my repository test:

https://github.com/manuel90/crop-image-field

This is my composer.json of my package:

{
    "name": "manuel90/crop-image-field",
    "description": "New voyager form field to cut image when uploading",
    "authors": [
        {
            "name": "Manuel",
            "email": "testmlzra@gmail.com"
        }
    ],
    "require": {
        "tcg/voyager": "^1.1"
    },
    "autoload": {
        "psr-4": {
            "Manuel90\\CropImageField\\": "src/"
        }
    },
    "extra": {
        "laravel": {
            "providers": [
                "Manuel90\\CropImageField\\CropImageFieldServiceProvider"
            ]
        }
    }
}

I can see these lines there's a trouble, it didn't detect the class "Voyager", but I don't know how to fix it:


if( class_exists('Voyager') ) {
    Voyager::addFormField(CropImageFormField::class);
}

https://github.com/manuel90/crop-image-field/blob/master/src/CropImageFieldServiceProvider.php#L34-L36 ( According docs this is the way to add a custom form Docs here )

I expect to see in the BREAD edit section the new custom field listed on the input type option, like this:

enter image description here

TylerH
  • 20,799
  • 66
  • 75
  • 101
Manuel
  • 33
  • 1
  • 8

1 Answers1

2

You need to move the Voyager::addFormField call to the boot() method as this counts as a "piece of functionality" which should be called after the voyager service providers are properly registered.

This is missing from Voyager's documentation because they only document the use case for adding FormFields at app level where the call from the register method runs after all vendor Service Providers are registered.

jacmkno
  • 1,189
  • 11
  • 25