0

I'm trying to add a new field to a group using the API but it doesn't work (according to the docs it is supposed to be a key).

I have tried running the example code but the 'group_id' field is ignored when I add it to the field object sent to the update_field method:

        ee()->load->library('api');
        ee()->legacy_api->instantiate('channel_fields');
        $field_data = array(
            'site_id' => 1,
            'group_id' => 1,
            'field_name' => 'aaaa',
            'field_label' => 'AAAA',
            'field_type' => 'text',
            'field_order' => 10,
            'field_required' => 'y',
            'field_search' => 'y',
            'field_is_hidden' => 'n',
            'field_instructions' => '',
            'field_maxl' => 128,
            'text_field_fmt' => 'none',
            'text_field_show_fmt' => 'n',
            'text_field_text_direction' => 'ltr',
            'text_field_content_type' => 'all',
            'text_field_show_smileys' => 'n',
            'text_field_show_glossary' => 'n',
            'text_field_show_spellcheck' => 'n',
            'text_field_show_file_selector' => 'n',
        );

        ee()->api_channel_fields->update_field($field_data);

So I added a call to field_edit_vars prior to the update_field call as stated in the docs:

    $vars = ee()->api_channel_fields->field_edit_vars(1, FALSE);
    ee()->load->view('admin/field_edit', $vars, TRUE);

But I get 'Call to undefined method Api_channel_fields::field_edit_vars()'

I've also tried using the return value from the 'update_field' method, which according to the docs should be the new field id but it returns nothing.

How do I add the new field to a group?

1

As far as I know there is no way to do this using API. I think you should try with EE Model. You can easily create new field using the Model. And Model is most effective way to handle all these core things.

You can create new field using below Model code.

$field = ee('Model')->make('ChannelField');        
$field->site_id = 0;
$field->field_name = 'your_field_name';
$field->field_label = 'Your Field Label';
$field->field_type = 'text';
$field->field_pre_populate = 'n';                
$field->field_ta_rows = '6';
$field->field_maxl = '256';
$field->field_required = 'n';
$field->field_text_direction = 'ltr';
$field->field_search = 'n';
$field->field_is_hidden = 'n';
$field->field_fmt = 'xhtml';
$field->field_show_fmt = 'n';
$field->field_order = '10';
$field->field_content_type = 'any';
$field->field_settings = '';
$field->legacy_field_data = 'n';                
$field->save();

To create Select field :-

$field = ee('Model')->make('ChannelField');        
$field->site_id = 0;
$field->field_name = 'my_dropdown';
$field->field_label = 'My Dropdown';
$field->field_type = 'select';
$field->field_pre_populate = 'n';                
$field->field_ta_rows = '6';
$field->field_maxl = '';
$field->field_required = 'n';
$field->field_text_direction = 'ltr';
$field->field_search = 'n';
$field->field_is_hidden = 'n';
$field->field_fmt = 'xhtml';
$field->field_show_fmt = 'y';
$field->field_order = '10';
$field->field_content_type = 'any';
$field->field_settings = '';
$field->legacy_field_data = 'n';                
$field->save();

I hope this code will work for you. I have added code to create select field.

5
  • I appreciate you taking the time to answer, but when I tried adding '$field->group_id = 1;' it fails 'No such property:' – wot Apr 1 '19 at 19:52
  • 1
    @wot This is most likely because that isn't part of the a channel field model; you need modify a ChannelFieldGroup model to add fields to it to a group. In EE4+, channel fields are independent of field groups. – jrothafer Apr 2 '19 at 3:06
  • 1
    @wot In this Model query group_id will not pass because as jrothafer said, EE has defined separate model for group_id. – Harsh Barach Apr 2 '19 at 5:36
  • Thank you! Can you tell me how to populate a select field? – wot Apr 2 '19 at 19:40
  • 1
    @wot I have added code to populate select field. Please see my modified answer. – Harsh Barach Apr 3 '19 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.