Bug #23417 See Comments

Channel Structure API Not Working

Version: 4.0.1 Reporter: anthony.pinskey

In my module I have it where it creates a field group, and then a channel that should get the field group attached to it. It creates the channel but never assigns the field group to it and has errors show when you go to edit the channel or create a new entry in the backend.

Warning
Invalid argument supplied for foreach()
ee/legacy/libraries/api/Api_channel_fields.php, line 775

Severity: E_WARNING
Warning
Invalid argument supplied for foreach()
ee/EllisLab/ExpressionEngine/Model/Channel/Display/DefaultChannelLayout.php, line 179

Severity: E_WARNING
Warning
Invalid argument supplied for foreach()
ee/EllisLab/ExpressionEngine/Model/Channel/ChannelEntry.php, line 687

Severity: E_WARNING
Warning
Invalid argument supplied for foreach()
ee/EllisLab/ExpressionEngine/Model/Channel/ChannelEntry.php, line 971

Severity: E_WARNING
Warning
Cannot modify header information - headers already sent by (output started at ee/legacy/core/Exceptions.php:111)
ee/EllisLab/ExpressionEngine/Boot/boot.common.php, line 491

Severity: E_WARNING

Here is the install method

/**
 * Installation Method
 *
 * @return  boolean
 */
public function install()
{
  // Module_name data for exp_modules table
  $data = array(
     'module_name' => 'Module_name',
     'module_version' => $this->version,
     'has_cp_backend' => 'y',
     'has_publish_fields' => 'y'
  );

  // Record module info to exp_modules
  ee()->db->insert('exp_modules', $data);

  // Load Channel Structure API
  ee()->load->library('api');
  ee()->legacy_api->instantiate('channel_fields');
  ee()->legacy_api->instantiate('channel_structure');

  // Create the field group
  ee()->db->insert(
      'exp_field_groups',
      array(
          'site_id'  => 1,
          'group_name'  => 'Module_name'
      )
  );

  // Store the field group id
  $field_group_id = ee()->db->insert_id();

  // Create Module_name channel
  $data = array(
      'channel_title' => 'Module_name',
      'channel_name'  => 'module_name',
      'field_group' => $field_group_id,
      'channel_url' => '',
      'status_group'  => 1
  );

  // Create the channel
  $module_name_channel = ee()->api_channel_structure->create_channel($data);

  if ($module_name_channel === FALSE)
  {
      show_error('An Error Occurred Creating the Channel');
  }
  return TRUE;
}

I noticed it never creates the relationship between the field group and the channel in the exp_channel_field_groups_fields table.

Does this API still need updating for EE4 or is there something else wrong? It seems I followed the documentation properly to create a channel and assign a field group as seen on https://docs.expressionengine.com/latest/development/legacy/api/api_channel_structure.html

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases