Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Maximum 10 Items: DataObjectManager Module


Reply

6 Posts   1024 Views

Avatar
Deklin

15 July 2010 at 10:50am Community Member, 16 Posts

Using the DataObjectManager Module, how can I limit the maximum number of items added to 10?

Avatar
UncleCheese

15 July 2010 at 11:47am 4085 Posts

function canCreate() {
return DataObject::get("SomeObject")->Count() < 10;
}

Avatar
DeklinKelly

2 August 2010 at 2:14pm Community Member, 197 Posts

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');

Avatar
UncleCheese

2 August 2010 at 3:50pm 4085 Posts

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');

Avatar
DeklinKelly

2 August 2010 at 11:40pm Community Member, 197 Posts

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;
   }
}

?>

Avatar
UncleCheese

3 August 2010 at 1:01am 4085 Posts

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.