5. Consumer - nuvoleweb/integration GitHub Wiki

Create a Consumer programmatically

To create a Consumer programmatically we will make use of the ConsumerFactory static class. In order to have a Consumer working correctly we need to have at valid Backend stored in the system, so we must create that first.

use Drupal\integration_consumer\ConsumerFactory;
use Drupal\integration\Backend\BackendFactory;

$backend = BackendFactory::create('filesystem', 'filesystem_backend')
  ->setBackendSetting('path', '/path/to/data')
  ->setResourceSchema('article')
  ->setResourceSchemaSetting('article', 'folder', 'article');

$consumer = ConsumerFactory::create('article', 'filesystem')
  ->setMapping('source2', 'destination2')
  ->setResourceSchema('schema')
  ->setEntityBundle('article');

Note that, at this point, the Backend configuration is only stored in the class static cache. To persist configuration objects in the database use the following:

$backend->getConfiguration()->save();
$consumer->getConfiguration()->save();