Jump to content
Sign in to follow this  
timothy.m.wilson

Trying to save multiple page reference fields.

Recommended Posts

Good afternoon,

I am attempting to loop through approximately 600 XML nodes. Each of those nodes has 2 child nodes (example below). One is an id for an existing page and the other contains values for a page reference field. I have put together a loop (below as well) to attempt to save each page with the label values. This all works....once. After getting the first XML node "label" saved to the correct page, nothing else is saved. I'm sure there is probably something that I have overlooked. But, I am also very new to this CMS. I'm hoping there is a veteran user who may be able to point me in the right direction. 

 

<row>
    <id>1041</id>
    <labels>lifestyle|savings</labels>
  </row>

 

foreach ($xml->row as $items) {
        
        //get page id to update and set output formatting to false
        $blog_page = $pages->get('id='.$items->id);
        $blog_page->of(false);

        //create array from pipe separated label values
        $labelsArray = explode('|', $items->labels);

        //loop through array to get page ids by page name
        $labelIDS = array();
        foreach ($labelsArray as $labelValue) {

            $labelPage = $pages->get('name='.$labelValue);
            array_push($labelIDS, $labelPage->id);
            
        }

        //add labels to label field in post
        $blog_page->labels = $labelIDS;
        
        //save page
        $blog_page->save();
    

}

Share this post


Link to post
Share on other sites

Wild guess, maybe it's only finding the first blog post, and not the others. I'm not sure $blog_page->labels = $labelIDS; works, since it's assigning plain arrays to a PageArray (as all page fields --set to accept multiple pages-- are).

Try this one. I replaced multiple calls to DB with $pages->findIds, which makes single call, so it's faster. I also replaced $blog_page->labels = $labelIDS; with PageArray::add($ids) method, which can take single id or array of ids (not documented but it works). As labels in your XML set as label1|label2, suitable as $pages selector, you dont need to split it.

Uncomment echo statements to see what pages are the issue

<row>
	<id>1041</id>
	<labels>lifestyle|savings</labels>
</row>

<?php 

foreach ($xml->row as $items) {
        
        //get page id to update and set output formatting to false
        $blog_page = $pages->get('id='.$items->id);
        if(! $blog_page->id) {
        	// echo for debugging
        	// echo "Cant find page with id $items->id <br/>";
        	continue;
        }

        // Get matching page ids at once, returns array of ids
        // https://processwire.com/api/ref/pages/find-i-ds/
        $labelIDS = $pages->findIds("name=$items->labels");

        if(! count($labelIDS)) {
        	// echo for debugging
        	// echo "Cant find labels $items->labels for page $items->id\n";
        	continue;
        }

        $blog_page->of(false);
        $blog_page->labels->add($labelIDS);
        $blog_page->save();
}

 

  • Like 2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jom
      Hi everyone
      It seems that I don't fully understand the wireTempPath() function and I need some help.
      I use wireTempPath() to create a new location in assets/cache/WireTempDir and than copy a pdf from the assets/files/page folder to the new folder. I want the file to be accessible only for a limited time, that's why I use wireTempPath.
      The file seems to be copied to the right location, but gets deleted right afterwards, according to 
      As mentioned in the topic above, 
      $wireTempDir->setRemove(false); prevents the file to be deleted. But I like the file to be automatically deleted after a few days. So, how can I do that?
      My code so far (everything works, but the automatic removal of the tempDir folder):
      //generate and show download link $folder = time(); // timestamp as temporary folder $maxAge = (int) $settings->options_downloadlink_valid_hours * 3600; //tempDir wants maxAge as seconds $options = array( 'maxAge' => $maxAge ); $wireTempDir = wireTempDir($folder, $options); $wireTempDir->setRemove(false); $src_file = $page->ebook_download->filename; // Create a new directory in ProcessWire's cache dir if(wire('files')->mkdir($wireTempDir, $recursive = true)) { if(wire('files')->copy($src_file, $wireTempDir)){ //get subdirs from tempDir: $pos = strpos($wireTempDir, "WireTempDir"); $subdir = substr($wireTempDir, $pos, 100); $out .= "<p><a href='" . wire('pages')->get('template=passthrough')->httpUrl . "?file=" . $subdir . $page->ebook_download->basename . "' target='_blank'>$page->title</a></p>"; } } I appreciate any ideas - thanks!
      Oliver
    • By Mithlesh
      Changing it to null
    • By Mithlesh
      Hi, I have one URL - writerrelocations.com/contact-now/

      I have one issue where my header Image is appearing again after the contact form, for your reference:



      I have checked my processwire template section where have not added any Image field besides Header Image, attaching the same for the reference:




      PLEASE help me to get rid of the image below contact form

      Waiting for the swift response!

      Thanks
       
    • By VeiJari
      Hello forum, this is my first security related post, so I'm a bit of a newbie.
      I understand that when I have direct front-input from user I should sanitize the input, but how about when I use a secret key for showing a API for a third-party supplier? Should I sanitize the input->get() key?
      I've tested this issue and I tried ?key=<?php echo $page->field; ?> And without adding any sanitization it comes back: /?key=<?php%20echo%20$page->field;%20?>
      So can I rely on this, or should I still use $sanitizer just in case?
       
      Thanks for the help!
×
×
  • Create New...