Jump to content

Use existing field but change label and description


pogidude
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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

  • Like 4
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...