You are here

function _workbench_email_content_moderation_event_shim in Workbench Email 8

Same name and namespace in other branches
  1. 2.x workbench_email.module \_workbench_email_content_moderation_event_shim()

Shim for content moderation event.

@todo Remove when https://www.drupal.org/project/drupal/issues/2873287 is in.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Entity being updated/inserted.

See also

https://www.drupal.org/project/drupal/issues/2873287#comment-12619624

2 calls to _workbench_email_content_moderation_event_shim()
workbench_email_content_moderation_state_insert in ./workbench_email.module
Implements hook_ENTITY_TYPE_insert for content_moderation_state.
workbench_email_content_moderation_state_update in ./workbench_email.module
Implements hook_ENTITY_TYPE_update for content_moderation_state.

File

./workbench_email.module, line 239
Provides main module functions.

Code

function _workbench_email_content_moderation_event_shim(ContentModerationState $entity) {
  if (!\Drupal::moduleHandler()
    ->moduleExists('content_moderation') || class_exists('\\Drupal\\content_moderation\\Event\\ContentModerationStateChangedEvent')) {

    // https://www.drupal.org/project/drupal/issues/2873287 will add this class
    // to core.
    return;
  }
  $entityStorage = \Drupal::entityTypeManager()
    ->getStorage($entity->content_entity_type_id->value);
  $moderationStateStorage = \Drupal::entityTypeManager()
    ->getStorage($entity
    ->getEntityTypeId());
  $moderated_entity = $entityStorage
    ->loadRevision($entity->content_entity_revision_id->value);

  /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
  $moderation_info = Drupal::service('content_moderation.moderation_information');

  // Check to see if the content is moderated or not
  $is_moderated = $moderation_info
    ->isModeratedEntity($moderated_entity);
  if (!$is_moderated) {
    return;
  }
  if (!$entity
    ->getLoadedRevisionId()) {
    $original_state = FALSE;
  }
  else {
    $original_content_moderation_state = $moderationStateStorage
      ->loadRevision($entity
      ->getLoadedRevisionId());
    if (!$entity
      ->isDefaultTranslation() && $original_content_moderation_state
      ->hasTranslation($entity->activeLangcode)) {
      $original_content_moderation_state = $original_content_moderation_state
        ->getTranslation($entity->activeLangcode);
    }
    $original_state = $original_content_moderation_state->moderation_state->value;
  }
  $new_state = $entity->moderation_state->value;
  if ($original_state !== $new_state) {
    $workflow = $entity->workflow->target_id;
    \Drupal::service('event_dispatcher')
      ->dispatch('content_moderation.state_changed', new ContentModerationStateChangedEvent($moderated_entity, $new_state, $original_state, $workflow));
  }
}