Skip to content

Metadata

Metadata is intended for storing application data such as fields, relationships, front-end controllers, views, panels, dashlets etc.

JSON Schema is available here. It provides autocompletion for IDEs (works by default in PhpStorm and VSCode).

In this article:

Reference

Accessing

Backend

The Metadata instance (of Espo\Core\Utils\Metadata class) is available as a container service.

Path to a needed parameter is specified with an array.

<?php
// entityDefs > Account > fields > type
$value = $metadata->get(['entityDefs', 'Account', 'fields', 'name', 'type']);
will return the string value "varchar".

<?php
$metadata->get(['entityDefs', 'Account', 'fields']);
will return an associative array with definitions of all fields.

Frontend

Metadata object is accessible from all view objects by method #getMetadata. It works the same way as the backend's one.

this.getMetadata().get(['entityDefs', 'Account', 'fields', 'name', 'type']);

How it's stored

Metadata is stored in JSON files that can be located in different places:

  • application/Espo/Resources/metadata – core;
  • application/Espo/Modules/{ModuleName}/Resources/metadata – internal modules;
  • custom/Espo/Modules/{ModuleName}/Resources/metadata – custom modules;
  • custom/Espo/Custom/Resources/metadata – instance specific customizations, customizations made via the admin UI are stored here.

When you access data by a path clientDefs.Account.views.edit the first lexeme clientDefs corresponds to a dir name, the second Account – to a file name Account.json. All the following lexemes correspond to a path in the JSON object.

{
    "views": {
        "edit": "crm:views/account/views/edit" 
    }
}

All JSON files from these directories get merged recursively into a single file and stored in an application cache.

Extending

Since metadata is merged recursively, you can easily redefine JSON objects and arrays in the custom directory.

You can append values to existing arrays by using the __APPEND__ string as the first element of an array. This will preserve array values rather than override them.

custom/Espo/Custom/Resources/metadata/entityDefs/Account.json:

{
    "fields": {
        "employeeCount": {
          "type": "int"
        },
        "type": {
            "options": [
                "__APPEND__",
                "Dealer",
                "Lawyer"
            ]
        }
    }
}