Jump to content

Image field creation via API requiring field to be saved before use


hellomoto
 Share

Recommended Posts

I have a module that I am using to create an image field like so:

  $mf[] = [[
    'tags' => $tag,
    'name' => $pre.'logo',
    'type' => wire('modules')->get('FieldtypeImage'),
   ],[
    'label' => 'Logo',
    'columnWidth' => 50,
    'inputfieldClass' => 'InputfieldImage',
  ]];

Yet upon its creation, initially, when I try creating a page of a template including it, it says that the field must first be configured. Once I go and save the field as-is, everything's honkey dory. It's just an annoyance, with every image field. I figured it would be the required inputfieldClass field field, but as you can see, that's included up in there, and it's still not good to go off the bat.

Link to comment
Share on other sites

@adrian Its only the extensions. maxFiles and outputFormat is set to 0 ((int) NULL) by default.

@hellomoto  No need to set inputfieldClass. The following is enough to get it work.

$f = new Field;
$f->name = 'myfield';
$f->type = 'FieldtypeImage';
$f->extensions = 'gif jpg jpeg png';
$f->save();

 

  • Like 2
Link to comment
Share on other sites

  • 1 year later...
6 hours ago, Laikmosh said:

and how can i set the formatted value of an image file created via API (auto, single, multi, string)?

Go to the field edit page in the PW admin and then in TracyDebugger open the RequestInfo panel. This shows you all the properties currently set for a field. Just use these in the API as needed.

image.thumb.png.18ec512c8a686c1e56a2797cb426bff2.png

  • Like 2
Link to comment
Share on other sites

You could also use the Field Code section (created by @bernhard) which lets you copy / paste the API code need to create the field.

Be sure to grab the very latest version of Tracy because I just committed a fix to this feature. It was previously missing certain properties.

image.png.d215916404eba1086b4f911efaeac6c1.png

 

Note that this option is not enabled in Tracy by default so you need to go to the module settings and enable:

image.png.2bed3151a4b9fac15638dbddd82718c0.png

 

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...