Jump to content

Utility to help generate module install() function?


Recommended Posts

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

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 by kongondo
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

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

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...

  • Like 2
Link to comment
Share on other sites

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();
  		}
  	}
  }
  • Like 3
Link to comment
Share on other sites

  • 4 years later...

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...