1

I’m creating an extension that on installation, programmatically creates a channel and fields for that channel.

One of the fields I’d like to create is a Grid field to store PDF documents against an entry. But reading through ExpressionEngine’s documentation on the channel fields API there’s no mention of Grid, and I can’t find anywhere that specifies the options for creating a Grid field.

What options do I need to specify? So far I have this:

array(
    'field_name' => 'floor_plans',
    'field_label' => 'Floor Plans',
    'field_type' => 'grid',
    'field_order' => 30,
    'field_instructions' => 'The floor plans for the property',
    'field_required' => 'n',
    'field_search' => 'n',
    'field_is_hidden' => 'n',
),

This creates the Grid field, but there are no inputs to enter data.

1

Have you had a look at the Grid Model that is in the system folder? Specifically /system/expressionengine/models/grid_model.php

This model has methods for creating new Grid Fields (create_field on line 140), add and update columns (save_col_settings on lone 235) and saving data into the grid (save_field_data on line 689). Is this the API you are looking for?

Surfice to say, this Model shows you the code to create grids, columns and data. With a little searching you could probably find the code that calls these model functions for examples on how to use (appologies I don't have a test enviroment to post actual code for you atm) I suspect in the /system/expressionengine/fieldtypes/ft.grid.php is a good place to start for this. Another place is the apply_settings function on line 606 of /system/expressionengine/fieldtypes/libraries/Grid_lib.php

In order to load and use the model, all you need to do in your model is :

//load the Grid Model
ee()->load->model('grid_model');

//create a new Grid field (PSEUDO)
$settings = array(
    'settings_for_grid' => true
);
$new_field = ee()->grid_model->create_field($settings, 'grid');

//create a column on the new grid
$col_id = 1;
$col_settings = array(
    'settings_for_column' => true
);
$col_ids[] = ee()->grid_model->save_col_settings($col_settings, $col_id = 1, 'grid');

Seems straight forward enougth, although determining your settings (for the columns look to like 645 ;) ) might be a pain it should work, after all this is how the admin CP creates the fields!

Let us know how you get on mate!

| improve this answer | |
0

I've been sitting back waiting for an API of some kind to show up, but I haven't been able to find anything. You have created the grid field, but it doesn't have any columns. I believe you are going to have to manipulate the exp_grid_columns table manually.

$field_id = 0; //THE FIELD ID RETURNED BY THE INSERT ABOVE

$grid_column = array(
    'field_id' => $field_id,
    'content_type' => 'channel',
    'col_order' => 0,          //order of the columns
    'col_type'  => '',         //field type name (text, textarea, assets)
    'col_label' => '',         //the name of the column
    'col_name' => '',          //the short_name of the column
    'col_instructions' => '',  //instructions for the column
    'col_required' => 'n',     //is this column required
    'col_search => 'n',        //is this column searchable
    'col_width' => 0,          //zero is auto
    'col_settings' => ''       //the settings for the field type
);
$query = ee()->db->insert("grid_columns", $grid_column);

What I would suggest, is to create a grid fieldtype the way you would like through the channel fields editor, and then look at the grid_columns table to get an idea of how it's set up.

| improve this answer | |

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