We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

paralipomena of EE documentation about developing extensions that manage models

Development and Programming

Alessandro Proglio's avatar
Alessandro Proglio
8 posts
5 months ago
Alessandro Proglio's avatar Alessandro Proglio

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.

       
Alessandro Proglio's avatar
Alessandro Proglio
8 posts
5 months ago
Alessandro Proglio's avatar Alessandro Proglio

// 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.

       
Yuri's avatar
Yuri
301 posts
about 5 months ago
Yuri's avatar Yuri

@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

       
Alessandro Proglio's avatar
Alessandro Proglio
8 posts
about 5 months ago
Alessandro Proglio's avatar Alessandro Proglio

Yes Yuri. It seems to me that a starting point to look at is the function update_field(array $field_data) of the class Api_channel_fields. It’s well documented and there are a lot of checks to prevent invalid entries.

       
Alessandro Proglio's avatar
Alessandro Proglio
8 posts
about 5 months ago
Alessandro Proglio's avatar Alessandro Proglio

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);
       
Yuri's avatar
Yuri
301 posts
about 5 months ago
Yuri's avatar Yuri

Would you be willing to open pull request to add your examples and/or notes to the docs? https://github.com/ExpressionEngine/ExpressionEngine-User-Guide/blob/7.dev/docs/development/models/channel-field.md

       
Alessandro Proglio's avatar
Alessandro Proglio
8 posts
about 5 months ago
Alessandro Proglio's avatar Alessandro Proglio

Yes, good idea. I rearrange the contents and give it a try.

👍 1
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.