Jump to:

7938 Posts in 1541 Topics by 945 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Maximum 10 Items: DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1007 Views
  • Deklin
    Avatar
    Community Member
    16 Posts

    Maximum 10 Items: DataObjectManager Module Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Maximum 10 Items: DataObjectManager Module Link to this post

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

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: Maximum 10 Items: DataObjectManager Module Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Maximum 10 Items: DataObjectManager Module Link to this post

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

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: Maximum 10 Items: DataObjectManager Module Link to this post

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

    ?>

  • UncleCheese
    Avatar
    4085 Posts

    Re: Maximum 10 Items: DataObjectManager Module Link to this post

    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.

    1007 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.