Creating a CiviCRM contact using "Chaining" for a group works, but not with Tags (EntityTag), any idea why?
If I remove the chain using EntityTag the contact is created.
here is the error:
Error: Cannot use object of type CRM_Core_BAO_EntityTag as array in civicrm_entity_civicrm_post() (line 551 of modules/contrib/civicrm_entity/civicrm_entity.module).
$civi_fields = [
'values' => [
'contact_type' => 'Individual',
'first_name' => $input['first_name'],
'last_name' => $input['last_name'],
'employer_id' => $employer_id,
'job_title' => $input['job_title'],
],
'chain' => [ // Create these entities using the Contacts '$id'
'phone' => ['Phone', 'create', ['values' => ['contact_id' => '$id', 'phone' => $input['phone']]]],
'email' => ['Email', 'create', ['values' => ['contact_id' => '$id', 'email' => $input['mail']]]],
'tag' => ['EntityTag', 'create', ['values' => ['entity_table' => 'civicrm_contact', 'entity_id' => '$id', 'tag_id' => '4']]],
],
'checkPermissions' => false, // Or API will throw a permission exception
];
// --- Create CiviCRM Contact ---
$civi_result = civicrm_api4('Contact', 'create', $civi_fields);
I also tried removing the chain to entity_id and creating that entity separately, but got the same error:-
// --- Create CiviCRM Tags linked to the contact ---
$civi_fields = [
'values' => [
'entity_table' => 'civicrm_contact', // Specify the contact type.
'entity_id' => $civi_result[0]['id'],
'tag_id' => '4',
],
'checkPermissions' => false, // Or API will throw a permission exception
];
// Create CiviCRM Tag
$civi_result = civicrm_api4('EntityTag', 'create', $civi_fields);
My Setup:-
Drupal 10.1.6 with Enabled Drupal "civi" modules:-
CiviCRM Core (civicrm)
CiviCRM Theme (civicrmtheme)
CiviCRM Entity (civicrm_entity) 4.0.0-alpha6
CiviGroup Roles Sync (civicrm_group_roles) 1.3.2
PHP 8.1.23 CiviCRM + extensions are up to date and status is good:-
Extensions
All extensions are up-to-date:
AuthX: Version 5.67.0
CiviEvent: Version 5.67.0
CiviMail: Version 5.67.0
CiviMember: Version 5.67.0
CiviReport: Version 5.67.0
CKEditor4: Version 5.67.0
Contribution cancel actions: Version 5.67.0
Custom search framework: Version 5.67.0
Event Cart: Version 5.67.0
Financial ACLs: Version 5.67.0
FlexMailer: Version 5.67.0
FormBuilder: Version 5.67.0
Form Core: Version 5.67.0
Mosaico: Version 3.3.1697392242
reCAPTCHA: Version 5.67.0
SearchKit: Version 5.67.0
Sequential credit notes: Version 5.67.0
SparkPost integration: Version 1.6
Theme: Greenwich: Version 5.67.0
As a temporary fix, I'm using API3:
$civi_result = civicrm_api3('EntityTag', 'create', array(
'entity_table' => 'civicrm_contact',
'entity_id' => ($civi_result[0]['id']),
'tag_id' => [4,7],
));
Most examples found online show looping over civicrm_api3 and adding one tag at a time, but this does not work due a different civi error when adding more than one entry. It appears that in the latest version of either API or Drupal module civicrm_entity you must add multiple entries using an array.
CRM_Core_Exception: Unable to add tags in civicrm_api3() (line 138 of /var/www/html/vendor/civicrm/civicrm-core/api/api.php).