How to access "bpmn:definitions" within Plugin?

Hey folks!

I have a quick question. I have a plugin that does some changes on the BPMN model itself. Now I want to change the content of the <bpmn:definitions modeler:executionPlatform attribute - but have no idea how to access this in my plugin. It is not part of the elementRegistry as far as I see. My current code is here:

Thanks for every hint!
Bernd
Bernd

The modeler executionPlatform properties are part of the modeler-moddle library, which you would also need to include.

There exists a bpmnJs.getDefinitions method which you can use. If you don’t have access to the modeler inside your service, there are also easy to use helpers you could integrate and we also use in different parts of bpmn-js.

/**
 * Gets process definitions
 *
 * @return {ModdleElement} definitions
 */
function getDefinitions() {
  const rootElement = canvas.getRootElement();
  const businessObject = rootElement.businessObject;

  return businessObject.$parent;
}

Hi Niklas. Thanks for your answer!

Unfortunately, I don’t get this to work correctly.

I added moddle: camunda-platform-to-cloud-migration/ConvertToCamundaCloudPlugin.js at main · berndruecker/camunda-platform-to-cloud-migration · GitHub

Then I try to access the definition, but don’t succeed:

bpmnDefinitions is undefined. I also tried in the console - but also without luck regarding your code proposal:

grafik

Any help appreciated :pray:
Thanks - Bernd!

In you context, your custom initialized bpmnJS instance won’t be the one of the Modeler - since you are initializing it outside of the service.

I think you can simply go by

export default function ConvertToCamundaCloudPlugin(elementRegistry, editorActions, canvas, modeling) {
  this._elementRegistry = elementRegistry;
  this._canvas = canvas;
  this._modeling = modeling;

 // somwhere in your service
 const definitions = getDefinitions(this._canvas);
}

// helper //////////////////

function getDefinitions(canvas) {
  const rootElement = canvas.getRootElement();
  const businessObject = rootElement.businessObject;

  return businessObject.$parent;
}

Note that you’re then using the Canvas service of the current bpmn-js Modeler where the plugin lives in.

1 Like

Works now - I injected the “canvas”

Then I can access the defintions later:

  console.log( this._canvas.getRootElement().businessObject.$parent );

See added definitionsElements variable · berndruecker/camunda-platform-to-cloud-migration@0d5aefd · GitHub

results in:

grafik

1 Like