Answers.Body: <p>You can use standard Drupal access hooks and place whatever custom logic you like.
  <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_access/9" rel="nofollow noreferrer">https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_access/9</a></p>
  <p>Here is a possible way. Add this hook implementation in a custom module. You can have any custom logic there.</p>
  <p>In this example, I look up the contact id via uf_match record for the user, and see if the contact id being viewed matches the user's contact id. If so, it should grant access.</p>
  <p>This is off the cuff, but should point in the right direction</p>
  <p>You'll need the proper 'use' statements at the top of the file for EntityInterface, AccountInterface, and AccessResult.</p>
  <pre><code>function YOUR_MODULE_civicrm_contact_access(EntityInterface $entity, $operation, AccountInterface $account) {
    if ($operation == 'update' &amp;&amp; $account-&gt;id()) {
      \Drupal::service('civicrm')-&gt;initialize();
      try {
        $uf_match_result = civicrm_api3('UfMatch', 'get', [
          'sequential' =&gt; TRUE,
          'uf_id' =&gt; $account-&gt;id(),
        ]);
        if (!empty($uf_match_result['values'][0]['contact_id'])) {
          return AccessResult::allowedIf($entity-&gt;id() == $uf_match_result['values'][0]['contact_id']);
        }
      }
      catch (Exception $e) {
  
      }
    }
    return AccessResult::neutral();
  }
  </code></pre>
  <p>It would also be possible to check if the $account has a permission, like 'edit my contact' in the allowedIf() conditional</p>
  <p>$account-&gt;hasPermission('edit my contact');</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-03T18:48:20.987Z
Answers.Text: This is good but `CRM_Core_Session::singleton()->getLoggedInContactID();` may be more forward-compatible than API3?  Though either way is valid IMO.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-04T19:09:06.163Z
Answers.Text: Yes could be a better way to find a user's linked contact id, time to stop using APIv3 :) APIv4 maybe fine too.
Answers.UserDisplayName: jackrabbithanna
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-03T16:25:08.907Z
Answers.LastActivityDate: 2023-01-03T16:56:26.903Z
Answers.LastEditDate: 2023-01-03T16:56:26.903Z
Answers.UserDisplayName: jackrabbithanna
Body: <p>How to allow a specific Drupal role to modify its own content in the CiviCRM Contact form (civicrm-contact/%user)?
  I don't see any permissions like &quot;edit own content&quot; for type nodes in admin/people/permissions for the CiviCRM contact entity.
  Do you have a solution?
  Thanks</p>
  
Tags: <drupal><forms><civicrm-entity><roles><edit>
Title: How to enable a specific role to edit own content in CiviCRM Contact Form?
ThreadText: # How to enable a specific role to edit own content in CiviCRM Contact Form?
  
  (Asked by: username_0 on 2022-12-30)
  
  How to allow a specific Drupal role to modify its own content in the CiviCRM Contact form (civicrm-contact/%user)?
  I don't see any permissions like "edit own content" for type nodes in admin/people/permissions for the CiviCRM contact entity.
  Do you have a solution?
  Thanks
  
  **Tags:** drupal, forms, civicrm-entity, roles, edit
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-01-03. Score: 1
  
  You can use standard Drupal access hooks and place whatever custom logic you like.
  <https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_access/9>
  
  Here is a possible way. Add this hook implementation in a custom module. You can have any custom logic there.
  
  In this example, I look up the contact id via uf\_match record for the user, and see if the contact id being viewed matches the user's contact id. If so, it should grant access.
  
  This is off the cuff, but should point in the right direction
  
  You'll need the proper 'use' statements at the top of the file for EntityInterface, AccountInterface, and AccessResult.
  
  ```
  function YOUR_MODULE_civicrm_contact_access(EntityInterface $entity, $operation, AccountInterface $account) {
    if ($operation == 'update' && $account->id()) {
      \Drupal::service('civicrm')->initialize();
      try {
        $uf_match_result = civicrm_api3('UfMatch', 'get', [
          'sequential' => TRUE,
          'uf_id' => $account->id(),
        ]);
        if (!empty($uf_match_result['values'][0]['contact_id'])) {
          return AccessResult::allowedIf($entity->id() == $uf_match_result['values'][0]['contact_id']);
        }
      }
      catch (Exception $e) {
  
      }
    }
    return AccessResult::neutral();
  }
  
  ```
  
  It would also be possible to check if the $account has a permission, like 'edit my contact' in the allowedIf() conditional
  
  $account->hasPermission('edit my contact');
  
  > **Comments:**
  > **username_2**: This is good but `CRM_Core_Session::singleton()->getLoggedInContactID();` may be more forward-compatible than API3?  Though either way is valid IMO.  
  > **username_1**: Yes could be a better way to find a user's linked contact id, time to stop using APIv3 :) APIv4 maybe fine too.  
  
  ---
  
  