kreativan
3/8/2019 - 11:57 AM

Processwire Templates

Processwire Templates

<?php

// Get pages usign this template
$t = $this->templates->get("TEMPLATE_NAME);
$t->getNumPages();

// Get fields on a template
$t = $this->templates->get("TEMPLATE_NAME);
foreach($t->fields as $f) {
	// do something
}

//-----------------------------------------------------------------------------
//	Create Template
//-----------------------------------------------------------------------------

// 1. new fieldgroup
$fg = new Fieldgroup();
$fg->name = 'TEMPLATE_NAME';
$fg->add($this->fields->get('title')); // needed title field
$fg->add($this->fields->get('body')); // needed body field
$fg->save();

// 2. new template using the fieldgroup and a template
$t = new Template();
$t->name = 'TEMPLATE_NAME';
$t->fieldgroup = $fg; // add the field group
$t->save();

// 3. set template options
$t = wire('templates')->get("TEMPLATE_NAME");
$t->noParents = '-1';
$t->allowPageNum = '1';
$t->urlSegments = '1';
$t->tags = '-Blog';
$t->pageLabelIcon = 'fa-rocket';
$t->parentTemplates = array(wire('templates')->get('PARENT_TEMPLATE_NAME')); // allowedForParents
$t->childTemplates = array(wire('templates')->get('CHILD_TEMPLATE_NAME')); // allowedForChildren
$t->save();

//-----------------------------------------------------------------------------
//	Change field settings for this template
//-----------------------------------------------------------------------------
$t = wire('templates')->get('TEMPLATE_NAME');
$f = $t->fieldgroup->getField('FIELD_NAME', true);
$f->columnWidth = "50";
$this->fields->saveFieldgroupContext($f, $t->fieldgroup);//save new setting in context

//-----------------------------------------------------------------------------
//	Add Fields to the template
//-----------------------------------------------------------------------------
$t = $this->templates->get('home');
$t->fields->add("text");
$t->fields->save(); 


//-----------------------------------------------------------------------------
//	 Insert field to the template on a specific position
//-----------------------------------------------------------------------------
// get template
$template = $page->template;
// is field already exists on template
$exists = $template->fieldgroup->getField("headline");
// new field that we want to insert
$new_field = $fields->get("headline");
// insert new field before existing one
if(!$exists) {
	// we will insert new field before or after this field
	$insertBefore = $page->template->fieldgroup->fields->get("body");
	$template->fieldgroup->insertBefore($new_field, $insertBefore);
	$template->fieldgroup->save();	
}

//-----------------------------------------------------------------------------
//	Get field settinng from template contenxt
//-----------------------------------------------------------------------------
$t = $templates->get("home");
$f = $t->fieldgroup->getFieldContext('body');
print_r($f->label);
print_r($f->showIf);

//-----------------------------------------------------------------------------
//	Remove template system flag
//-----------------------------------------------------------------------------
$tmpl = $this->templates->get("language");
$tmpl->flags = Template::flagSystemOverride;
$tmpl->flags = 0;
$tmpl->save();