Limit number of structure entries

So I would like to limit the number of entries a structure can have. I tried the documentation but didn’t find anything.

Hope someone can help me

~ Max

Unfortunately, there is no option to limit the number of structure items. You could try to copy the structure field and adapt it to your needs.

There is already a feature request issue on GitHub.

Although you can not limit it in the panel, you can limit the number you display in your panel. I often use an information text in the panel to let the user know only the first X entries will be displayed.

You could even go for some custom panel css to show for exemple in red the entries after the nth.

Please keep in mind, that this will only work correctly, if you don’t sort, shuffle, flip etc. the entries in your template.

1 Like

As long as this feature is not part of the core, you can achieve it like this:

  • copy the structure field and put it into site/fields, so that the current field will be overwritten and you don’t have to modify the core code
  • make the following changes to /site/fields/structure/structure.php

In line 19, add a new property called limit:

public $limit = null;

Replace the headline function from lines 127ff with the following code

public function headline() {
    // get entries
    $entries = $this->entries();
   // check if limit is either null or the number of entries less than limit 
    if(!$this->readonly && (is_null($this->limit) || (is_int($this->limit) && $entries->count() < $this->limit))) {
        $add = new Brick('a');
        $add->html('<i class="icon icon-left fa fa-plus-circle"></i>' . l('fields.structure.add'));
        $add->addClass('structure-add-button label-option');
        $add->data('modal', true);
        $add->attr('href', purl($this->model, 'field/' . $this->name . '/structure/add'));

    } else {
      $add = null;
    }

    // make sure there's at least an empty label
    if(!$this->label) {
      $this->label = '&nbsp;';
    }

    $label = parent::label();
    $label->addClass('structure-label');
    $label->append($add);

    return $label;

  }

With these modifications, the add button is only shown if there is no limit given or if the limit is greater than the current number of items.

Now you can add the new option in your blueprint:

events:
    label: Events
    type: structure
    style: table
    limit: 5
    fields:
      title:
        label: Title
        type: text
      text:
        label: Text
        type: text
2 Likes

Well that’s simply brilliant ! I’m guessing you submitted it to the core ?

I am going to make some tests with this for the next release. :slight_smile:

3 Likes

Thank you very much :slight_smile:

@lukasbestle to include that properly one should prohibit example.com/panel/site/field/fieldname/structure/add from working when the limit is reached

That’s exactly what I thought as well. :slight_smile:

That’s what I thought as well until I found that the readonly option is handled exactly in this way, so I thought I might just use that for a quick solution.

Oh, then that’s a bug. I will take a look at it.

This feature is now on the develop branch for Kirby 2.3.2. I have also fixed the missing validation for the route, thanks @mxst!

1 Like

Has this been included into Kirby in the end ?

The limit option option works.

1 Like