I have a workflow to which I want to include entity/content types programmatically since these content types will be included to the system by the installation of the module to which they reside. I'm not sure how to load the specific workflow object and attach the entity type to that via code.
3 Answers
You need the addEntityTypeAndBundle method from ContentModerationInterface
A static way to implement it looks like:
$workflow = \Drupal::entityManager()->getStorage('workflow')->load([workflow_id]);
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'article');
$workflow->save();
I think you will need to load the workflow configuration as editable and modify the entity values in the type_settings.
Here's the quick code used to see how this was structured:
$config = \Drupal::config("workflows.workflow.editorial");
$settings = $config->get('type_settings');
print_r($settings['entity_types']);
Output:
Array
(
[node] => Array
(
[0] => basic_page
)
)
This is now entityTypeManager rather than entityManager:
// Add bundle to default workflow
$workflow = \Drupal::entityTypeManager()->getStorage('workflow_type')->load('workflow_id');
$workflow = Workflow::load('workflow_id');
$workflow->getTypePlugin()->addEntityTypeAndBundle("entity_type_id", "entity_bundle_id");
$workflow->save();