seddass Posted April 23, 2011 Share Posted April 23, 2011 Hello guys, hope you could help me. I will appreciate it. I am trying to migrate the data from one of my oldest sites to PW. It is about of hundreds of records, please, dont suggest to do it using the admin area. Based on Ryan's code (http://processwire.com/talk/index.php/topic,24.0.html) i have handled creating the pages, but I am still wandering how to "upload/import/insert" images from the file system and to attach them into the $page->images field using the PW API. PW is so scalable and powerful, there should to be a simple way of implementing that. I think i should create Pageimage objects and to "add" them in Pageimages but I missing something. Thanks in advance. Briefly with sample code: $myPage = wire('pages')->get('template=myPageTemplate'); $imagesOld = array('image1.jpg', 'image2.jpg', ....); $myPage->images = $imagesOld; // i know this is wrong $myPage->save(); Link to comment Share on other sites More sharing options...
seddass Posted April 24, 2011 Author Share Posted April 24, 2011 After the morning has come... i think i have figured out. It is simple as i expected. $pageImages = new Pageimages($pageWithImagesField); begin loop $pageImages->add($fullPathToOldImage); end loop $pageWithImagesField->images = $pageImages; $pageWithImagesField->setOutputFormatting(false); // not sure why but there was error without it while saving the page $pageWithImagesField->save(); or alternate way in the loop begin loop $pageImage = new Pageimage($pageImages, $fullPathToOldImage); $pageImage->rename(substr(basename($fullPathToOldImage), 0, -11) . ".jpg"); // some extra manipulations $pageImages->add($pageImage); end loop Hope it will be helpful for someone. Link to comment Share on other sites More sharing options...
ryan Posted April 25, 2011 Share Posted April 25, 2011 Thanks for your post. What you are doing looks right, except that you can skip creating the Pageimages field. PW would rather do that internally. So this is the entirety of what you should do (after you have your $page object): <?php $page->images->add("path or URL to image"); $page->save(); If you are doing this from a template file (as opposed to a shell script or admin module), you have to turn the output formatting off first, as you saw. This is to ensure that you don't accidentally add runtime-formatted content to a page and save it. By "runtime-formatted", I mean entity encoded, markdown, etc. This example might better explain it: <?php $mypage = $pages->get(123); echo $mypage->title; // value is "Products & Services" $mypage->setOutputFormatting(false); echo $mypage->title; // value is "Products & Services" Now lets say that you did this instead: <?php $mypage = $pages->get(123); $mypage->title = "Great " . $mypage->title; $mypage->save(); If ProcessWire let you save that page, the 'title' field would now have this value the next time you viewed it: Great Products && Services Basically, PW only wants to save content in an unformatted "clean" state. It throws that error asking you to set output formatting OFF in order to protect against you corrupting your content. So to follow up from the first example, this is what you should do if you are doing this import from a template file: <?php $page->setOutputFormatting(false); $page->images->add("path or URL to image"); $page->save(); 3 Link to comment Share on other sites More sharing options...
seddass Posted April 25, 2011 Author Share Posted April 25, 2011 Thank you for the clarifications. Now all makes sense. Link to comment Share on other sites More sharing options...
naldrocks98 Posted October 30, 2015 Share Posted October 30, 2015 Good Day! i have a problem when using your code @ryan it duplicates the upload of image even though my field image is set only to one image.. $society->society_logo->add("http://www.thefilipinodoctor.com/medsoc_logo/accp.jpg");$society->save(); Thanks for the advance help... Link to comment Share on other sites More sharing options...
bernhard Posted October 30, 2015 Share Posted October 30, 2015 hi naldrocks98, on the api side image fields are always multi no matter what the settings say! $society->society_logo->removeAll(); // add this line $society->society_logo->add("http://www.thefilipi...ogo/accp.jpg"); $society->save(); explanation by ryan: https://processwire.com/talk/topic/2597-multiple-image-bug-when-using-api-resolved/?p=25452 1 Link to comment Share on other sites More sharing options...
naldrocks98 Posted October 30, 2015 Share Posted October 30, 2015 $society->removeAll(); // add this line $society->society_logo->add("http://www.thefilipinodoctor.com/medsoc_logo/".$get_soc['logo'].""); $society->save(); Thanks.. BernhardB I edit your code bec. there's an error $society->removeAll(); to $society->society_logo->removeAll(); By the way thanks to you... you solved my problem... How about file upload like doc, pdf ? Link to comment Share on other sites More sharing options...
gebeer Posted October 30, 2015 Share Posted October 30, 2015 For file uploads, you may want to look at Soma's example gist. 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