We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38878
    • 255 Posts
    Quote from: muzzstick at Aug 08, 2015, 04:26 AM
    Can you show some of the sample json that is returned to the grid?

    Use Firebug to have a look at how the standard getlist processor structures the json that is returned to the grid.

    The fields should be the the json field names which off the top of my head would be the same as the array keys after they've been converted.
    ExtJS has a JsonReader which is implemented in the MODX.grid superclass. You can find it in /manager/assets/modext/widgets/core/modx.grid.js

    I haven't attempted feeding data into a grid from anything besides an overridden getlist processor before but I'm definitely keen to give it a go as soon as I get some time.

    There's an example of an overridden modprocessor in the core for lexicons which are read from a file and then populate a grid.
    Have a look at:
    /core/model/modx/processors/workspace/lexicon/getlist.class.php


    I'll take a look. Sample json response with 2 records:
    [
        {
            "resource_uri": "https://www.eventbriteapi.com/v3/events/17740447155/",
            "name": {
                "text": "CMH Works MODX Seminar",
                "html": "CMH Works MODX Seminar"
            },
            "description": {
                "text": "Features tour and demonstration of the benefits of MODX CMS and ModxCloud service. Participants should have a basic understanding of web design and/or development or a strong desire to learn.  \nTHIS IS A TEST EVENT PLEASE DO NOT REGISTER. ",
                "html": "<P>Features tour and demonstration of the benefits of MODX CMS and ModxCloud service. Participants should have a basic understanding of web design and/or development or a strong desire to learn. </P>\r\n<P><SPAN STYLE=\"color: #ff0000;\">THIS IS A TEST EVENT PLEASE DO NOT REGISTER.</SPAN></P>"
            },
            "id": "17740447155",
            "url": "http://www.eventbrite.com/e/cmh-works-modx-seminar-tickets-17740447155",
            "start": {
                "timezone": "America/New_York",
                "local": "2015-08-15T15:00:00",
                "utc": "2015-08-15T19:00:00Z"
            },
            "end": {
                "timezone": "America/New_York",
                "local": "2015-08-15T17:00:00",
                "utc": "2015-08-15T21:00:00Z"
            },
            "created": "2015-07-12T23:06:25Z",
            "changed": "2015-08-07T00:43:46Z",
            "capacity": 18,
            "status": "live",
            "currency": "USD",
            "listed": true,
            "shareable": true,
            "invite_only": false,
            "online_event": false,
            "show_remaining": true,
            "tx_time_limit": 480,
            "logo_id": "14287684",
            "organizer_id": "8310024813",
            "venue_id": "10943959",
            "category_id": "102",
            "subcategory_id": "2004",
            "format_id": "2",
            "logo": {
                "id": "14287684",
                "url": "https://img.evbuc.com/https%3A%2F%2Fimg.evbuc.com%2Fhttp%253A%252F%252Fcdn.evbuc.com%252Fimages%252F14287684%252F147483532268%252F1%252Foriginal.jpg%3Frect%3D0%252C125%252C500%252C250%26s%3D6bafbf4a3ca85f7db56826cba7ca4ede?h=200&w=450&s=0bf17695815c351bda82c869d7e2346a",
                "aspect_ratio": "2",
                "edge_color": "#1c1c1c",
                "edge_color_set": true
            },
            "organizer": {
                "description": null,
                "logo": null,
                "resource_uri": "https://www.eventbriteapi.com/v3/organizers/000000000/",
                "id": "8310024813",
                "name": "Joe Organizer",
                "url": "http://www.eventbrite.com/o/joe-organizer-8310024813",
                "num_past_events": 0,
                "num_future_events": 1
            },
            "venue": {
                "address": {
                    "address_1": "123 Maple St.",
                    "address_2": null,
                    "city": "Bismark",
                    "region": "North Dakota",
                    "postal_code": "20132",
                    "country": "US",
                    "latitude": 39.083503,
                    "longitude": -77.700644
                },
                "resource_uri": "https://www.eventbriteapi.com/v3/venues/0000000000/",
                "id": "10943959",
                "name": "CMH Works",
                "latitude": "42.083503",
                "longitude": "-86.70064400000001"
            }
        },
        {
            "resource_uri": "https://www.eventbriteapi.com/v3/events/17773523086/",
            "name": {
                "text": "New Test Event",
                "html": "New Test Event"
            },
            "description": {
                "text": "Here's another event from CMH Works. Ain't it cool? \n  \nTHIS IS A TEST EVENT ",
                "html": "<P>Here's another event from CMH Works. Ain't it cool?</P>\r\n<P> </P>\r\n<P>THIS IS A TEST EVENT</P>"
            },
            "id": "17773523086",
            "url": "http://www.eventbrite.com/e/new-test-event-tickets-17773523086",
            "start": {
                "timezone": "America/New_York",
                "local": "2015-08-23T19:00:00",
                "utc": "2015-08-23T23:00:00Z"
            },
            "end": {
                "timezone": "America/New_York",
                "local": "2015-08-31T22:00:00",
                "utc": "2015-09-01T02:00:00Z"
            },
            "created": "2015-07-15T01:25:45Z",
            "changed": "2015-08-01T19:09:19Z",
            "capacity": 125,
            "status": "live",
            "currency": "USD",
            "listed": true,
            "shareable": true,
            "invite_only": false,
            "online_event": false,
            "show_remaining": true,
            "tx_time_limit": 480,
            "logo_id": "14330005",
            "organizer_id": "8310024813",
            "venue_id": "11031253",
            "category_id": "102",
            "subcategory_id": "2005",
            "format_id": "100",
            "logo": {
                "id": "14330005",
                "url": "https://img.evbuc.com/https%3A%2F%2Fimg.evbuc.com%2Fhttp%253A%252F%252Fcdn.evbuc.com%252Fimages%252F14330005%252F147483532268%252F1%252Foriginal.jpg%3Frect%3D0%252C125%252C500%252C250%26s%3D6500bdb53e94c36d9012f501c82d1701?h=200&w=450&s=958fb4b711a9e2db1945752fd2abc917",
                "aspect_ratio": "2",
                "edge_color": "#1c1c1c",
                "edge_color_set": true
            },
            "organizer": {
                "description": null,
                "logo": null,
                "resource_uri": "https://www.eventbriteapi.com/v3/organizers/8310024813/",
                "id": "8310024813",
                "name": "Joe Organizer",
                "url": "http://www.eventbrite.com/o/joe-organizer-8310024813",
                "num_past_events": 0,
                "num_future_events": 1
            },
            "venue": {
                "address": {
                    "address_1": "123 Maple St.",
                    "address_2": null,
                    "city": "Bismark",
                    "region": "North Dakota",
                    "postal_code": "20132",
                    "country": "US",
                    "latitude": 39.083503,
                    "longitude": -77.700644
                },
                "resource_uri": "https://www.eventbriteapi.com/v3/venues/11031253/",
                "id": "11031253",
                "name": "CMH Works",
                "latitude": "42.083503",
                "longitude": "-86.70064400000001"
            }
        }
    ]


    I validated the JSON and it is good. Buuut the json decoder is puking on it, the browser is throwing
    Uncaught SyntaxError: Unexpected token }
    Ext.util.JSON.doDecode @ ext-all.js:21
    
      • 38878
      • 255 Posts
      Quote from: muzzstick at Aug 08, 2015, 04:26 AM
      C...

      Have a look at:
      /core/model/modx/processors/workspace/lexicon/getlist.class.php


      The lexicon getlist processor appears to be returning an array. My processor was converting to JSON before returning. I've emulated the format that lexicon getlist processor is returning to see what happens.

      My getEvents.class.php now looks like:
      <?php
      class GetEventsProcessor extends modProcessor {
          public $outputArray = array();
          public $count;
          public $modx;
          
          public function initialize() {
              $this->setDefaultProperties(array(
                  'start' => 0,
                  'limit' => 10,
                  'sort' => 'id',//<- probably not required
                  'dir' => 'ASC',//<- probably not required
              ));
              return true;
          }
          public function process() {
              // calls to main class functions to retrieve events
              $this->mgr_client = new Ebents($this->modx);
              
              $this->params = array ();
              $this->params['expand'] = 'venue,organizer';
              $this->args = array('id' => 'me', 'data' => 'owned_events', 'params' => $this->params);
              $this->output = $this->mgr_client->getData('users', $this->args);
      
              //lazy convert from stdClass to array
              $events = array();
              $events = json_decode(json_encode($this->output->events), true);
              
              $this->count = count($events);
              //error_log("count :" . $this->count); 
              $events = array_slice($events,$this->getProperty('start'),$this->getProperty('limit'),true);//assume I am limiting here for paging...*shrugs*
              /* loop through */
              $list = array();
              foreach ($events as $event) {
                  $entryArray = array(
                      'id' => $event['id'],
                      'name' =>  $event['name']['text'],
                      'description' => $event['description']['text'],
                      'organizer' => $event['organizer']['name'],
                      'venue' => $event['venue']['name'],
                      'start' => $event['start']['local'],
                      'end' => $event['end']['local'],
                      'status' => $event['status'],
                  );
                  $list[] = $entryArray;
              }
              //error_log(print_r($list,true)); 
              return $this->outputArray($list,$count);
          }
          
      }
      return 'GetEventsProcessor';


      which is returning (print_r output to log) to the grid :
      Array
      (
          [0] => Array
              (
                  [id] => 17740447155
                  [name] => CMH Works MODX Seminar
                  [description] => Features tour and demonstration of the benefits of MODX CMS and ModxCloud service. Participants should have a basic understanding of web design and/or development or a strong desire to learn.  
      THIS IS A TEST EVENT PLEASE DO NOT REGISTER. 
                  [organizer] => Joe Organizer
                  [venue] => CMH Works
                  [start] => 2015-08-15T15:00:00
                  [end] => 2015-08-15T17:00:00
                  [status] => live
              )
      
          [1] => Array
              (
                  [id] => 17773523086
                  [name] => New Test Event
                  [description] => Here's another event from CMH Works. Ain't it cool? 
        
      THIS IS A TEST EVENT 
                  [organizer] => Joe Organizer
                  [venue] => CMH Works
                  [start] => 2015-08-23T19:00:00
                  [end] => 2015-08-31T22:00:00
                  [status] => live
              )
      
      )
      


      This all looks okay to me if in fact the grid expects an array. Now I'm getting the browser error
      Uncaught TypeError: b[(d.xtype || e)] is not a function
      Ext.ComponentMgr.create	@	ext-all.js:21
      Ext.extend.load	@	modx.js:85
      Ext.extend.loadGrid	@	home.panel.js:51
      
      which I think means I need to add a
      preventRender: true
      somewhere. The saga continues...

      Thanks much for your help!
      • discuss.answer
        • 44195
        • 293 Posts
        I'm pretty sure the grid expects your data to be contained within the "results" parent.

        For example here's a sample from my doodles grid:

        {
            "success":true,
            "total":"4",
            "results":[
                {
                    "id":38,
                    "name":"dfthdfth",
                    "description":"fgyujftygj",
                    "filename":"painting.jpg",
                    "createdon":null,
                    "createdby":0,
                    "editedon":null,
                    "editedby":0
                },
                {
                    "id":37,
                    "name":"dshdfth",
                    "description":"drthfth",
                    "filename":"pirate.jpg",
                    "createdon":null,
                    "createdby":0,
                    "editedon":null,
                    "editedby":0
                },
                {
                    "id":39,
                    "name":"gtrerthg",
                    "description":"hytdtrh",
                    "filename":"image.jpg",
                    "createdon":null,
                    "createdby":0,
                    "editedon":null,
                    "editedby":0
                },
                {
                    "id":32,
                    "name":"Test",
                    "description":"fdthfthrth",
                    "filename":"Black-dragon.jpg",
                    "createdon":null,
                    "createdby":0,
                    "editedon":null,
                    "editedby":0
                }
            ]
        }
        


        Each 'result' would count as a grid row.
        I would recommend try using your processor to add some of your custom fields into a format like this one and troubleshoot from there.
        Keep it as simple as possible and slowly build up the complexity.

        The outputArray() function you're using will actually convert to json. It's just you did your recursive print before it happened.
          I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
          Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
          • 38878
          • 255 Posts

          That did it. I wasn't aware of the outPutArray function (thought it was just another array declared on the fly - silly me). So after a long and painful journey, the short answer is you just need a custom getlist class function extending modProcessor to populate a grid with 3rd party data. There is a lot of "implicit" handling going on but I learned a ton by screwing with the store, scriptTagProxy, etc. which was the goal, but no necessary.

          The final processor getlist.class.php looks like this:
          class GetEventsProcessor extends modProcessor {
              
              public function initialize() {
                  $this->setDefaultProperties(array(
                      'start' => 0,
                      'limit' => 10,
                  ));
                  return true;
              }
              public function process() {
                  // call to main class to retrieve events from theEventbrite API
                  $this->mgr_client = new Ebents($this->modx);
                  
                  $this->params = array ();
                  $this->params['expand'] = 'venue,organizer';
                  $this->args = array('id' => 'me', 'data' => 'owned_events', 'params' => $this->params);
                  $this->output = $this->mgr_client->getData('users', $this->args);
          
                  //lazy convert results from stdClass to array as the api returns a stdClass object
                  $events = array();
                  $events = json_decode(json_encode($this->output->events), true);
                  $count = count($events);
                  $events = array_slice($events,$this->getProperty('start'),$this->getProperty('limit'),true);
                  
                  $list = array();
                  // process event data for grid columns
                  foreach ($events as $event) {
                      $entryArray = array(
                          'id' => $event['id'],
                          'name' =>  $event['name']['text'],
                          'description' => $event['description']['text'],
                          'organizer' => $event['organizer']['name'],
                          'venue' => $event['venue']['name'],
                          'start' => $event['start']['local'],
                          'end' => $event['end']['local'],
                          'status' => $event['status'],
                      );
                      $list[] = $entryArray;
                  }
                  //convert to JSON for grid consumption
                  return $this->outputArray($list,$count);
              }
          }
          return 'GetEventsProcessor';


          Thanks a ton for your help on this. Now on to actually doing something with the data...
            • 44195
            • 293 Posts
            Well done! Glad you got it working. laugh
              I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
              Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com