Answers.Body: <p>You might want to expand the question a bit and be more specific about your custom field.  What does it depend on? When should it be updated?</p>
  
  <p>To the general question:</p>
  
  <p>A view-only custom field is just like any other custom field, except that you can't edit it via the GUI.  It exists in the database and is displayed just like a 'normal' custom field.</p>
  
  <p>Which hook you need depends on what should cause an update to the custom field value. If for example, it is based on the contact's name then you could use <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_post/" rel="nofollow noreferrer">hook_civicrm_post</a> so that your code gets run after the name is created or updated.  </p>
  
  <p>For setting the value, you can use the Contact.create API as the field is on a contact.  Specify the existing contact id and the new value of the custom field - eg:</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'create', array(
    'sequential' =&gt; 1,
    'id' =&gt; 203,
    'custom_2' =&gt; "M",
  ));
  </code></pre>
  
  <p>Your code relates to writing the value of the custom field to the database, not the display of it.</p>
  
  <p>[Update]
  Here's a quick demo:</p>
  
  <ul>
  <li>create a new custom field group for contacts</li>
  <li>add an Integer field 'Set me'</li>
  <li>add a view-only Integer field 'Doubled'</li>
  </ul>
  
  <p>Put the following in an extension and adjust the groupid and fieldids to match your values:</p>
  
  <pre><code>function viewonlydemo_civicrm_custom($op, $groupID, $entityID, &amp;$params) {
    $groupid_to_watch = 15;
    $fieldid_to_watch = 31; // 'Set me' field
    $fieldid_to_set = 32; // 'Doubled' field
    if ($groupID == $groupid_to_watch) {
      foreach ($params as $cf) {
        if ($cf['custom_field_id'] == $fieldid_to_watch) {
          civicrm_api3('Contact', 'create', array(
            'id' =&gt; $entityID,
            'custom_' . $fieldid_to_set =&gt; $cf['value'] ? $cf['value'] * 2 : "",
          ));
        }
      }
    }
  }
  </code></pre>
  
  <p>When you change 'Set me' you should see 'Doubled' updated to be twice the value of 'Set me'.</p>
  
  <p>Be aware that the hook will be called when you update 'Set me', and also by the API call in the hook so you need to be careful not to get stuck in a loop. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-20T13:35:32.723Z
Answers.Text: Your explanation is extremely helpful. I obviously did not understand how "view only" functions. Now it make a lot more sense to me. In this case I need to update the field when another custom field is updated. For example, I have a contact reference field. I want to get a specific group that the referenced contact is within and set that to be the field value.
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-20T20:22:45.277Z
Answers.Text: Can you give me a little more direction setting the value of the field when another custom field is updated? I'm trying to get post working, but it isn't running when I do an inline edit of the custom field (I'm probably doing something wrong).
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-21T11:39:13.060Z
Answers.Text: To react to custom fields being updated, use `hook_civicrm_custom`
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-19T17:15:57.077Z
Answers.LastActivityDate: 2018-02-21T11:38:12.900Z
Answers.LastEditDate: 2018-02-21T11:38:12.900Z
Answers.UserDisplayName: Aidan
Body: <p>I am using Drupal and I have an extensions that I am running this code on.</p>
  
  <p>I've seen several threads attempting to help with this. I haven't found any that really make it clear. From what I understand I set a hook, then update the value. </p>
  
  <p>I have a custom field that I have set to "view only". This particular field is on a contact. When the contact is viewed I want the text I have generated to display for that field.</p>
  
  <p>How do you actually do this? What hook? How do I update the displayed value? What specific API call?</p>
  
Tags: <custom-fields><hooks>
Title: How do you set the displayed text of a custom field ("view-only")?
ThreadText: # How do you set the displayed text of a custom field ("view-only")?
  
  (Asked by: username_0 on 2018-02-19)
  
  I am using Drupal and I have an extensions that I am running this code on.
  
  I've seen several threads attempting to help with this. I haven't found any that really make it clear. From what I understand I set a hook, then update the value.
  
  I have a custom field that I have set to "view only". This particular field is on a contact. When the contact is viewed I want the text I have generated to display for that field.
  
  How do you actually do this? What hook? How do I update the displayed value? What specific API call?
  
  **Tags:** custom-fields, hooks
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-02-19. Score: 1 | Accepted answer
  
  You might want to expand the question a bit and be more specific about your custom field.  What does it depend on? When should it be updated?
  To the general question:
  A view-only custom field is just like any other custom field, except that you can't edit it via the GUI.  It exists in the database and is displayed just like a 'normal' custom field.
  Which hook you need depends on what should cause an update to the custom field value. If for example, it is based on the contact's name then you could use hook_civicrm_post so that your code gets run after the name is created or updated.  
  For setting the value, you can use the Contact.create API as the field is on a contact.  Specify the existing contact id and the new value of the custom field - eg:
  $result = civicrm_api3('Contact', 'create', array(
    'sequential' => 1,
    'id' => 203,
    'custom_2' => "M",
  ));
  
  Your code relates to writing the value of the custom field to the database, not the display of it.
  [Update]
  Here's a quick demo:
  
  create a new custom field group for contacts
  add an Integer field 'Set me'
  add a view-only Integer field 'Doubled'
  
  Put the following in an extension and adjust the groupid and fieldids to match your values:
  function viewonlydemo_civicrm_custom($op, $groupID, $entityID, &$params) {
    $groupid_to_watch = 15;
    $fieldid_to_watch = 31; // 'Set me' field
    $fieldid_to_set = 32; // 'Doubled' field
    if ($groupID == $groupid_to_watch) {
      foreach ($params as $cf) {
        if ($cf['custom_field_id'] == $fieldid_to_watch) {
          civicrm_api3('Contact', 'create', array(
            'id' => $entityID,
            'custom_' . $fieldid_to_set => $cf['value'] ? $cf['value'] * 2 : "",
          ));
        }
      }
    }
  }
  
  When you change 'Set me' you should see 'Doubled' updated to be twice the value of 'Set me'.
  Be aware that the hook will be called when you update 'Set me', and also by the API call in the hook so you need to be careful not to get stuck in a loop.
  
  > **Comments:**
  > **username_0**: Your explanation is extremely helpful. I obviously did not understand how "view only" functions. Now it make a lot more sense to me. In this case I need to update the field when another custom field is updated. For example, I have a contact reference field. I want to get a specific group that the referenced contact is within and set that to be the field value.  
  > **username_0**: Can you give me a little more direction setting the value of the field when another custom field is updated? I'm trying to get post working, but it isn't running when I do an inline edit of the custom field (I'm probably doing something wrong).  
  > **username_1**: To react to custom fields being updated, use `hook_civicrm_custom`  
  
  ---
  
  