Answers.Body: <p>Apply this patch Jeremy -&gt; this is fixed in the D10 version of CiviCRM Entity, but the D9 version requires this backport by Pradeep:</p>
  <pre><code>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
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-15T21:09:39.563Z
Answers.Text: https://github.com/eileenmcnaughton/civicrm_entity/pull/449/files
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-16T09:47:15.207Z
Answers.Text: 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?
Answers.UserDisplayName: Jeremy M
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-16T11:06:47.417Z
Answers.Text: As a temporary fix I've managed to use API3.
  see example above.
Answers.UserDisplayName: Jeremy M
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-16T17:53:43.280Z
Answers.Text: Yes I think you do - the commit that fixed the issue -> https://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
Answers.UserDisplayName: KarinG - Semper IT
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-21T12:07:42.770Z
Answers.Text: thank you. i've completed this project now, but when I revisit i can try that or check for main branch updates
Answers.UserDisplayName: Jeremy M
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-21T16:02:55.253Z
Answers.Text: I've asked them to issue a new release for CE that includes the latest patch. If a site uses _post hooks (e.g. they have CiviRules) that do something with Tags - then the site will need PR 451.
Answers.UserDisplayName: KarinG - Semper IT
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-15T20:21:14.140Z
Answers.LastActivityDate: 2023-11-15T20:21:14.140Z
Answers.UserDisplayName: KarinG - Semper IT
Body: <p>Creating a CiviCRM contact using &quot;Chaining&quot; for a group works, but not with Tags (EntityTag), any idea why?</p>
  <p>If I remove the chain using EntityTag the contact is created.</p>
  <p>here is the error:</p>
  <blockquote>
  <p>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).</p>
  </blockquote>
  <pre><code>  $civi_fields = [ 
      'values' =&gt; [
        'contact_type'  =&gt; 'Individual', 
        'first_name'    =&gt; $input['first_name'],
        'last_name'     =&gt; $input['last_name'], 
        'employer_id' =&gt; $employer_id,
        'job_title' =&gt; $input['job_title'],
      ],
      'chain' =&gt; [ // Create these entities using the Contacts '$id'
        'phone' =&gt; ['Phone', 'create', ['values' =&gt; ['contact_id' =&gt; '$id', 'phone' =&gt; $input['phone']]]],
        'email' =&gt; ['Email', 'create', ['values' =&gt; ['contact_id' =&gt; '$id', 'email' =&gt; $input['mail']]]],
        'tag' =&gt; ['EntityTag', 'create', ['values' =&gt; ['entity_table' =&gt; 'civicrm_contact', 'entity_id' =&gt; '$id', 'tag_id' =&gt; '4']]],
      ],
      'checkPermissions' =&gt; false, // Or API will throw a permission exception
    ];
  
    // --- Create CiviCRM Contact ---
    $civi_result = civicrm_api4('Contact', 'create', $civi_fields);
  
  </code></pre>
  <p>I also tried removing the chain to entity_id and creating that entity separately, but got the same error:-</p>
  <pre><code>  // --- Create CiviCRM Tags linked to the contact ---
    $civi_fields = [ 
      'values' =&gt; [
        'entity_table'  =&gt; 'civicrm_contact', // Specify the contact type.
        'entity_id'     =&gt; $civi_result[0]['id'],
        'tag_id'        =&gt; '4',
      ],
      'checkPermissions' =&gt; false, // Or API will throw a permission exception
    ];
  
    // Create CiviCRM Tag
    $civi_result = civicrm_api4('EntityTag', 'create', $civi_fields);
  </code></pre>
  <p>My Setup:-</p>
  <p>Drupal 10.1.6 with Enabled Drupal &quot;civi&quot; modules:-</p>
  <pre><code>CiviCRM Core (civicrm)               
  CiviCRM Theme (civicrmtheme)
  CiviCRM Entity (civicrm_entity)                             4.0.0-alpha6
  CiviGroup Roles Sync (civicrm_group_roles)        1.3.2
  </code></pre>
  <p>PHP 8.1.23
  CiviCRM + extensions are up to date and status is good:-</p>
  <p>Extensions</p>
  <pre><code>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
  </code></pre>
  <p>As a temporary fix, I'm using API3:</p>
  <pre><code>      $civi_result = civicrm_api3('EntityTag', 'create', array(
          'entity_table'  =&gt; 'civicrm_contact', 
          'entity_id'     =&gt; ($civi_result[0]['id']),
          'tag_id'        =&gt; [4,7],
        ));
  </code></pre>
  <p>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.</p>
  <p>CRM_Core_Exception: Unable to add tags in civicrm_api3() (line 138 of /var/www/html/vendor/civicrm/civicrm-core/api/api.php).</p>
  
Tags: <tags><apiv4>
Title: civicrm_api4: How do I create a Contact with Tags (EntityTag)?
ThreadText: # civicrm_api4: How do I create a Contact with Tags (EntityTag)?
  
  (Asked by: username_0 on 2023-11-15)
  
  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).
  
  **Tags:** tags, apiv4
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-11-15. Score: 2
  
  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
  
  > **Comments:**
  > **username_2**: https://github.com/eileenmcnaughton/civicrm_entity/pull/449/files  
  > **username_0**: 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?  
  > **username_0**: As a temporary fix I've managed to use API3. see example above.  
  > **username_1**: Yes I think you do - the commit that fixed the issue -> https://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  
  > **username_0**: thank you. i've completed this project now, but when I revisit i can try that or check for main branch updates  
  > **username_1**: I've asked them to issue a new release for CE that includes the latest patch. If a site uses _post hooks (e.g. they have CiviRules) that do something with Tags - then the site will need PR 451.  
  
  ---
  
  