Jump to content

Where do I store arbitrary data like global info or settings?


Vigilante
 Share

Recommended Posts

I have a feeling I know the answer, but maybe there is a simpler way.

Basically, in order to avoid saving any hardcoded info in a template file, I'd rather store this data in some kind of global data set or 'settings' and then pull the value into templates as needed.

A simple example would be a company phone number, or an administrative email address. Rather than put these things hardcoded into the template that needs them, I'd rather call them up as some kind of global data as needed. And then of course this would make it easy to edit this data in the backend.

My guess is that you would have me create a special "page" using a custom template with a whole bunch of fields for every kind of custom data I need. I guess this would be fine, but it would require creating lots of fields which I think would clutter the fields area up.

The objective is just to have some place in the admin where a bunch of random things could be saved which may be used from any number of templates. And is easy to edit by the client.

Link to comment
Share on other sites

@Vigilante A config page with simple textfields for storing settings might be the best way to go. Another option would be your config.php . But in your case this is rather inappropriate as it might be hard to edit for your client. By the way instead of creating several fields in your config page you could also create one text field and put a json object  in it which you could access and parse when needed. (provided the customer can handle json.)

Link to comment
Share on other sites

@Vigilante That's exactly where (the often overlooked) Pro field "Functional Fields" shines. It does exactly what you are looking for.

https://processwire.com/blog/posts/functional-fields/

Or you could use the Textareas field: https://processwire.com/store/pro-fields/textareas/
The big advantage is that no matter how many fields you are using, behind the scenes it always just counts as one single field.

  • Like 1
Link to comment
Share on other sites

... and also in addition to a json object as I suggested above you could also simply use a plain textfield and store your ini there like:

[names]
me = "Hello"
you = "World"

[urls]
first = "http://www.example.com"

in your template you could access it as array with: 

print_r(parse_ini_string($page->ini))
Array ( [me] => Hello [you] => World [first] => http://www.example.com )

 

Link to comment
Share on other sites

9 hours ago, dragan said:

That's exactly where (the often overlooked) Pro field "Functional Fields" shines. It does exactly what you are looking for.

I usually do a combination of both - separate 'Business Info' page that stores stuff like logo versions, etc and a functional field that stores common text phrases. All editable by admin on that page and (I believe as don't use it), editable for multi-lang sites. Even go so far sometimes to make the page a link in the admin main menu, with or without a custom module

Link to comment
Share on other sites

20 hours ago, gmclelland said:

Haven't used it yet, but I think I read that Mystique is also a good module for this kind of stuff.  

 

I have also used SettingsFactory and can say that it works great.

Yes, it's possible to do these kind of stuff. Create 1 Mystique field and do what you want with that field.

Mystique config files

130130474_EkranResmi2020-01-0317_15_34.png.10ca8007b9f64f85d7144abaddc58990.png

Mystique seo configs

297584177_EkranResmi2020-01-0317_18_03.thumb.png.178f798f51deafb7363f9f763c879e42.png

Mystique admin edit for seo configs

204400863_EkranResmi2020-01-0317_17_31.thumb.png.bc125c84ac7e68f00d445aa9094e2e96.png

  • Like 2
Link to comment
Share on other sites

On 1/3/2020 at 2:53 PM, Pixrael said:

This one is nice, but also per-page scoped, so not really global. Also at one point of the lifecycle do you actually set those variables? Are they persistent? I assume we're not setting the all variables in the same template they are used, on every page load, so is there some other administrative area where you can set the data once to use?

 

On 1/1/2020 at 3:35 PM, dragan said:

That's exactly where (the often overlooked) Pro field "Functional Fields" shines. It does exactly what you are looking for.

https://processwire.com/blog/posts/functional-fields/

This is also nice, but not in the current release yet? I don't have these available in my version.

Also, it seems to focus on template-specific fields, rather than globally available data. Although I assume maybe you can access this data of one page from another page. Even still that's a bit clunky, trying to remember which page has particular fields you need, etc.

 

On 1/1/2020 at 3:35 PM, dragan said:

Or you could use the Textareas field: https://processwire.com/store/pro-fields/textareas/
The big advantage is that no matter how many fields you are using, behind the scenes it always just counts as one single field.

It's possible this client I'm working on, has Pro Fields. I'll double-check. But this is also per-template data.

I guess that's just how PW works, haha.

 

I would be nice to really have a completely separated, almost non-page area somewhere in admin, just to define site-wide data from the user admin. Like a custom admin page of arbitrary fields.

On 1/1/2020 at 10:06 PM, adrian said:

SettingsFactory is awesome - I use it on almost all sites now!

That might be a winner.

Link to comment
Share on other sites

1 hour ago, Vigilante said:

This is also nice, but not in the current release yet? I don't have these available in my version.

As noted, this is also part of Pro Fields, and it's been around for quite some time. On second thought, maybe that's not ideal, considering all the other options you have.

1 hour ago, Vigilante said:

Also, it seems to focus on template-specific fields, rather than globally available data. Although I assume maybe you can access this data of one page from another page. Even still that's a bit clunky, trying to remember which page has particular fields you need, etc.

I don't know why you keep repeating this. Use an _init.php that you prepend everywhere. In that file, grab those global field values from that one specific page you've set up for just that reason, and you're set. You'll be able to use these vars from anywhere. You don't even need a physical template file for such a "settings" page, since you don't need a frontend view for that.

  • Like 1
Link to comment
Share on other sites

Just to give you another option. Check out this module by @ryan himself. I did not find it in the modules directory. And it did not receive much attention from the author in a while. But it did the job when I used it (a few years ago ?) . As far as I remember it stores custom data written in site/config.php. And it is manageable from admin without the need to create pages.

  • Like 1
Link to comment
Share on other sites

I've been trying Settings Factory and I simply used the kitchen sink file for testing.

For some reason these errors come up on nearly every page load of the admin, not even editing my custom settings page, they just show up every time the page refreshes:

errors.png.737c546401b3e3a61c820f3e2e8d7834.png

I have valid URLs and Emails but these keep popping up all the time.

Link to comment
Share on other sites

@Vigilante, sorry to hear about that; Probably best to just remove those 2 fields from the kitchen sink file. I'll run a test here to see if those are caused by any recent changes in the fields or admin.

Also, FYI, the reason those show up everywhere is that settings are stored in module config and those do load in the admin; So for example if you have invalid JSON you may see an error everywhere....

Link to comment
Share on other sites

@Vigilante, testing this now, but not seeing any problems here with the kitchen sink file,  either PHP or JSON version.

if i do remove the http:// from the URL field, i do get the notice, but only once.

In terms of the email address, the browser validation won't let me enter an invalid email address, can you post the email address you put in that field?

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...