pogidude Posted August 15, 2013 Posted August 15, 2013 I'd like to add an existing field to a template via api but I'd like to change some attributes first. Equivalent action is if I were using gui, I would edit the template and in the list of fields (asmselect) I'd click on the field which would trigger a modal window where I can then edit the field's label and description. How do I do that using api?
ryan Posted August 15, 2013 Posted August 15, 2013 It sounds like you want to change the field attributes just for the context of a single template, is this correct? If so, look into $fields->saveFieldgroupContext().
pogidude Posted August 15, 2013 Author Posted August 15, 2013 Thanks for pointing me in the right direction Ryan. Although it took a bit of figuring tinkering around since I initially got this error "Field must be in fieldgroup context before its context can be saved" for those passing through this thread, finally got the following code working correctly: //add the field to the template first $mytemplate = wire('templates')->get('mytemplate'); $mytemplate->fieldgroup-add('roles'); $mytemplate->fieldgroup->save(); //-- we now *override* the default label and description to suit our template --// $field = $mytemplate->fieldgroup->getField('roles', true); //!IMPORTANT: set 2nd parameter to true to get the field in the context of the fieldgroup. //note: I don't really understand what "in the context of.." means though $field->label = 'Select roles'; $field->description = 'Select the roles that should receive notifications.'; //now save! wire('fields')->saveFieldgroupContext($field, $template->fieldgroup); For those who want to go through core, the hints are in wire/modules/Process/ProcessField/ProcessField.module 4
Martijn Geerts Posted August 15, 2013 Posted August 15, 2013 The context in your case is "mytemplate" as I understand correctly. Because multiple templates can share 1 fieldgroup. 1
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