Jump to content

How to create template file using api under a module


Gayan Virajith
 Share

Recommended Posts

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

post-1911-0-16882200-1384171166_thumb.pn

post-1911-0-80469500-1384171173_thumb.pn

Link to comment
Share on other sites

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

}

  • Like 8
Link to comment
Share on other sites

  • 8 years later...

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

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