You are here

public function AdminModeration::unpublish in moderated content bulk publish 8

Same name and namespace in other branches
  1. 2.0.x src/AdminModeration.php \Drupal\moderated_content_bulk_publish\AdminModeration::unpublish()
  2. 1.0.x src/AdminModeration.php \Drupal\moderated_content_bulk_publish\AdminModeration::unpublish()

Unpublish current revision.

File

src/AdminModeration.php, line 32

Class

AdminModeration
A Helper Class to assist with the publishing and unpublishing bulk action.

Namespace

Drupal\moderated_content_bulk_publish

Code

public function unpublish() {
  $user = \Drupal::currentUser();
  $allLanguages = AdminHelper::getAllEnabledLanguages();
  foreach ($allLanguages as $langcode => $languageName) {
    if ($this->entity
      ->hasTranslation($langcode)) {
      \Drupal::logger('moderated_content_bulk_publish')
        ->notice(utf8_encode("Unpublish {$langcode} for " . $this->id . " in moderated_content_bulk_publish"));
      $this->entity = $this->entity
        ->getTranslation($langcode);
      $this->entity
        ->set('moderation_state', 'archived');
      if ($this->entity instanceof RevisionLogInterface) {

        // $now = time();
        $this->entity
          ->setRevisionCreationTime(\Drupal::time()
          ->getRequestTime());
        $msg = 'Bulk operation create archived revision';
        $this->entity
          ->setRevisionLogMessage($msg);
        $current_uid = \Drupal::currentUser()
          ->id();
        $this->entity
          ->setRevisionUserId($current_uid);
      }
      $this->entity
        ->setSyncing(TRUE);
      $this->entity
        ->setRevisionTranslationAffected(TRUE);
      if ($user
        ->hasPermission('moderated content bulk unpublish')) {
        $this->entity
          ->save();
      }
      else {
        \Drupal::logger('moderated_content_bulk_publish')
          ->notice(utf8_encode("Bulk unpublish not permitted, check permissions"));
      }
    }
  }
  foreach ($allLanguages as $langcode => $languageName) {
    if ($this->entity
      ->hasTranslation($langcode)) {
      $this->entity = $this->entity
        ->getTranslation($langcode);
      $this->entity
        ->set('moderation_state', 'draft');
      if ($this->entity instanceof RevisionLogInterface) {

        // $now = time();
        $this->entity
          ->setRevisionCreationTime(\Drupal::time()
          ->getRequestTime());
        $msg = 'Bulk operation create draft revision';
        $this->entity
          ->setRevisionLogMessage($msg);
        $current_uid = \Drupal::currentUser()
          ->id();
        $this->entity
          ->setRevisionUserId($current_uid);
      }
      $this->entity
        ->setRevisionTranslationAffected(TRUE);
      if ($user
        ->hasPermission('moderated content bulk unpublish')) {
        $this->entity
          ->save();
      }
      else {
        \Drupal::logger('moderated_content_bulk_publish')
          ->notice(utf8_encode("Bulk unpublish not permitted, check permissions."));
      }
    }
  }
  return $this->entity;
}