extras-evolution / multiTV

Custom TV for Evolution CMS: Transform a template variables into a sortable multi item list for the Evolution content management framework
8 stars 23 forks source link

[I] prepare, prepareWrap #42

Closed mnoskov closed 6 years ago

mnoskov commented 6 years ago

These changes give us the ability to prepare data in each row and outer wrap.

Prepare function can be a snippet name, function name or closure, each delimited by comma if string, or as an array of functions. Prepare functions can be defined in config file or as snippet parameter.

Function/snippet parameters is: $data, $modx, $_multiTV.

Examples:

// in config:
$settings['prepare'] = function($data, $modx, $_multiTV) {
   return $data;
};
// in snippet prepareRowsSnippet:
return $data;

// in config:
function prepareRowsFunction($data, $modx, $_multiTV) {
   return $data;
}

$settings['prepare'] = 'prepareRowsSnippet,prepareRowsFunction'; 
// or
$settings['prepare'] = ['prepareRowsSnippet', 'prepareRowsFunction']; 
// or
$settings['prepare'] = ['prepareRowsSnippet', function($data, $modx, $_multiTV) {
   return $data;
}]; 
[[multiTV? &tvName=`test` &prepare=`prepareRowsSnippet,prepareRowsFunction`]]

For rows prepare function $data array will contain all variable fields + next values:

`docid` Current document identifier
`iteration` Current iteration
`row` Array with these keys: `row.number`: Current iteration, starts from 1 `row.total`: Total rows

For prepareWrap additional values will be next:

`docid` Current document identifier
`wrapper` Array of rendered rows
`rows` Array with these keys: `rows.offset`: Count of skipped rows `rows.total`: Total rows

Also this pullrequest adds functions setStore($key, $value) and getStore($key) for use in prepare functions.

Examples:

$settings['prepare'] = function($data, $modx, $_multiTV) {
   $store = $_multiTV->getStore('storekey');

   if (is_null($store)) {
      $store = $modx->runSnippet('HeavySnippet');
      $_multiTV->setStore('storekey', $store);
   }

   return $data;
};