is there any way that by code can assign a type of content to a workflow? I have reviewed the workflow module but I have not managed to find a method or a service that allows me to do it.

What I am trying to emulate is the action that makes this modal (programmatically not visually):

enter image description here

The only way I have found is by manipulating the workflows.workflow.myworkflow.yml

Per I'm trying to find a cleaner way.

Does anybody have an idea?

1 Answer 1


I found a way, I would like to know your opinion about how to do it.

No doubt you have to take into account additional validations, but basically to save a type of content to a workflow would be something like this:

/** @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $this->entityTypeManager->getStorage("workflow")->loadByProperties(["id" => "editorial"])["editorial"];
$config = $workflow->getTypePlugin()->getConfiguration();
$config["entity_types"]["node"][] = "article";

At the moment I'm just setting contents types, for blocks it's something similar. Greetings.

  • Excellent! This worked for me from within a .install file and associated hook_install (for a custom profile). One minor change needed simply in initializing the entityTypeManager, where I used the core static method "\Drupal::entityTypeManager()" Sep 22, 2020 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.