Search the Community
Showing results for tags 'create template'.
-
Hey folks. Iam working on a module that creates some templates and fields. The module creates these on install and deletes them on uninstall, works great. Now Iam looking for a way to run the create function when the module is updated, but It's not doing anything. I changed the version number and hit refreh on the module manager and a message indicated the update, but my function is not executed. Not sure if Iam hooking the right function here or if there is an __update() function I can use? Here is my code so far: <?php namespace ProcessWire; class BlockEditor extends WireData implements Module { public static function getModuleInfo() { return [ 'title' => 'Editor for PageGrid', 'summary' => 'Installs Template and Fields for a PageGrid Block', 'author' => 'Jan Ploch', 'icon' => 'font', 'autoload' => 'template=admin', 'version' => 1 ]; } public function getBlockName() { return array( 'name' => 'pg_editor', 'label' => 'Editor', ); } //function to create block public function createBlock() { $this->message("Create Block"); $this->log->save("PageGridBlock", "Create Block"); $fs = wire('fields'); $t = wire('templates'); $blockName = $this->getBlockName()['name']; $blockLabel = $this->getBlockName()['label']; if (!$fs->get($blockName)) { $f = new Field; $f->type = $this->modules->get("FieldtypeTextarea"); $f->name = $blockName; $f->label = $this->_($blockLabel); $f->tags = 'pgrid'; $f->save(); } if (!$t->get($blockName)) { $f = $fs->get($blockName); $titleF = $fs->get('title'); // fieldgroup for template $fg = new Fieldgroup(); $fg->name = $blockName; $fg->add($titleF); $fg->add($f); $fg->save(); $t = new Template(); $t->name = $blockName; $t->fieldgroup = $fg; $t->icon = $this->getModuleInfo()['icon']; $t->save(); } // Copy block files $copyFrom = $this->config->paths->BlockEditor . "blocks/"; $copyTo = $this->config->paths->templates . "blocks/"; $this->files->copy($copyFrom, $copyTo); } //function to remove block public function removeBlock() { $this->message("Remove Block"); $blockName = $this->getBlockName()['name']; $t = $this->templates->get($blockName); $fg = $this->fieldgroups->get($blockName); $f = $this->fields->get($blockName); if ($t && $f && $t->getNumPages() > 0 && $f->getNumPages() > 0) { throw new WireException("Can't uninstall because template or fields been used by some pages."); } else { if ($t) { wire('templates')->delete($t); } if ($fg) { wire('fieldgroups')->delete($fg); } if ($f) { wire('fields')->delete($f); } } } public function init() { // trigger on module update $this->addHookAfter('Process::upgrade', function (HookEvent $event) { bd('update'); $this->createBlock(); }); } public function ___install() { $this->createBlock(); } public function ___uninstall() { $this->removeBlock(); } }
-
Hi! I tried to crate a little module that creates me a few template ... but without success so far. I have an array with a few template-names. I walk througt the array to create the templates. The module doesnt work porperly. And i havent find out how to add fields to the templates (at least a title field)? Here is my code: <?php class maketemplate extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'maketemplate', 'version' => 1, 'summary' => 'Creates templates', 'author' => 'Me', 'singular' => true, 'autoload' => true, 'icon' => 'diamond' ); } public function ___install() { $array1 = array("mytemplate1", "mytemplate2","mytemplate3"); foreach($array1 as $templ) { $t = new Template(); $t->name = $templ; $t->save(); } $this->message("Template created"); } } ?> Or is there already a module that can do this job? Cheers!