Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-05-09 14:41:39

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

set_pref() to change prefs values on saving

Hi,
I’m able to set a callback in a plugin with the prefs event and my function return the right variables (related to a first selected option) after saving but I can’t use set_pref to change some input values with my variables.
…I mean, I can but it does not work with ajax, only on good old page refresh.
Does it make sense for anyone?
Is there a reason why new variables can be displayed after saving (ajax) but new prefs values set via set_pref() are ignored?
Thanks!

Last edited by NicolasGraph (2016-05-09 14:43:12)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#2 2016-05-09 14:58:14

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: set_pref() to change prefs values on saving

I was always wondering too, why one has to write something like

set_pref('something', $prefs['something'] = 'new value', ...);

Offline

#3 2016-05-09 15:15:14

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: set_pref() to change prefs values on saving

etc wrote #299036:

I was always wondering too, why one has to write something like

set_pref('something', $prefs['something'] = 'new value', ...);...

Should the previous code work? I can’t see any change; $value and set_pref('something', $value) + get_pref('something') are still differents for ajax…


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#4 2016-05-09 15:25:29

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: set_pref() to change prefs values on saving

NicolasGraph wrote #299037:

Should the previous code work?

I mean

global $prefs;
set_pref('something', 'new value', ...);
echo get_pref('something'); // old value

vs

global $prefs;
set_pref('something', $prefs['something'] = 'new value', ...);
echo get_pref('something'); // new value

I can’t see any change; $value and set_pref('something', $value) + get_pref('something') are still differents for ajax…

I’m not sure to understand what you mean by “for ajax”?

Offline

#5 2016-05-09 15:36:44

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: set_pref() to change prefs values on saving

etc wrote #299038:

I mean

global $prefs;...

Oh yes, that’s what I did; the mistake is in my post only.

I’m not sure to understand what you mean by “for ajax”?

I talk about ajax for the usual preferences saving vs a page refresh.
After a page refresh my prefs are ok, but on saving via the Save button, they are not (while my variables are).
Is it clear?

Last edited by NicolasGraph (2016-05-09 15:39:50)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#6 2016-05-09 15:50:53

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: set_pref() to change prefs values on saving

…wait, wait. In fact echo get_pref('something') works with your previous code but the input value stays unchanged after saving for now.

Last edited by NicolasGraph (2016-05-09 15:56:44)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#7 2016-05-09 15:57:29

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: set_pref() to change prefs values on saving

Probably, your callback is fired when the page is already rendered? Try to set it with $pre = 1.

Offline

#8 2016-05-09 16:07:45

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: set_pref() to change prefs values on saving

etc wrote #299041:

Probably, your callback is fired when the page is already rendered? Try to set it with $pre = 1.

Here is what I’m using:

register_callback('something', 'prefs', 'prefs_save', 1);

It should be ok, isn’t it?

Edit: register_callback('oui_quote_inject_data', 'prefs', 'prefs_save', $pre=1); is not better…
Edit#2: $step changed.

Last edited by NicolasGraph (2016-05-09 17:07:59)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#9 2016-05-09 18:58:01

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: set_pref() to change prefs values on saving

I confess it’s rather confusing. There is no evidence of ajax requests while saving the preferences. The whole updated html page is sent to clients, unless your plugin is changing something. Mind posting the code?

Offline

#10 2016-05-09 22:44:59

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,269
Website GitHub

Re: set_pref() to change prefs values on saving

I don’t think it’s anything to do with AJAX per se. When you save a pref, it writes it to the database but it does so after the txp_prefs table has been extracted into the global scope. This is irrespective of pre. Usually this situation is fine because you set the pref and move on to another page or something, but in the case where you save and then re-display the same page, you see the stale (previous) value until you refresh the page one more time.

The solution? In your get_pref() call, set its third parameter to true (the second param is the default value in case the pref is not yet set). Using the third param forces the value to be fetched from the database instead of relying on the already extracted value, which we do to save on the number of queries being made. See if that works.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#11 2016-05-10 08:23:17

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: set_pref() to change prefs values on saving

Bloke wrote #299046:

The solution? In your get_pref() call, set its third parameter to true

Hi, thank you Stef but I’m not sure to understand. I’m already able to display the good pref value with a provisory echo get_pref('something'); on the Preferences page but the pref input value is not refreshed (as it comes from the global scope if I follow you) …and I don’t use get_pref to display this input or its value; the field already exists as it can be also filled manually.
Anyway, If there is a way to do it, I’d be happy to learn, otherwise I think I could do without for now. Thank you both; Oleg I keep your way to set prefs in callbacks in my toolbox.


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#12 2016-05-10 08:37:35

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: set_pref() to change prefs values on saving

Nicolas, what if you unset($_POST['something']) in your plugin registered with $pre = 1?

Offline

Board footer

Powered by FluxBB