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:-


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

1 Answer 1


Apply this patch Jeremy -> this is fixed in the D10 version of CiviCRM Entity, but the D9 version requires this backport by Pradeep:

root@46c1218044c0:/var/www/drupal/web/sites/default/modules/civicrm_entity# curl -L https://patch-diff.githubusercontent.com/raw/eileenmcnaughton/civicrm_entity/pull/449.diff --output 449.diff

root@46c1218044c0:/var/www/drupal/web/sites/default/modules/civicrm_entity# cat 449.diff |patch -p1 --dry-run
checking file civicrm_entity.module
root@46c1218044c0:/var/www/drupal/web/sites/default/modules/civicrm_entity# cat 449.diff |patch -p1          
patching file civicrm_entity.module
  • 1
  • Thank you so much for getting back to me. I should have mentioned I'm running Drupal 10.1.6. Ive added my setup to the bottom of my post above Is any patch still applicable to me?
    – Jeremy M
    Commented Nov 16, 2023 at 9:47
  • As a temporary fix I've managed to use API3. see example above.
    – Jeremy M
    Commented Nov 16, 2023 at 11:06
  • 1
    Yes I think you do - the commit that fixed the issue -> github.com/eileenmcnaughton/civicrm_entity/pull/451/files was merged October 03 so prior to the 4.0.0-alpha6 (August 07) and 4.0.0-alpha7 (August 15) tags Commented Nov 16, 2023 at 17:53
  • thank you. i've completed this project now, but when I revisit i can try that or check for main branch updates
    – Jeremy M
    Commented Nov 21, 2023 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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