Announcement

Collapse
No announcement yet.

entityDefs json Syntax for passing custom field parameter when extending field view

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • entityDefs json Syntax for passing custom field parameter when extending field view

    Hello,

    This may seem like a basic question but how does one pass a custom field parameter from entityDefs so it can be read in the custom view?

    In the example below I have a field called 'procedureFile' in my entity def. I want to pass the parameter 'myCustomParam' into the custom view but its always showing the default value false in my code?

    Example:

    //custom/Espo/Custom/Resources/metadata/entityDefs/MyEntity.json

    Code:
    "procedureFile": {
    "type": "file",
    "accept": [
    ".pdf"
    ],
    "inlineEditDisabled": true,
    "maxFileSize": 5,
    "sourceList": [
    "Document"
    ],
    "isCustom": true,
    "myCustomParam":true,
    "view": "custom:views/fields/custom-file"
    }​

    //site/client/custom/src/views/fields/custom-file.js

    Code:
    define('custom:views/fields/custom-file', ['views/fields/file'], function (Dep) {
    
    return Dep.extend({
    
    myCustomParam: false,​
    
    setup: function () {
    
    Dep.prototype.setup.call(this);
    
    this.myCustomParam = this.options.myCustomParam || this.params.myCustomParam || this.myCustomParam;
    
    console.log(this.myCustomParam); // WHY IS THIS ALWAYS FALSE ??
    
    },​
    
    });
    
    });

  • #2
    Hi, It's not possible by design. Only parameters defined in metadata > fields > {type} > params are passed. You can always access any metadata directly from this.getMetadata() or from this.model.getFieldParam(this.name, 'myParam').

    Comment


    • czcpf
      czcpf commented
      Editing a comment
      Thanks. Looks like one could also do this.model.defs.fields[this.name]['myParam'].

  • #3
    Hello czcpf,

    The granular "class" used to display data in Espo is the field view class.

    The entity view is only a collection of field views plus buttons and layout data, so if you want to be able to set the value of a field's custom parameter in an entity metadata definition, you must first define the custom parameter in the field metadata definition.

    These are the steps that I would follow to implement your example

    1) Create a custom field metadata definition that includes the custom parameter:
    custom/Espo/Custom/Resources/metadata/fields/procedureFile.json
    Code:
    {
        "params": [
            {
                "name": "required",
                "type": "bool",
                "default": false
            },
            {
                "name": "sourceList",
                "type": "multiEnum",
                "view": "views/admin/field-manager/fields/source-list"
            },
            {
                "name": "maxFileSize",
                "type": "float",
                "tooltip": true,
                "min": 0
            },
            {
                "name": "accept",
                "type": "multiEnum",
                "noEmptyString": true,
                "allowCustomOptions": true,
                "options": [
                    "image/*",
                    "audio/*",
                    "video/*",
                    ".zip",
                    ".pdf",
                    ".odt",
                    ".ods",
                    ".odp",
                    ".docx",
                    ".xlsx",
                    ".pptx",
                    ".doc",
                    ".xls",
                    ".ppt",
                    ".rtf",
                    ".csv",
                    ".md",
                    ".txt"
                ],
                "tooltip": "fileAccept"
            },
            {
                "name": "audited",
                "type": "bool"
            },
            {
                "name": "myCustomParam",
                "type": "bool"
            }
        ],
        "actualFields": [
            "id"
        ],
        "notActualFields": [
            "name"
        ],
        "converterClassName": "Espo\\Core\\Utils\\Database\\Orm\\FieldConverters\\File",
        "validationList": [
            "required",
            "pattern"
        ],
        "mandatoryValidationList": [
            "pattern"
        ],
        "filter": true,
        "linkDefs": {
            "type": "belongsTo",
            "entity": "Attachment",
            "skipOrmDefs": true,
            "disabled": true
        },
        "personalData": true,
        "duplicatorClassName": "Espo\\Classes\\FieldDuplicators\\File",
        "view": "custom:views/fields/custom-file"
    }
    2) Create the custom field view "class" that will display and update the database data
    client/custom/src/views/fields/custom-file.js
    (Code copied from your example)
    Code:
    define('custom:views/fields/custom-file', ['views/fields/file'], function (Dep) {
    
        return Dep.extend({
    
            myCustomParam: false,​
    
            setup: function () {
    
                Dep.prototype.setup.call(this);
    
                this.myCustomParam = this.options.myCustomParam || this.params.myCustomParam || this.myCustomParam;
    
                console.log(this.myCustomParam);
            },​
    
        });
    
    });​
    3) Create the field parent entityDefs metadata file
    custom/Espo/Custom/Resources/metadata/entityDefs/MyEntity.json
    Code:
    " fields":
    "procedureFile": {
        "type": "procedureFile",
        "accept": [
            ".pdf"
        ],
        "inlineEditDisabled": true,
        "maxFileSize": 5,
        "sourceList": [
            "Document"
        ],
        "isCustom": true,
        "myCustomParam":true,
        "view": "custom:views/fields/custom-file"
    }​​
    Last edited by telecastg; 07-29-2023, 06:18 AM.

    Comment


    • czcpf
      czcpf commented
      Editing a comment
      Thank you for taking the time to detail out this example.

    • telecastg
      telecastg commented
      Editing a comment
      You are very welcome. I appreciate that you also post your solutions in detail.

  • #4
    Thanks telecastg , I was missing the field metadata definition. Is a built in field metadata definition extendable? Meaning, could I leave the field type "file" in the example above and create a file

    custom/Espo/Custom/Resources/metadata/fields/file.json​​
    Code:
    {
        "params": [
            "__APPEND__",
            {
                "name": "myCustomParam",
                "type": "bool"
            }
    }
    Last edited by czcpf; 07-31-2023, 04:11 PM.

    Comment


    • telecastg
      telecastg commented
      Editing a comment
      So were you able to make it work with __APPEND__ instead ?

    • czcpf
      czcpf commented
      Editing a comment
      Yes. It worked fine with "___APPEND___". This is useful in situations where you just want to configure a field view but want to pass parameters to it directly from entity defs to do front-end logic stuff in the custom view depending on the parameter.

    • telecastg
      telecastg commented
      Editing a comment
      Great thanks !

  • #5
    For an even simpler implementation you can use yuri's suggestion and do this with no need to set anything in custom/Espo/Custom/Resources/metadata/fields/file.json. For example, if you don't want to include the parameter in all implementations of file.json...


    custom/Espo/Custom/Resources/metadata/entityDefs/MyEntity.json

    Code:
    "fields":
    "procedureFile": {
    "type": "procedureFile",
    "accept": [
    ".pdf"
    ],
    "inlineEditDisabled": true,
    "maxFileSize": 5,
    "sourceList": [
    "Document"
    ],
    "isCustom": true,
    "myCustomParam":true,
    "view": "custom:views/fields/custom-file"
    }​​​
    Create the custom field view "class" that will display and update the database data
    client/custom/src/views/fields/custom-file.js

    Code:
    define('custom:views/fields/custom-file', ['views/fields/file'], function (Dep) {
    
    return Dep.extend({
    
    myCustomParam: false,​
    
    setup: function () {
    
    Dep.prototype.setup.call(this);
    
    this.myCustomParam =
    this.options.myCustomParam ||
    this.params.myCustomParam ||
    this.model.getFieldParam(this.name, 'myCustomParam') ||
    this.model.defs.fields[this.name].myCustomParam ||
    this.myCustomParam;​
    console.log(this.myCustomParam);
    },​
    
    });
    
    });​​







    Comment


    • #6
      For an even simpler implementation you can use yuri's suggestion and do this with no need to set anything in custom/Espo/Custom/Resources/metadata/fields/file.json. For example, if you don't want to include the parameter in all implementations of file.json...


      custom/Espo/Custom/Resources/metadata/entityDefs/MyEntity.json

      Code:
      "fields":
      "procedureFile": {
      "type": "procedureFile",
      "accept": [
      ".pdf"
      ],
      "inlineEditDisabled": true,
      "maxFileSize": 5,
      "sourceList": [
      "Document"
      ],
      "isCustom": true,
      "myCustomParam":true,
      "view": "custom:views/fields/custom-file"
      }​​​
      Create the custom field view "class" that will display and update the database data
      client/custom/src/views/fields/custom-file.js

      Code:
      define('custom:views/fields/custom-file', ['views/fields/file'], function (Dep) {
      
      return Dep.extend({
      
      myCustomParam: false,​
      
      setup: function () {
      
      Dep.prototype.setup.call(this);
      
      this.myCustomParam =
      this.options.myCustomParam ||
      this.params.myCustomParam ||
      this.model.getFieldParam(this.name, 'myCustomParam') ||
      this.model.defs.fields[this.name].myCustomParam ||
      this.myCustomParam;​
      console.log(this.myCustomParam);
      },​
      
      });
      
      });​​

      Comment

      Working...
      X