Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Get all unpublished sitetree objects in modeladmin? [Solved]

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

Page: 1
Go to End
Author Topic: 663 Views
  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Get all unpublished sitetree objects in modeladmin? [Solved] Link to this post

    Hi,

    I need to get an overview of all SiteTree objects which are not Published yet in modeladmin.
    Wondering how to do this in 3.1

    class UnPublishedPagesAdmin extends ModelAdmin {

    public static $managed_models = array('SiteTree');

    static $url_segment = 'unpublishedpages';

    static $menu_title = 'Publish content';

    public $showImportForm = false;

    public $showExportForm = false;

    public function getList() {

    $unpublished_site_trees = ArrayList::create('SiteTree');

    $all_site_trees = SiteTree::get();

    foreach ($all_site_trees as $sitetree_obj) {
    if (! $sitetree_obj->isPublished()) {
    $unpublished_site_trees->push($sitetree_obj);
    }
    }

    return $unpublished_site_trees;

    }

    }

    }

    Above my unsuccesful attempt ... The is not something like ->filter('NotPublished')?

    Cheers,

    Bart

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: Get all unpublished sitetree objects in modeladmin? [Solved] Link to this post

    Hey bartvanirsel,

    Not the best answer, and I hope someone does come along with a better one but, I usually just fall back and use the Versioned class to get a list of pages by stage. I haven't seen or figured out an elegant way of doing this using DataList::get() yet

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: Get all unpublished sitetree objects in modeladmin? [Solved] Link to this post

    Hi IOTI,

    Thanks for your tip! It helped me on the way.

    I Solved it by doing the following:

    <?php

    class UnPublishedPagesAdmin extends ModelAdmin {
       
       public static $managed_models = array('SiteTree');
       
       static $url_segment = 'unpublishedpages';
       
       static $menu_title = 'Publish content';
       
       public $showImportForm = false;
       
       public $showExportForm = false;
       
       public function getList() {
          
          $stageRecords = Versioned::get_by_stage('SiteTree', 'Stage');

          $concept_records_ids_array = array();

          foreach ($stageRecords as $rec) {
             if (!$rec->isPublished()) {
                $concept_records_ids_array[] = $rec->ID;
             }
          }

          $sitetree_objects = SiteTree::get()->filter(array(
             'ID' => $concept_records_ids_array
          ));

          return $sitetree_objects;
       }
       
    }

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Get all unpublished sitetree objects in modeladmin? [Solved] Link to this post

    Hi,

    I just did something similar to this for 2.4 - below might cause a lot less database queries as I think each isPublished() is a new query. (for 3.1 I guess just change DataObjectSet to DataList)

    $draftPages = Versioned::get_by_stage('SiteTree', 'Stage');
    $livePages = Versioned::get_by_stage('SiteTree', 'Live');
    $newPages = new DataObjectSet();

    foreach ($draftPages as $p) {
       if (!$livePages->find('ID',$p->ID)) {
          $newPages->push($p);
       }
    }

    return $newPages;

  • BenWu
    Avatar
    Community Member
    87 Posts

    Re: Get all unpublished sitetree objects in modeladmin? [Solved] Link to this post

    Thanks JonoM,

    I use the following to get random products to show on a page :

    $randomProducts = Versioned::get_by_stage('ProductPage', 'Live', $filter=' ', ' rand()' , $join='', $limit=12 );

    663 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.