Jump to content

Page added to page field does not appear to be saved


rlwsota
 Share

Recommended Posts

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

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

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

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

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 Error

We'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

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

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");
  • Like 1
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...