Jump to content

Creating a template to use as a category


GuruMeditation
 Share

Recommended Posts

Hi,

I'm trying to create a template for a module I'm working on. The page that uses this template will be used specifically as a category to hold other pages. For this reason, I don't need to add any extra fields to it, other than the title, and obviously the name. But I can't seem to figure out how to do it.

I've tried the following, but it complains about needing a field.

$template_category = new Template();
$template_category->name = 'template_category';
$template_category = $template_category->save();

Any help appreciated.

Link to comment
Share on other sites

Templates need corresponding fieldgroups...Something like below should do it (not tested, written quickly!)

//new fieldgroup
$fg = new Fieldgroup();
$fg->name = 'template-category';
$fg->add(wire('fields')->get('title'));
$fg->save();//save the fieldgroup

//create a new template to use with this fieldgroup
$template_category = new Template();
$template_category->name = 'template-category';
$template_category->fieldgroup = $fg; //add the fieldgroup we created above
$template_category->label = 'Category Template'; //template label
$template_category->save();
  • Like 4
Link to comment
Share on other sites

kogondo was a little faster, anyway also not tested, just grabbed from a module i was working on. This also includes the creation of the page containing the template.

$fieldgroup_category = new Fieldgroup();
$fieldgroup_category->name = 'template_category';
$fieldgroup_category->add($this->fields->get('title'));
$fieldgroup_category->save();

$template_category = new Template();
$template_category->name = 'template_category';
$template_category->fieldgroup = $fieldgroup_category;
$template_category->noChildren = 0;
$template_category->noParents = 0;
$template_category->slashUrls = 1;
$template_category->urlSegments = 0;
$template_category->pageLabelField = 'title';
$template_category->save();

$page_category = new Page();
$page_category->template = $template_category;
$page_category->parent = 1;
$page_category->title = 'Category';
$page_category->name = 'category';
$page_category->process = $this;
$page_category->save();
  • Like 3
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

×
×
  • Create New...