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
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.
As long as this feature is not part of the core, you can achieve it like this:
site/fields
, so that the current field will be overwritten and you don’t have to modify the core codeIn 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 = ' ';
}
$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
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.
Thank you very much
@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.
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!
Has this been included into Kirby in the end ?
The limit option option works.