Jump to content

Dynamically Create Template


Brian Scramlin
 Share

Recommended Posts

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');

 

  • Like 4
Link to comment
Share on other sites

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