Jump to content

Hook on Templates::save and get affected Template


LuisM
 Share

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?

Link to comment
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
        }
    ]
}

 

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

×
×
  • Create New...