Hello. I’m trying to follow the documentation to get into EE development, but there are gaps here and there that don’t allow me to create a simple extension with a channel and a group of fields. if I use the code examples in the documentation to create a simple text field,
private function create_field($nome_field)
{
$field = ee('Model')->make('ChannelField');
// Set required fields.
$field->site_id = ee()->config->item('site_id');
$field->field_name = $nome_field;
$field->field_label = $nome_field;
$field->field_type = 'text';
// Validate and Save.
$result = $field->validate();
if ($result->isValid())
{
$field->save();
}
}
I get this error:
SQLSTATE[HY000]: General error: 1364 Field 'field_list_items' doesn't have a default value:
INSERT INTO `exp_channel_fields` (`site_id`, `field_name`, `field_label`, `field_type`) VALUES (1, 'pensieri', 'pensieri', 'text')
If I add a default value to ‘field_list_items’ with this code:
$field->field_list_items = '';
I end up with this error:
SQLSTATE[HY000]: General error: 1364 Field 'field_order' doesn't have a default value:
INSERT INTO `exp_channel_fields` (`site_id`, `field_name`, `field_label`, `field_type`, `field_list_items`) VALUES (1, 'pensieri', 'pensieri', 'text', '')
If I add a value to ‘field_order’ with this code:
// field_order increment the last field order number
$ordernumber = 1 + ee('Model')->get('ChannelField')->order('field_order', 'DESC')->first()->field_order;
$field->field_order = $ordernumber;
there are no more errors. But trying to create a “number” field instead of a text field with a more generic function:
private function _create_field($field_label,$field_type)
{
$field = ee('Model')->make('ChannelField');
// Set required fields.
$field->site_id = ee()->config->item('site_id');
$field->field_label = $field_label;
$field_name = strtolower(str_replace(" ","_",$field_label));
$field->field_name = $field_name;
$field->field_type = $field_type;
$field->field_list_items = '';
// field_order increment the last field order number
$ordernumber = 1 + ee('Model')->get('ChannelField')->order('field_order', 'DESC')->first()->field_order;
$field->field_order = $ordernumber;
// Validate and Save.
$result = $field->validate();
if ($result->isValid())
{
$field->save();
//return $field->getId();
return $field->field_id;
}
}
i get errors complaining about undefined indexes of field_max_value, field_min_value and field_content_type, therefore I try to pass settings to the field_settings property:
$my_number_field_settings = array(
'field_min_value' => 1,
'field_max_value' => 12345678,
'field_step' => null,
'datalist_items' => null,
'field_content_type' => 'integer'
);
$my_field = $this->_create_field('pick a number','number',$my_number_field_settings);
private function _create_field($field_label,$field_type,$custom_settings)
{
$field = ee('Model')->make('ChannelField');
// Set required fields.
$field->site_id = ee()->config->item('site_id');
$field->field_label = $field_label;
$field_name = strtolower(str_replace(" ","_",$field_label));
$field->field_name = $field_name;
$field->field_type = $field_type;
$field->field_list_items = '';
// field_order increment the last field order number
$ordernumber = 1 + ee('Model')->get('ChannelField')->order('field_order', 'DESC')->first()->field_order;
$field->field_order = $ordernumber;
// field settings
$changing_settings = $field->getSettingsValues()['field_settings'];
foreach ($custom_settings as $s => $s_valore) {
$changing_settings[$s] = $s_valore;
}
$field->setProperty('field_settings', $changing_settings);
// Validate and Save.
$result = $field->validate();
if ($result->isValid())
{
$field->save();
//return $field->getId();
return $field->field_id;
}
}
finally it works.
// field settings can be changed without overwriting all of them. the unserialized data taken from the database field_settings is
Array
(
[field_min_value] => 1
[field_max_value] => 12345678
[field_step] =>
[datalist_items] =>
[field_content_type] => integer
)
I would like to continue these investigations with other types of field.
@Alessandro Proglio , field_list_items
and field_order
are definitely the gaps in the examples from documentation, thanks for pointing those out!
Different fields have different requirements, so it is expected that number field would need some extra properties - but again, I agree this should be in the documentation
here is an example with three different possibilities to populate the options for multi select fields or equivalent types of field.
//Checkboxes (the same way to set field options also applies to Multi Select, Radio Buttons, Select Dropdown and Selectable Buttons)
// below there is an explicative if/else/else cycle used only to show the three possibilities
$checkboxes_settings = array();
$checkboxes_properties = array();
$field_values_source = 'from-other-channel'; // 'value-labels' or 'manually' or 'from-other-channel'
if ($field_values_source == 'manually') {
//// manually populated list, to be written in the 'field_list_items' property
$checkboxes_properties = array(
'field_list_items' => "first\nsecond\nthird"
);
} elseif ($field_values_source == 'value-labels') {
//// value/label pairs, instead these go to the settings array
$checkboxes_settings = array(
'value_label_pairs' => array('primo' => 'first', 'secondo' => 'second')
);
} elseif ($field_values_source == 'from-other-channel') {
//// or populate from other channel field, these settings go to the properties array
$checkboxes_properties = array(
'field_pre_populate' => 'y', // default is 'n'
'field_pre_channel_id' => 4, //channel id
'field_pre_field_id' => 80, // field id
);
}
$Checkboxes_id = $this->_create_field('closed boxes','checkboxes',$checkboxes_settings,$checkboxes_properties);
Yes, good idea. I rearrange the contents and give it a try.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.