thetuningspoon Posted February 4, 2015 Share Posted February 4, 2015 I've been building some modules which use fields/pages/templates for data storage and manipulation. I'm nearing completion on one of these modules, and am going to have to move it from my development to my production PW installation on a different site. The problem is that most of the fields, templates, and structural pages were created via the admin UI, which means creating my install() function is going to be quite tedious. I suppose this is my fault for not creating these things via the API all along. Is there any kind of utility that exists that would help me generate this code, or is there something else I might be overlooking? If not, I guess I have a bit of coding ahead of me Link to comment Share on other sites More sharing options...
apeisa Posted February 4, 2015 Share Posted February 4, 2015 Fields and templates have export and import utilities, that are usable from UI and API. 1 Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 4, 2015 Author Share Posted February 4, 2015 Hi apeisa, So those produce json, right? So I could use the utility to generate the json and then use that json in my install() function? Link to comment Share on other sites More sharing options...
kongondo Posted February 4, 2015 Share Posted February 4, 2015 (edited) Yes, the export generates JSON. It's all documented here: https://processwire.com/blog/posts/august-2014-core-updates-1/ - Field export/import And here: https://processwire.com/blog/posts/august-2014-core-updates-3/#template-export-import - Template export/import and more here: https://processwire.com/talk/topic/2117-continuous-integration-of-field-and-template-changes/?p=68899 Edited February 4, 2015 by kongondo 1 1 Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 4, 2015 Author Share Posted February 4, 2015 Yes, I was looking that over. It hadn't occurred to me that the JSON export/import could be used in such a way. I'm thinking that it is still best to do it one field and one template at a time, so I can check for the existence of the item before creating it and also make sure I get the order correct when it comes to Page fields. But it should save tons of time either way! Edit: This looks a little more complex than at first glance, looking at ProcessFieldExportImport. Link to comment Share on other sites More sharing options...
Pierre-Luc Posted February 4, 2015 Share Posted February 4, 2015 @kongondo, it's nice to know. I've been looking for this too myself, but would never have thought to look at the blog for documentation… Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 4, 2015 Author Share Posted February 4, 2015 Okay, so after playing around with this and looking at Ryan's code in ProcessFieldExportImport I was able to put together a simplified function to create fields from a JSON export. This only creates a field if it doesn't already exist, and you can do it one at a time or send in multiple fields at once. protected function buildFieldsFromJson($json) { $data = is_array($json) ? $json : wireDecodeJSON($json); // Loop through each field in the JSON foreach($data as $name => $fieldData) { unset($fieldData['id']); // Get rid of the ID so it doesn't conflict with the new installation // Create the field only if it doesn't already exist if(!$this->fields->get($name)) { $field = new Field(); $field->name = $name; $field->setImportData($fieldData); // Import the data for the field $field->save(); } } } Calling it from within my module's install() function: $this->buildFieldsFromJson('PASTE_JSON_EXPORT_HERE'); Hope that helps someone else. Now onto the templates... 2 Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 4, 2015 Author Share Posted February 4, 2015 Hmm... When I try this with templates I get the error "You must save Fieldgroup 'X' before adding to Template 'X'" It seems like that would be the job of the setImportData() function to make sure that happens? Edit: Got it. Here's the template builder: protected function buildTemplatesFromJson($json) { $data = is_array($json) ? $json : wireDecodeJSON($json); // Loop through each template in the JSON foreach($data as $name => $templateData) { unset($templateData['id']); // Get rid of the ID so it doesn't conflict with the new installation $template = $this->templates->get($name); // If the template exists, grab it // Create the template if it doesn't already exist if(!$template) { $template = new Template(); $template->name = $name; } $template->setImportData($templateData); // Import the data for the field $fieldgroup = $template->fieldgroup; $fieldgroup->save(); $fieldgroup->saveContext(); $template->save(); if(!$template->fieldgroup_id) { $template->setFieldgroup($fieldgroup); $template->save(); } } } 3 Link to comment Share on other sites More sharing options...
gebeer Posted December 19, 2019 Share Posted December 19, 2019 Just noticed, that the $field->setImportData($fieldData) method is not documented at https://processwire.com/api/ref/field/ 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