Jump to content

Hook on Templates::save and get affected Template

Recommended Posts

Hi there,

im a bit lost right now. I added an hook after templates::save which should get the affected template and write its template ID into a JSON.

The problem is, the event->object gives me an instance of the templates class. How can I narrow this down to the saved Template and get its ID and fieldgroup?

Share this post

Link to post
Share on other sites
9 minutes ago, BitPoet said:

Use $event->arguments(0);

Yeah, this did the trick... Thank you 🙂

My solution for what I needed this for:

public function templateSave($event){
        $templateId = $event->arguments(0)->id;
        $templates = $this->wire('templates');
        $template = $templates->get($templateId);
        $fieldgroup = $this->wire->fieldgroups->get($template->name);
        $data['name'] = $template->name;
        foreach($fieldgroup as $k => $v){
            $field = $this->wire('fields')->get($k);
            $data['fields'][] = ['name' => $field->name, 'id' => $k];
        $data = json_encode($data, JSON_PRETTY_PRINT);

        $file = $this->wire('config')->paths->root.'models/'. $template->name .'.json';
        file_put_contents($file, $data);

which would generate something like

    "name": "BaseController",
    "fields": [
            "name": "title",
            "id": 1
            "name": "ControllerEnvironment",
            "id": 219
            "name": "active",
            "id": 132
            "name": "accesspermissions",
            "id": 197
            "name": "accesspermission",
            "id": 196
            "name": "appcompanyname",
            "id": 125
            "name": "appuserreference",
            "id": 177
            "name": "appuserroles",
            "id": 186


Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Clément Lambelet
      I've been working on a Processwire installation (3.0.123) for a few days now and I must have made a big mistake this morning because the links in the admin's main menu no longer appear.
      This is not related to the admin theme, because the bug occurs with all themes (Default, Reno, Uikit).
      I tried to reinstall with the dev version (3.0.136), but the problem is still there. I also uninstalled all the modules I had added, without success. There is no error in the js console. 
      I still can access/view/edit the pages  by going through the admin/page list.
      Thanks in advance for your help!

    • By lecrackffm
      Hey all,
      i do, most probably have a problem with file encoding and / or BOM. 
      My <head>  content is getting moved into <body> and i can inspect a couple of &#65279;  ( ZERO WIDTH NO-BREAK SPACE) chars with dev tools.
      I checked all template files with phpstorm, they are all in utf-8 w/o BOM.
      The current installation is based on a profile i exported from another project where i had not such problems.
      However, in the new project i did change the default language to german and installed english as the second language. 
      Might there be any conflicts with the language support modules?
      Hints, tips or solutions are highly appreciated 🙂
    • By pwFoo
      I try to add page-edit-own and page-delete-own permissions, but it's strange...
      If a add the custom permissions it looks like both are children of page-edit respectively page-delete. I played with added / revoked permissions, but I can't get it work, that a user of a role just can delete own content.
      First the user can't delete any content and now the user can delete own and foreign pages 🤪
      Is there a tutorial to learn more about the PW permissions?
      Or do I have to rename the permissions to page-own-edit and page-own-delete to be independent from page-edit and page-delete?
    • By pwFoo
      I tried to add a new page with $pages->add() with a custom method inside of a module. It looks like the method is called each page load (as should) and also if the backend page tree is loaded / refreshed (bug??)?
      public function create($template, $parent, Array $data = array(), $name = false) { $name = wire('sanitizer')->pageName($name ? $name : $data['title']); $obj = wire('pages')->add($template, $parent, $name, $data); return $obj; } called inside of home page ("/"):
      $api->create('basic-page', '/', array('title' => 'ApiCreatedTest1')); Works fine. Load it twice (create duplicate page) results in an error.
      But without "/" requested the page gets created if I view the backend?!
      Checked it by delete the page, check the page tree again and it looks fine. But if I reload the backend page the page appears again. So backend page tree load executes the home page ("/") and create the page?
      Is it a bug or I'm missing anything...?
    • By cehlo
      Hello everyone,
      I am starting to creating some hooks, but I am little bit new to this part of Processwire so please help 🙂. 
      My problem: 
      I have multiple fields in one template - Page Reference - I activated autocomplete and also possibility of creating new pages from the fields. 
      Is it possible to add additional values to newly created page via Page Reference field? 
      For example: I know that the name of Page reference field is called for example car - I am searching with autocomplete for example for Mercedez, it is not created, so I know that newly created page via this field will have value in category field - cars,...
      I think I should addHookbefore to InputfieldPage::processInputAddPages, am I on the right track? Any assumptions? 
      Thanks for help.
  • Create New...