API Content Block

Hi,

we manage multiple MODX install with CB installed, and sometimes we add new CB fields and we need to update all these installs.

At the moment we manually import-export for each the fields, and we are interested to know if there is a alternative to run this process programmatically.

Somebody have experience in that? Do you know how run the ContentBlockImportProcessor ?

Thx

Hi Samuel,

That’s not something I’ve tried before, but MODX is pretty good at letting you run any processor. As long as the ContentBlocks service is loaded, something like this should do the trick:

<?php
$corePath = $modx->getOption('contentblocks.core_path', null, $modx->getOption('core_path') . 'components/contentblocks/');
$contentBlocks = $modx->getService('contentblocks', 'ContentBlocks', $corePath . 'model/contentblocks/');
if (!$contentBlocks) {
    return 'Unable to load ContentBlocks service';
}

$props = array();
$options = array(
    'processors_path' => $contentBlocks->config['processorsPath'],
);
$response = $modx->runProcessor('mgr/rebuild_content', $props, $options);

If you’d like to access the output of the processor, you’ll probably want to call $modx->setLogTarget() and $modx->setLogLevel() methods with the appropriate options to save the log entries somewhere you can read them easily, or maybe just echoing them.

1 Like

Thx @mhamstra, I’ll try in the next days and report a feedback :smile:

Hi Samuel ( @webmasterunifr ),

did you get this to work? I have the same problem and would like to try populating/updating many Sites at once. Any input or shared experience is appreciated!

Hi @sebastian-marinescu,

for the moment we haven’t implemented this features on our website, it’ll come later this summer.

We’ll share our solution as soon possible :wink:

3 Likes

Sounds very interesting @webmasterunifr!

And yeah I feel your pain @sebastian-marinescu. Right now I’m doing it manually too, mostly by copy / pasting the database rows with Navicat or the _data files with Gitify. The main issue with that is the conflicting ID’s of the items. As soon as you add project specific layouts or fields, the database diverges from the “default” set you want to update.

To bypass that a litte, I now upped the auto_increment setting on the ContentBlocks tables, so that the ID’s of the default set will at least stay the same across multiple installs and new items will get an ID starting at 1000 or something. Makes it slightly more manageable, but far from ideal. Chunks for example are referenced by ID too, so sometimes I need to change the ID’s again after import for all the Chunk field types.

Hi @mhamstra!
Hi everybody!

after a long delay, we’re happy to share with you our code.

We created a little script that:

  1. install ContentBlock
  2. update some SystemSettings (useful four our case)
  3. load the export (Fields, Layouts, Templates)
  4. regenerate the content

The script is available on gitst, we hope that will help somebody.

# Pseudo code


##  Config (lines 12-65)

var $provider is the textual name of the provider to use
var $searchedPackage is the name of the searched package

array $settings is a collection of array $setting. These array are the setting of MODX
array $setting is a array with the key,value and namespace of the settings to update

var $pathToContentBlockExports is the basePath of the location where the CB export are located

array $settings_cb is a collection of array $setting_cb (see the code)


## Main code

### line 71-81: retrieve the id of the provider

### line 83-97: download and install the package 

### line 99-112: update the systems settings

### line 114-153: run the import of fields/layouts/templates 

### line 155-165: rebuild the website


## Functions 

###  function getPackageUrl() return the signature and the package_url for the download
2 Likes

Interesting, thanks for sharing :slight_smile:

Wow, thanks @webmasterunifr - looks promising!
I’ll check it out soon!