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?

1 Answer 1


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.

  • 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.
    – Sleewok
    Feb 20, 2018 at 13:35
  • 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).
    – Sleewok
    Feb 20, 2018 at 20:22
  • To react to custom fields being updated, use hook_civicrm_custom
    – Aidan
    Feb 21, 2018 at 11:39

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.