We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10378
    • 375 Posts
    Hello,

    I'm currently working on a CMP to load resources from an Articles container (or any other resource container) and list them in an CMP page in MODx backend including some TV values.

    I already worked through the Doodles tutorial and have the CMP up and running.
    But I'm very new to MODx development so it's hard for me to figure out how to extend the Doodles tutorial to get this running.

    Anyone having code samples on how to do this?

    Especially the getlist.class.php processor and the corresponding .grid.js would be interesting

    MODx Revo 2.2.4

    Thanks,
    Martin

    This question has been answered by multiple community members. See the first response.

    [ed. note: gadgetto last edited this post 14 years ago.]
      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
      • 4172
      • 5,888 Posts
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 10378
        • 375 Posts
        Thanks Bruno for your fast response, but I'd like to do it from scratch without using another extra. I'll have a look at MIGXdb later.
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/
        • discuss.answer
          • 33968
          • 863 Posts
          IMO the best way to figure out the new class based processors is to dig into the core code.
          Check out: core/model/modx/modprocessor.class.php

          You'll see the various object processor classes you can extend, as well as the various methods available for modifying the default queries and output.

          Also useful are all the MODX processors in core/model/modx/processors/

          An example starting point for your grid:
          var containerId = 6; // just for example
          
          myCmp.grid.Resource = function(config) {
              config = config || {};
              Ext.applyIf(config,{
                  id: 'mycmp-grid-resource'
                  ,url: myCmp.config.connectorUrl
                  ,baseParams: { 
                      action: 'mgr/mycmp/getList' 
                      // set the 'parent' parameter for retrieval in the processor
                      ,parent: containerId
                  }
                  // add fields you want in the grid (or record) here
                  ,fields: ['id','pagetitle','template']
                  ,paging: true
                  ,remoteSort: true
                  ,anchor: '97%'
                  ,columns: [{
                      header: 'Id'
                      ,dataIndex: 'id'
                      ,sortable: true
                  },{
                      header: 'Pagetitle'
                      ,dataIndex: 'pagetitle'
                      ,sortable: true
                  },{
                      header: 'Template'
                      ,dataIndex: 'template'
                      ,sortable: true
                  }/*
                      Then add the columns here
                  */]
              });
              myCmp.grid.Resource.superclass.constructor.call(this,config)
          };
          Ext.extend(myCmp.grid.Resource,MODx.grid.Grid);
          Ext.reg('mycmp-grid-resource',myCmp.grid.Resource);
          


          The minimum resource processor you can use is as simple as this:
          <?php
          class myResourceGetListProcessor extends modObjectGetListProcessor {
              public $classKey = 'modResource';
              public $languageTopics = array('resource');
              public $defaultSortField = 'pagetitle';
              
              /* Override prepareQueryBeforeCount(), adding in the 'parent' clause  */
              public function prepareQueryBeforeCount(xPDOQuery $c) {
                  $parent = $this->getProperty('parent');  // we set this in the grid, remember?
                  if (!empty($parent)) {
                      $c->where(array('parent:=' => $parent));
                  }
                  return $c;
              }
          }
          return 'myResourceGetListProcessor';
          

          To get the TV values as well, you'll need to add the appropriate join condition to prepareQueryBeforeCount(). Have a look at getTemplateVarCollection() in core/model/modx/modresource.class.php
            • 33968
            • 863 Posts
            Also - and these are a lot more advanced so start with the basics first and don't get put off - it would be worth seeing how it's done in the Articles grid:

            core/components/articles/processors/article/getlist.class.php
            assets/components/articles/js/container/container.articles.grid.js
              • 3749
              • 24,544 Posts
              This is another good place to learn how to set things up: https://github.com/splittingred.


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 10378
                • 375 Posts
                Thanks all for your help!
                @Lucas: your sample code helped alot! I could solve the problem!
                  Freelancer @bitego http://www.bitego.com
                  ---
                  GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                  More infos here: http://www.bitego.com/extras/goodnews/
                  • 10378
                  • 375 Posts
                  As I told - thanks to you help I got the grid up and running, but I'd still need a little help:

                  I try to display the username field from the users table in each row. I tried back and forth but had no succes finding a solution. As a beginner in MODx and also in PHP it's hard to understand the xPDO model - and its hard to find a hint in the documentation without knowing what to search for.

                  I also had a look at the processor class in the Articles plugin as I thought it should match my requirements. But the prepareQueryBeforeCount method there hasn't enlightened me (it confused me even more).

                  <?php
                  class MyExtraGetListProcessor extends modObjectGetListProcessor {
                      public $classKey = 'modResource';
                      public $languageTopics = array('myextra:default');
                      public $defaultSortField = 'published';
                      public $checkListPermission = true;
                      public $defaultSortDirection = 'ASC';
                      public $objectType = 'modResource';
                  
                      public function prepareQueryBeforeCount(xPDOQuery $c) {
                          $parent = $this->getProperty('parent');
                          if (!empty($parent)) {
                              //as I understand this should leftJoin the users table with an alias of Users
                              $c->leftJoin('modUsers','Users');
                  
                              //what code to add now to get the username field from the 
                              //users table and display it in the CMP grid?
                  
                  
                              $c->where(array('parent:=' => $parent));
                              $c->where(array('deleted' => 0));
                          }
                          return $c;
                      }
                  
                  }
                  return 'MyExtraGetListProcessor';
                  


                  In the grid.js I have this field which should get the username of the users table:

                  ...
                  ,{
                          header: _('myextra.resource_publisher')
                          ,dataIndex: 'publishedby_username'
                          ,width: 100
                  }
                  ...
                  
                  [ed. note: gadgetto last edited this post 14 years ago.]
                    Freelancer @bitego http://www.bitego.com
                    ---
                    GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                    More infos here: http://www.bitego.com/extras/goodnews/
                    • 4172
                    • 5,888 Posts
                    have a look into the modResource - schema:

                            <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign" />
                            <aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign" />
                            <aggregate alias="DeletedBy" class="modUser" local="deletedby" foreign="id" cardinality="one" owner="foreign" />
                            <aggregate alias="PublishedBy" class="modUser" local="publishedby" foreign="id" cardinality="one" owner="foreign" />
                    


                    now, if you want join to users by publishedon you can do it with:

                    $joinclass = 'modUser';
                    $joinalias = 'PublishedBy';
                    
                    $c->leftjoin($joinclass, $joinalias);
                    

                    to get the fields you can do:

                    $c->select($modx->getSelectColumns($joinclass, $joinalias, 'publishedby_'));


                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 10378
                      • 375 Posts
                      Hi Bruno17,

                      thanks for your hints, but this also doesn't work. I added the code you provided and the function now looks like:

                          public function prepareQueryBeforeCount(xPDOQuery $c) {
                              $parent = $this->getProperty('parent');
                              if (!empty($parent)) {
                      
                                  $joinclass = 'modUser';
                                  $joinalias = 'PublishedBy';
                                  $c->leftjoin($joinclass, $joinalias);
                                  
                                  $c->select($modx->getSelectColumns($joinclass, $joinalias, 'publishedby_'));
                                  
                                  $c->where(array('parent:=' => $parent));
                                  $c->where(array('deleted' => 0));
                              }
                              return $c;
                          }
                      


                      Now the corresponding grid has no entries at all and I think the processor script crashes.
                        Freelancer @bitego http://www.bitego.com
                        ---
                        GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                        More infos here: http://www.bitego.com/extras/goodnews/