Brian Scramlin Posted October 8, 2021 Share Posted October 8, 2021 (edited) Hi, Everyone! I have been attempting to track down how to 1. Create a template 2. Add fields to it 3. Save it to database via the API. Can anyone get me sniffing in the right direction? Edited October 8, 2021 by Brian Scramlin I was out in the weeds. Link to comment Share on other sites More sharing options...
ukyo Posted October 8, 2021 Share Posted October 8, 2021 Basically <?php namespace ProcessWire; // create field $field = new Field(); $field->name = 'my_field'; $field->label = 'My Field'; $field->type = 'FieldtypeText'; // save field wire('fields')->save($field); // get field $field = wire('fields')->get('my_field'); // Create fieldgroup $fieldgroup = new Fieldgroup(); // if you want to edit fields in this field group via admin panel, name need to be same with template name $fieldgroup->name = 'my_template'; $fieldgroup->add('title'); $fieldgroup->add('body'); $fieldgroup->add($field); // save fieldgroup wire('fieldgroups')->save($fieldgroup); // get fieldgroup $fieldgroup = wire('fieldgroups')->get('my_template'); // Create template $template = new Template(); $template->name = 'my_template'; $template->label = 'My Template'; $template->fieldgroup = $fieldgroup; // save template wire('templates')->save($template); // get template $template = wire('templates')->get('my_template'); 3 Link to comment Share on other sites More sharing options...
elabx Posted October 9, 2021 Share Posted October 9, 2021 Another really useful tool is RockMigrations. Link to comment Share on other sites More sharing options...
Brian Scramlin Posted October 10, 2021 Author Share Posted October 10, 2021 Thank you @ukyo and @elabx this is exactly what I needed. I really tried to read through the API before asking, mainly because I didn't understand the relationship / order between everything. You guys are great! 1 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