Gayan Virajith Posted November 11, 2013 Share Posted November 11, 2013 Hi, Thanks again for great CMS. I was trying to create a module that can create a new template file and fields. Here is my module script: class NewsPager extends WireData implements Module{ public static function getModuleInfo() { return array( 'title' => 'News pager', 'version' => 001, 'summary' => 'News pager testing!', 'singular' => true, 'autoload' => true, ); } public function init() { //todo add some listeners } /** * Install the module * */ public function ___install() { // new fieldgroup $fg = new Fieldgroup(); $fg->name = 'new-template9'; $fg->add($this->fields->get('title')); // needed title field $fg->add($this->fields->get('body')); // needed title field $fg->save(); // new template using the fieldgroup $t = new Template(); $t->name = 'new-template9'; $t->filename=$this->config->paths->templates . 'news-pager-index.php'; $t->fieldgroup = $fg; // add the fieldgroup $t->save(); } /** * Uninstall the module * */ public function ___uninstall() { $templateNeeded = $this->templates->get("new-template9"); if ($templateNeeded->getNumPages() > 0) { throw new WireException("Can't uninstall because template been used by some pages."); }else { wire('templates')->delete($templateNeeded); } } } The module works fine. But when I try to create pages by using newly created template, in that case the view link is not appearing. Is there any property that I should use for template creation to get view link to be worked. Thank you Link to comment Share on other sites More sharing options...
apeisa Posted November 11, 2013 Share Posted November 11, 2013 try setting the filename without full path. 1 Link to comment Share on other sites More sharing options...
Soma Posted November 11, 2013 Share Posted November 11, 2013 I think you don't need to set it. It will automatically take the template name without .php To set a filename other than its name you need to set the alternative name. 1 Link to comment Share on other sites More sharing options...
adrian Posted November 12, 2013 Share Posted November 12, 2013 Does the news-pager-index.php file exist in the templates directory? 1 Link to comment Share on other sites More sharing options...
Gayan Virajith Posted November 12, 2013 Author Share Posted November 12, 2013 Thank you so much apesia, soma and adrian. @soma and @apeisa - I followed your advise. Thanks again now it is working fine (happy). @adrian - Yes adrian I put news-pager-index.php file under template directory. Just to recap: if we need use external template file other than the template name we have to use altFilename property. In the normal scenario we can use name property if the template file name and template name were same. Here is the working copy thanks to processwire and friendly community members. It might be helpful to newbies like me. class NewsPager extends WireData implements Module{ public static function getModuleInfo() { return array( 'title' => 'News pager', 'version' => 001, 'summary' => 'News pager template', 'singular' => true, 'autoload' => true, ); } public function init() { //todo add some listeners } /** * Install the module * */ public function ___install() { // new fieldgroup $fg = new Fieldgroup(); $fg->name = 'news-pager-index'; $fg->add($this->fields->get('title')); // needed title field $fg->add($this->fields->get('body')); // needed body field $fg->save(); // new template using the fieldgroup and a template $t = new Template(); $t->name = 'news-pager-index';//Used different name other than file name /* * altFilename property will accept file-name or file-name.php * or even $this->config->paths->templates . 'file-name.php' * Note: template file needs to be placed under * template directory */ $t->altFilename = 'news-pager-index-file';//name of the template file which is under tml dir. $t->fieldgroup = $fg; // add the field group $t->save(); } /** * Uninstall the module * */ public function ___uninstall() { $templateNeeded = $this->templates->get("news-pager-index"); $fgOld = $this->fieldgroups->get('news-pager-index'); if ($templateNeeded->getNumPages() > 0) { throw new WireException("Can't uninstall because template been used by some pages."); }else { wire('templates')->delete($templateNeeded); wire('fieldgroups')->delete($fgOld); } } } 8 Link to comment Share on other sites More sharing options...
froot Posted September 5, 2022 Share Posted September 5, 2022 what an old thread… anyways, since PW is backward compatible to the extreme, I wonder why my approach doesn't work if it's so similar, see issue in this thread here… The problem being, that I cannot edit the template's fields afterwards (add/remove/change order of fields), it's not an ASMSelect field and so it's kind of locked. Here's my code to create the template and fieldgroup with if(!$templates->get('kiosk_order')) : $fg = new Fieldgroup(); $fg->name = 'fieldgroup_kiosk_order'; $fg->add($fields->get('title')); // Add title field (mandatory!) $fg->add($fields->get('kiosk_order_id')); $fg->add($fields->get('kiosk_order_products')); $fg->add($fields->get('kiosk_order_customer_name')); $fg->add($fields->get('kiosk_order_customer_emailaddress')); $fg->add($fields->get('kiosk_order_customer_address_billing')); $fg->add($fields->get('kiosk_order_customer_address_shipping')); $fg->add($fields->get('kiosk_order_payment_method')); $fg->add($fields->get('kiosk_order_total_and_shipping')); $fg->save(); $kiosk_order = new Template(); $kiosk_order->name = 'kiosk_order'; // $kiosk_order->title = 'Kiosk Order'; $kiosk_order->label = 'Kiosk Order'; $kiosk_order->fieldgroup = $fg; $kiosk_order->pageLabelField = 'title'; $kiosk_order->parentTemplates = array($kiosk_orders->id); $kiosk_order->noChildren = 1; $kiosk_order->tags = 'kiosk order'; $kiosk_order->icon = 'shopping-bag'; $kiosk_order->save(); endif; 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