3. Backend - nuvoleweb/integration GitHub Wiki

Create a Backend programmatically

To create a Backend programmatically we will make use of the BackendFactory static class. In order to have a Backend working correctly we need to add at least one Resource Schema object, so we should need to create that first.

The code would look something like the following:

use Drupal\integration\Backend\BackendFactory;
use Drupal\integration\ResourceSchema\ResourceSchemaFactory;

$resource_schema = ResourceSchemaFactory::create('article')
  ->setField('title', 'Title')
  ->setField('image', 'Image')
  ->setField('body', 'Body');

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

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

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