Using the DataObjectManager Module, how can I limit the maximum number of items added to 10?
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
DataObjectManager Module /
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba
Maximum 10 Items: DataObjectManager Module
function canCreate() {
return DataObject::get("SomeObject")->Count() < 10;
}
Where should I put:
function canCreate() {
return DataObject::get("SomeObject")->Count() < 10;
}
?
Could I use something like this instead?
if (DataObject::get("yourDom")->Count() > 10) $yourDom->removePermission('add');
Yeah.. well, really, the DOM should respect the canCreate() function on your source class, but clearly this just a gap in the code, so in the interim, I would just use something like:
if($this->YourRelatedObjects()->Count() > 10)
$dom->removePermission('add');
Thanks. That successfully prevents more than the allowed number of items to be added however when you delete an item you can't add another until you refresh the admin page.
I am not sure if the other problem is a bug or if I am just putting the canCreate() function in the wrong place.
<?php
class SliderItem extends DataObject
{
static $db = array (
'Heading' => 'Varchar',
'Text' => 'Text',
'Link' => 'Varchar',
'LinkText' => 'Text',
);
static $has_one = array(
'Image' => 'Crop250x167',
'Page'=>'Page'
);
static $defaults = array(
"LinkText" => 'View Details',
);
public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('Heading'),
new TextareaField('Text'),
new TextField('Link'),
new TextField('LinkText','Link Text'),
new ImageField('Image')
);
}
public function canCreate() {
return false;
}
}
?>
Yeah, delete doesn't refresh the page, so that's going to be an issue no matter what approach you use for canCreate(). I'm not sure if there's a reasonable solution for that one.