Limit some blocks in the repeater

Hello to all,
i have a repeater block where you can select 3 blocks:

  • Project
  • Text
  • Services

Is it possible to limit the Services block to 1 in the entire repeater?

For example, I would like to give the possibility to insert many project blocks and many text blocks but only one service block.

{
  "fields": [
{
  "type": "set",
  "label": "Project",
  "options": {
    "fields": [
      {
        "type": "select",
        "name": "layout",
        "label": "Layout",
        "options": {
          "options": {
            "wide": "Wide",
            "half": "Half"
          }
        },
        "default": "wide"
      },
      {
        "type": "collectionlink",
        "name": "project",
        "label": "Project",
        "options": {
          "link": "projects",
          "display": "project_name",
          "multiple": false
        }
      }
    ]
  }
},
{
  "type": "set",
  "label": "Text",
  "options": {
    "fields": [
      {
        "type": "select",
        "name": "style",
        "label": "Style",
        "options": {
          "options": {
            "singlecol": "Single Coloumn",
            "multicol": "Multiple Coloumn"
          }
        },
        "default": "singlecol"
      },
      {
        "type": "wysiwyg",
        "name": "content",
        "label": "Text",
        "options": {
          "editor": {
            "toolbar": "undo redo | bold italic underline link |",
            "formats": {
              "underline": {
                "inline": "u",
                "exact": true
              }
            },
            "default_link_target": "_blank",
            "entity_encoding": "raw",
            "forced_root_block": "",
            "menubar": false
          }
        }
      }
    ]
  }
},
{      
  "type": "collectionlink",
  "name": "services",
  "label": "Services",
  "options": {
    "link": "services",
    "display": "name",
    "multiple": true
  }
}
  ]
}

Thanks

No, that’s not possible. But you could split the field into two fields or into a surrounding set. The one with the services has a limit of one.

Alernatively you could write your own modified repeater field that checks against the existence of the services field.

How can I write my repeater field?
Is it possible to give an example?

Thanks

You can copy the original repeater component from /modules/Cockpit/assets/components/field-repeater.tag into /config/tags/field-repeater.tag and change it here.

Now have a look at the add function. There you can add a check, if the items contain a service field already.

If you need some more inspiration for creating custom fields, have a look at

1 Like