# Define extension fields

Extension fields are extra fields that a deployment adds to some Care records, such as patients, accounts and supply deliveries. You describe them on the backend with JSON Schema, and the frontend shows the fields on the matching screens.

Staff see these fields as additional fields. For the user view, see [Additional fields](https://docs.ohc.network/concepts/platform/additional-fields.md).

There is no screen for defining them. Use one of these two ways:

* Set `CORE_EXTENSIONS_<RESOURCE>_WRITE`, and if needed `_READ` and `_RETRIEVE`, in the backend environment.
* Install a backend plug that registers a `PlugExtension`.

## Resources that take extension fields

Nine resources take extension fields, and each of their models stores the values in an `extensions` JSON field. The Care 3.1 frontend knows only seven of them: it does not know facility and resource\_request.

| Resource | Variable prefix | Screens in 3.1 |
| - | - | - |
| patient | `CORE_EXTENSIONS_PATIENT_` | Registration and edit form, patient profile, appointment token card (printed only), appointment print |
| account | `CORE_EXTENSIONS_ACCOUNT_` | Account sheet |
| payment\_reconciliation | `CORE_EXTENSIONS_PAYMENT_RECONCILIATION_` | Payment sheet |
| supply\_delivery\_order | `CORE_EXTENSIONS_SUPPLY_DELIVERY_ORDER_` | Delivery order form, delivery order page, delivery order print |
| supply\_delivery | `CORE_EXTENSIONS_SUPPLY_DELIVERY_` | Add supply delivery form, supply deliveries table |
| encounter | `CORE_EXTENSIONS_ENCOUNTER_` | None |
| product | `CORE_EXTENSIONS_PRODUCT_` | None |
| facility | `CORE_EXTENSIONS_FACILITY_` | None |
| resource\_request | `CORE_EXTENSIONS_RESOURCE_REQUEST_` | None |

## Set the environment variables

Care registers a core extension for every resource, so every resource can get fields from the environment.

The core extension reads its schemas from `CORE_EXTENSIONS_<RESOURCE>_<ACTION>`, with the resource name in upper case and ACTION one of WRITE, READ or RETRIEVE. For example, `CORE_EXTENSIONS_PATIENT_WRITE`.

| Variable | What it is for | If it is not set |
| - | - | - |
| `..._WRITE` | Care validates submitted values against it. The patient registration and edit form builds its fields from it. | The schema is empty. |
| `..._READ` | Published as `read_schema`. The supply deliveries table uses it. | Care uses the WRITE schema. |
| `..._RETRIEVE` | Published as `retrieve_schema`. The patient profile and the delivery order page use it. | Care uses the READ schema, and so the WRITE schema if READ is not set. |

The schemas apply to the whole instance. Nothing in them is per facility.

warning

Each value must be valid JSON. If it is not, Care raises "Invalid JSON" each time it reads that schema. Check the JSON before you set the variable.

### Example

This minimal example is for illustration only. It adds one optional text field, `employer`, to patients. The field uses the textarea control and is left off the printed appointment token card and the appointment print.

```
CORE_EXTENSIONS_PATIENT_WRITE='{"type": "object", "properties": {"employer": {"type": "string", "x-ui": {"control": "textarea", "render_blacklist": ["appointment_print"]}}}}'
```

With `CORE_EXTENSIONS_PATIENT_READ` and `CORE_EXTENSIONS_PATIENT_RETRIEVE` not set, Care uses this schema for both. Care stores the value at `extensions.core.employer`.

## How values are validated and stored

The core extension validates submitted values against the WRITE schema with jsonschema. If the values do not match, Care rejects the request with "Invalid extensions".

Care stores values under the name of the extension that owns them: `extensions.core.<field>` for fields from the environment, and `extensions.<extension name>.<field>` for fields from a plug.

Top-level keys under `extensions` that match no registered extension are dropped without an error.

An update that sends `extensions: {}` does not erase stored values, because the backend skips fields that equal their default when it writes.

## Add UI hints with x-ui

A field can carry an `x-ui` object with hints for the frontend: `control`, `render_blacklist` and `placement.after`.

### control

`x-ui.control` accepts these values: page, section, grid, table, list, textbox, textarea, dropdown, radio, checkbox, switch, date, datetime and autocomplete.

| Values | What the frontend does |
| - | - |
| section, grid, table (for arrays), autocomplete, switch, radio, textarea, checkbox, date, datetime | Renders the field with that control |
| Any other value | Falls back to the JSON type. A field with `enum` or titled `oneOf` options is a drop-down, and a string is a text box. |
| page, list | No special rendering |

### render\_blacklist

`x-ui.render_blacklist` lists the contexts where the field must not show. A field with no list shows in every context. Fields with `const` and hidden fields ignore the list.

| Context | Screen |
| - | - |
| `registration` | Patient registration and edit form, at the end of section "2: Additional Details" (WRITE schema) |
| `patient_edit` | No screen uses it |
| `appointment_print` | Appointment token card (printed copy only) and appointment print ("Appointment Details") |
| `patient_summary` | Patient profile, section "Additional Details" (RETRIEVE schema) |
| `account_form` | Account sheet ("Create Account" and "Edit Account") |
| `payment_reconciliation_form` | Payment sheet ("Collect Payment" or "Record Credit Note") |
| `supply_delivery_order_form` | Delivery order form |
| `supply_delivery_order_summary` | Delivery order page details card, and delivery order print (RETRIEVE schema) |
| `supply_delivery_form` | Add supply delivery form, items table |
| `supply_delivery_table` | Supply deliveries table on the delivery order page (READ schema) |

caution

The patient form passes `registration` both when it registers a patient and when it edits one. If you hide a field from `registration`, it is hidden on edit too.

### placement.after

`x-ui.placement.after` places a field right after a named element of the screen. Anchors exist only for supply\_delivery: product, batch, expiry, category, requested\_quantity, pack\_size, pack\_quantity, quantity, date, sale, purchase, tax, discount, status and condition. Fields without placement stay in the screen's default block.

## Where the fields show in 3.1

| Resource | Screen | Where the fields show |
| - | - | - |
| patient | Patient registration and edit form | At the end of section "2: Additional Details" |
| patient | Patient profile | In a section headed "Additional Details", which holds only extension values and shows only when at least one value is filled |
| patient | Appointment token card | Only on the printed card, not on screen |
| patient | Appointment print ("Appointment Details") | As rows right after the "Patient" row |
| account | Account sheet ("Create Account" and "Edit Account") | At the end of the form, filled in on edit |
| payment\_reconciliation | Payment sheet ("Collect Payment" or "Record Credit Note") | At the end of the form, after "Notes" |
| supply\_delivery\_order | Delivery order form | After "Tags" |
| supply\_delivery\_order | Delivery order page | Filled values, with their labels, in the details card |
| supply\_delivery\_order | Delivery order print | Filled values in the details block at the top, alternating between the left and right columns |
| supply\_delivery | Add supply delivery form | As extra columns in the items table, after their anchor column or at the end. Required fields have a red asterisk. |
| supply\_delivery | Supply deliveries table on the delivery order page | As extra columns, after their anchor or at the end. An empty value shows "-". |

These resources have extension fields but no screen in 3.1: encounter, product, facility and resource\_request. The product form always sends `extensions: {}`, which does not erase stored values. Medication returns also always send empty supply delivery extensions.

The facility and resource request APIs still take extension values on create and return them on list and detail.

Report templates can print patient and encounter extensions, as "Patient Extensions" and "Encounter Extensions".

## Add fields from a plug

A backend plug adds fields by subclassing `PlugExtension`, whose owner is "plug", and registering it with `ExtensionRegistry.register`. Registration needs a resource type and an extension name.

`PlugExtension` does no validation unless the plug overrides `validate`.

Care stores the plug's values under `extensions.<extension name>`.

For how to install a plug, see [Install apps](https://docs.ohc.network/deployment/configuration/install-apps.md#install-the-backend-part).

## Check what Care loaded

`GET /api/v1/extensions/` returns, for each resource, the registered extensions with their name, owner, version, `write_schema`, `read_schema` and `retrieve_schema`. It is read-only.

Any signed-in user can call it.

The frontend reads this list once and keeps it for 24 hours. Production builds also keep it in the browser's local storage, which is cleared at staff sign-in and when a new frontend version is deployed.

So after you change a schema, users see the new fields when they sign in again, or within 24 hours.

## Related

* [Additional fields](https://docs.ohc.network/concepts/platform/additional-fields.md)
* [Install apps](https://docs.ohc.network/deployment/configuration/install-apps.md)
* [Plugin system](https://docs.ohc.network/contributing/plugins.md)
