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   1058 Views

Avatar
Deklin

Community Member, 16 Posts

15 July 2010 at 10:50am

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

Avatar
UncleCheese

Forum Moderator, 4096 Posts

15 July 2010 at 11:47am

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

Avatar
DeklinKelly

Community Member, 197 Posts

2 August 2010 at 2:14pm

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

Forum Moderator, 4096 Posts

2 August 2010 at 3:50pm

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

Community Member, 197 Posts

2 August 2010 at 11:40pm

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

Forum Moderator, 4096 Posts

3 August 2010 at 1:01am

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.