You are here

function _lightning_workflow_moderate_content_type in Lightning Workflow 8.3

Adds a content type to a moderation workflow.

@internal This function may be changed or removed at any time without warning. It should NOT be called by external code!

Parameters

\Drupal\node\NodeTypeInterface $node_type: The content type.

string $workflow_id: The workflow ID. The workflow must exist and use a plugin that implements \Drupal\content_moderation\Plugin\WorkflowType\ContentModerationInterface.

1 call to _lightning_workflow_moderate_content_type()
lightning_workflow_node_type_insert in ./lightning_workflow.module
Implements hook_ENTITY_TYPE_insert().

File

./lightning_workflow.module, line 110
Provides workflow enhancements for Drupal.

Code

function _lightning_workflow_moderate_content_type(NodeTypeInterface $node_type, $workflow_id) {
  $workflow = Workflow::load($workflow_id);
  if (empty($workflow)) {
    return;
  }
  $plugin = $workflow
    ->getTypePlugin();
  if ($plugin instanceof ContentModerationInterface) {
    $plugin
      ->addEntityTypeAndBundle('node', $node_type
      ->id());
    $workflow
      ->save();

    // The moderation_history view depends on the existence of the
    // moderation_state base field, which is only defined once a content type
    // has been opted into moderation. Now that's done, so create the
    // moderation_history view if it doesn't already exist.
    if (Drupal::moduleHandler()
      ->moduleExists('views')) {
      $view = View::load('moderation_history');
      if (empty($view)) {
        $values = file_get_contents(__DIR__ . '/config/dynamic/views.view.moderation_history.yml');
        $values = Yaml::decode($values);
        View::create($values)
          ->save();
      }
    }

    // We need to rebuild all routes because Content Moderation needs to ensure
    // that edit forms load the latest revision, and that the moderation_history
    // view's routes are registered if needed.
    Drupal::service('router.builder')
      ->rebuild();
  }
}