Jump to content

How to allow image upload in configurable module form?


JayGee
 Share

Recommended Posts

I'm teaching myself about PW module development by creating a simple module to hold frequently used settings for web design projects. I' ve set up a module with a config file to define a form with my settings fields. @ryan's tutorial here https://processwire.com/blog/posts/new-module-configuration-options/ says you can use "any Inputfield module name" - but image field gives me an error is an image field possible?

"Error: Uncaught Error: Call to a member function path() on string in /home/testproject/public_html/wire/modules/Inputfield/InputfieldFile/InputfieldFile.module:913"

Example array so far below - am I missing some confiuration values?

array(
	'name' => 'logo',
	'label' => 'Logo',
	'type' => 'file',
	'value' => '',
	'maxFiles' => 1,
),
Link to comment
Share on other sites

Module config settings are stored as a JSON string in a single field in the database. Module config fields don't store data in separate tables as per the fieldtypes you might be used to from using fields in a template.

So certain core inputfields that need special support from a fieldtype to store their data can't be used in module configs: Files, Images, Repeater, PageTable, maybe a few more.

BTW, personally I would use a standard page/template to store site settings rather than a module config for just this reason.

  • Like 1
Link to comment
Share on other sites

9 minutes ago, Robin S said:

Module config settings are stored as a JSON string in a single field in the database. Module config fields don't store data in separate tables as per the fieldtypes you might be used to from using fields in a template.

So certain core inputfields that need special support from a fieldtype to store their data can't be used in module configs: Files, Images, Repeater, PageTable, maybe a few more.

BTW, personally I would use a standard page/template to store site settings rather than a module config for just this reason.

Ok thanks that makes sense why it doesn’t work.

I do normally store this stuff in a standard page, but was using it as an exercise to dip my toe into modules as haven’t touched that element of PW yet. I just thought it might be cool to be able to install the module into each project without having to set up the fields etc.

Link to comment
Share on other sites

22 minutes ago, Guy Incognito said:

I just thought it might be cool to be able to install the module into each project without having to set up the fields etc.

Everyone has their own strategies for this sort of thing. Some people like to use modules for site settings and just deal with the limitations around inputfield types. There are a couple of existing settings modules you could study:
https://modules.processwire.com/modules/process-general-settings/
https://modules.processwire.com/modules/settings-factory/

Personally I have a custom site profile (generated via Site Profile Exporter) that I use as the starting point for every new project, that has all the fields/templates/pages/modules that I tend to use in every project.

Link to comment
Share on other sites

Yes to date I’ve been using a combo of pages and my own include files within the templates folder which works ok, just experimenting with the options ?

My only hang up with profiles is how do you update them once installed. Whereas a module is easily updated across multiple sites.

If I’m right, you can use a module to add pages and fields? If so maybe I need to switch my strategy for the module and do this instead of config fields.

Link to comment
Share on other sites

3 hours ago, Guy Incognito said:

My only hang up with profiles is how do you update them once installed. Whereas a module is easily updated across multiple sites.

Exactly. That's one of the reasons why I don't like site profiles and created ProcessWire Kickstart. It's not finished at all, but it works for me and it's just a showcase of a different and more flexible approach.

3 hours ago, Guy Incognito said:

If I’m right, you can use a module to add pages and fields? If so maybe I need to switch my strategy for the module and do this instead of config fields.

Yes, you're right, and that would have been my suggestion for you to try. Module development is great and a lot of fun. From what I've read in your posts (this one was awesome btw) I guess you'll also like it a lot ? Creating pages, templates and fields can be a little tedious though. There is the Migrations Module that has some helpers. I'd love to have a nicer and cleaner API though for such tasks, also handling naming collisions etc., eg

  • createField, createTemplate, addFieldToTemplate, removeFieldFromTemplate, editInContext, etc, etc

Maybe you want to take that idea as inspiration and start something like this while playing around with pw module development? ? 

 

  • Like 1
Link to comment
Share on other sites

2 hours ago, bernhard said:

From what I've read in your posts (this one was awesome btw)

Thanks ? PW is also helping my coding get better - I'm mainly frontend, but it makes a great framework to safely grow my skillset and also have to rely on other members of my team less ? 

I'll check out Kickstart - looks awesome.

2 hours ago, bernhard said:

Module development is great and a lot of fun

Yes - I've loved the template and dev work created so far with PW so having fun so far just experimenting to see what's possible with modules too.

Link to comment
Share on other sites

Thanks to everyone who was helping me the other night. A little update on what I ended up doing with my helper module so far....

For the site settings, I ended up just making a configurable module with only one field containing a load of defaults stored as a simple JSON array. I thought this would be far easier for my specific purpose as it means that it's easily customisable with new data on a site-by-site basis without having to update the module interface or add new fields.

I've hooked the $page object to access this data on the front end, so I can now do <?=$page->siteSettings['company_name'];?> etc in my templates which is cool.

I've also set some dependencies in the config for the modules we regularly use.

image.thumb.png.5f3cc9f30ad8ca8cc5efbca417a20cef.png

I have also used a hook to inject an overlay 'edit mode' icon that displays on the front end whenever an editor is logged into the site, as we've found some people don't realise when they're logged in and can use the front-end editing on their site. The little cog icon also links back to the PW dashboard.

image.png.492e08809caa62094dfeacae29df4a60.png

Really starting to get the hang of how the PW modules work now, so will be adding in lots more useful stuff we tend to use for every project in due course. ? 

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...