Peter Falkenberg Brown Posted October 27, 2013 Share Posted October 27, 2013 Dear All, I couldn't find the answer to this... hopefully I didn't miss a post, since I don't want to duplicate posts. I'm having trouble adding a page that has an image field defined in the template field group, even though I'm not calling the image field in the code. I realize that you have to save the page first, before you add images. Thus, I'm trying to add a page and save it, without calling the image field. I've created a generic routine to create a form to add pages, based on a field list array and a routine that loops through the fields, calling them like this. First, I create the arrray (without the image field): $field_array = array( 'title' => '', 'body' => '', ); Then, based an action param, the script goes into "add mode", which presents the user with a blank add form. NOTE that at this point, nothing has been saved. # add mode $out .= "<br><h2>:: Add Mode ::</h2><br>"; $new_page = new Page(); $new_page->template = $templates->get("$template_field_group"); $field_template = file_get_contents("$edit_template"); if ( !@include "./inc_private_add_fields_code.php" ){echo $include_error; exit;} $save_form = " $cancel_button <form action='$full_page_url' method='POST' style='display: inline; margin: 0;'> $save_button <input type='hidden' name='a_' value='s'><br> <br> $field_template <br> $save_button </form> $cancel_button "; $out .= $save_form; Here's the code in the add routine, inc_private_add_fields_code.php: I think it's bombing out on the very first line: $inputfields = $new_page->getInputfields(); When I removed the image field from the template, it all worked fine. $inputfields = $new_page->getInputfields(); foreach ( $field_array as $field_name => $display_status ) { $field_tag = ':!:' . $field_name . ':!:'; if ( $display_status == 'display_only' ) { $field_value = ''; } else { $new_page->setOutputFormatting(false); $field = $inputfields->get("$field_name"); $field_value = $field->render(); } $field_template = str_replace($field_tag, $field_value, $field_template); } The problem is that I want to: - keep the image field in the template - allow a user to add a page and save it without an image (per the PW need to have the page saved first) - add images later, in 'edit' mode. However, when I try to present an add form above, because the template field group has an image field, it bombs with the error: Error: Exception: New page '//' must be saved before files can be accessed from it I don't think it's a good idea to save a page from an 'add page form' before the user has hit save, because the user might want to cancel it. I hope there's a clever, PW way around this. I also hope that it can be integrated into the code above in "inc_private_add_fields_code.php", which as been working like a charm so far, as a generic code routine. Thanks for anyone's help! Peter Link to comment Share on other sites More sharing options...
adrian Posted October 28, 2013 Share Posted October 28, 2013 Hi Peter, This may not be an ideal approach, but you can save the page unpublished until the user actually clicks the save button. That way if they cancel/don't save, then it won't be published. You could also setup a lazycron to cleanup these canceled pages if need be. Link to comment Share on other sites More sharing options...
Peter Falkenberg Brown Posted October 28, 2013 Author Share Posted October 28, 2013 Dear Adrian, Yes, that would work, but I have to believe there's a correct "API" way to do this. Ryan and Soma, or anyone else?? I'll bet a donut that it's something simple, but perhaps undocumented. At least I'm hoping it's simple. Peter Link to comment Share on other sites More sharing options...
Pete Posted October 28, 2013 Share Posted October 28, 2013 I'm really confused by what you're trying to do - why would the page ever be saved before the user clicks save? If you could post a few bullet points explaining what the user will experience whilst filling out this form it might help us come up with a suggestion. Other than that there are many topics here how to save the page (http://processwire.com/talk/topic/352-creating-pages-via-api/ ) but you appear to be talking about everything up to that point which I would have thought would just be the job of a normal form? Link to comment Share on other sites More sharing options...
Soma Posted October 28, 2013 Share Posted October 28, 2013 $new_page->getInputfields(); will get the inputfields in context of a page already and thinks there's images or tries to access them, but there isn't. This has come up a a few times but can't find it. FormTemplateProcessor module by Ryan does the same and suffers from the same problem when using file fields (it's not meant to work with it anyway) https://github.com/ryancramerdesign/FormTemplateProcessor/blob/master/FormTemplateProcessor.module One way around this would be to create the form not using $page->getInputfields() but $page->template->fields and use it to get the inputfield foreach($page->template->fields as $field) { $inputfield = $fields->get($field->name)->getInputfield(null); } 1 Link to comment Share on other sites More sharing options...
Peter Falkenberg Brown Posted October 28, 2013 Author Share Posted October 28, 2013 Dear Pete and Soma, I don't want to save the form before the user does; I was just referring to the PW need to save the page before images get added. The error actually happens when the script tries to present the user with a blank form to add data, what I would call "add" mode, rather than "save" mode. I think Soma hit upon the answer. Thanks, Soma! I believe that I can still use my array of fields, and simply use the line you suggested: $inputfield = $fields->get($field->name)->getInputfield(null); inside the loop. I'll test it soon. If this works, then it's great, because it's an easy fix. Thanks again, Peter 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