rlwsota Posted February 21, 2016 Share Posted February 21, 2016 I have what I gather is some sort of incompatibility and ask for any advice that may be available.I have a field (‘favs’) in the user template. This is a page field currently set for multiple pages (page array) with checkboxes and calls pages from a specific parent (articles).A user profile is based on Ryan’s Form Builder profile and those items that are checked load when calling the field.In another template, I have an ‘add' button that leads to a php file (currently shown on a page to troubleshoot) that includes ‘$user->favs->add($new_page)’ (also tried push and append, but results the same) that is intended to add the page that hosts the button to the favs field. The new php file/page does echo the $new_page ID and on that page, as a test I do run a “foreach…” routine to list the pages in the field, the new one appears to be added to the other pages in the field.Unfortunately, when I look at the user information from the admin side, the new page does not appear to have been added and when I pull up the Profile again, it is not added.On a suspicion that the checkboxes is the issue, I tried changing it to select/selectmultiple/asmselect but there is no change. I also tried various versions of adding the new page with a value = 1 (got an error).All I want to do is deal with ‘favs’ as an array of page ids that I can reference accourdingly. I’m sure I must be missing something quite obvious, but I can’t figure out how to add a new page to the page field and have it actually appear.Help appreciated. To add some clarity, this is the bit of code with which I am trying to add the new page:$page_id = $_GET[‘source_page’]; //referring page, shows up properly with echo$p = $user->favs; //existing page field contents, shows up correctly with echo$p->add($page_id);$p->save(); Thanks. Link to comment Share on other sites More sharing options...
cstevensjr Posted February 21, 2016 Share Posted February 21, 2016 I have a field (‘favs’) in the user template. This is a page field currently set for multiple pages (page array) with checkboxes and calls pages from a specific parent (articles). A user profile is based on Ryan’s Form Builder profile and those items that are checked load when calling the field. If you don't mind, could you please explain a little more in-depth about what you mean when you say "A user profile is based on Ryan’s Form Builder profile"? Thanks. Link to comment Share on other sites More sharing options...
rlwsota Posted February 21, 2016 Author Share Posted February 21, 2016 cstevensjr, Ah, clarity. Often useful, sometimes not provided. My bad. I have a Profile page that Ryan put forth in the Form Builder forum (https://processwire.com/talk/topic/9574-edit-user-profile-frontend/?hl=profile) that shows fields in a user's profile. In my case, I have added to the Profile to show the favs pages. The ones that already are in the array appear (checked) but the new one I am trying to add do not appear. Does that answer your question? Link to comment Share on other sites More sharing options...
cstevensjr Posted February 21, 2016 Share Posted February 21, 2016 Thanks for providing additional information. Can you please provide any code, that you are using, so that forum members can possibly look at it? I believe this will help someone properly troubleshoot your problem. Link to comment Share on other sites More sharing options...
rlwsota Posted February 22, 2016 Author Share Posted February 22, 2016 cstevensjr, I edited my original post to show the code that I am using for the page array addition. The only other items on the page are some echo statements to confirm that, in fact, the page referral is correct (it is), that the original page field shows the current contents (it does) and a 'foreach(...' to step through each of the page field contents. It is as follows: $page_id = $_GET[‘source_page’]; echo $page_id; // confirmed $p = $user->favs; //existing page field echo $user->name; // confirmed echo $p; // confirmed $p->add($page_id); $p->save(); $FP = $user->favs; foreach($FP as $fave) { $content .= "<p>$fave $fave->title</p>"; }When the ‘foreach…’ routine runs, it does show the newly added $page_id, but that is not saved to the page itself.Thanks for your consideration. Link to comment Share on other sites More sharing options...
adrian Posted February 22, 2016 Share Posted February 22, 2016 Make sure you have debug mode on (or TracyDebugger installed) and let us know if there are any errors. I think it might be as simple as needing to add: $p->of(false); before you add the page. Link to comment Share on other sites More sharing options...
rlwsota Posted February 22, 2016 Author Share Posted February 22, 2016 Adrian, Thanks for the suggestion. I did have TracyDebugger installed (but do now). When I added the $p->of(false); code before adding the page, I got an error as follows: Server ErrorWe're sorry! The server encountered an internal error and was unable to complete your request. Please try again later. error 500 The error log shows the following with the code installed: Error: Exception: Method PageArray::of does not exist or is not callable in this context (in /home/my_website/public_html/wire/core/Wire.php line 358) #0 /home/my_website/public_html/wire/core/WireArray.php(1686): Wire->___callUnknown('of', Array) #1 [internal function]: WireArray->___callUnknown('of', Array) #2 /home/my_website/public_html/wire/core/Wire.php(398): call_user_func_array(Array, Array) #3 /home/my_website/public_html/wire/core/Wire.php(333): Wire->runHooks('callUnknown', Array) #4 /home/my_website/public_html/wire/core/Wire.php(337): Wire->__call('callUnknown', Array) #5 /home/my_website/public_html/wire/core/Wire.php(337): PageArray->callUnknown('of', Array) #6 /home/my_website/public_html/site/templates/follow.php(18): Wire->__call('of', Array) #7 /home/my_website/public_html/site/templates/follow.php(18): PageArray->of(false) #8 /home/my_website/public_html/wire/core/TemplateFile.php(182): require('/home/my_website/...') #9 [internal function]: TemplateFile->___render() #10 /home/my_website/public_html/wire/core/Wire.ph When I // the line, the page runs with the right echos but still no saving to the page field. Link to comment Share on other sites More sharing options...
adrian Posted February 22, 2016 Share Posted February 22, 2016 Are you trying to add a new page to the list of available favs, or to select a page from favs (defined by $page_id) for the current user's favs field? Link to comment Share on other sites More sharing options...
rlwsota Posted February 22, 2016 Author Share Posted February 22, 2016 Adrian Sorry if I didn't make it clear. As a user is reading an article (page), they are able to hit a button to add that page to their favs list. Right now, I have a button that takes it to a page to confirm the transfer of information as shown above and make the array addition but I once it is working I will simply refer it to a .php file to handle the addition to the page field. Does that answer your question? Link to comment Share on other sites More sharing options...
adrian Posted February 22, 2016 Share Posted February 22, 2016 Yep, so this is what you need: $user->of(false); $user->favs->add($page_id); $user->save("favs"); This of course assumes you are editing the currently logged in user. If not, then something like this: $u = $users->get($user_id); $u->of(false); $u->favs->add($page_id); $u->save("favs"); 1 Link to comment Share on other sites More sharing options...
rlwsota Posted February 22, 2016 Author Share Posted February 22, 2016 Adrian, Perfect! You are a Champ! Many thanks. 1 Link to comment Share on other sites More sharing options...
adrian Posted February 22, 2016 Share Posted February 22, 2016 No problem, I thought I should add that you can also do: $user->setAndSave('favs', $page_id); Simple as that! It's a relatively new method and I still forget about it myself. Read more about it here: https://processwire.com/blog/posts/processwire-2.6.9-core-updates-and-new-procache-version/#new-page-gt-setandsave-method Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now