You are here

public function AdminPin::pin in moderated content bulk publish 8

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

Pin Content.

File

src/AdminPin.php, line 83

Class

AdminPin
A Helper Class to assist with the pin and unpin bulk action.

Namespace

Drupal\moderated_content_bulk_publish

Code

public function pin() {
  $user = \Drupal::currentUser();
  \Drupal::logger('PIN_UNPIN')
    ->notice(utf8_encode('Pin action in moderated_content_bulk_publish'));
  $allLanguages = AdminHelper::getAllEnabledLanguages();
  foreach ($allLanguages as $langcode => $languageName) {
    if ($this->entity
      ->hasTranslation($langcode)) {
      $this->entity = $this->entity
        ->getTranslation($langcode);
      $this->entity
        ->setSticky(TRUE);
      if ($this->entity instanceof RevisionLogInterface) {
        $this->entity
          ->setRevisionCreationTime(\Drupal::time()
          ->getRequestTime());
        $msg = 'Bulk operation pin content';
        $this->entity
          ->setRevisionLogMessage($msg);
        $current_uid = \Drupal::currentUser()
          ->id();
        $this->entity
          ->setRevisionUserId($current_uid);
      }
      if ($user
        ->hasPermission('moderated content bulk pin content')) {
        $this->entity
          ->save();
      }
      else {
        \Drupal::logger('moderated_content_bulk_publish')
          ->notice(utf8_encode("Bulk pin not permitted, check permissions."));
      }
      if (!$this->entity
        ->isSticky()) {
        $entity_manager = \Drupal::entityTypeManager();
        $this->entity = $entity_manager
          ->getStorage($this->entity
          ->getEntityTypeId())
          ->load($this->nid);
        $this->entity
          ->setSticky(TRUE);
        if ($this->entity instanceof RevisionLogInterface) {
          $this->entity
            ->setRevisionCreationTime(\Drupal::time()
            ->getRequestTime());
          $msg = 'Bulk operation pin content';
          $this->entity
            ->setRevisionLogMessage($msg);
          $current_uid = \Drupal::currentUser()
            ->id();
          $this->entity
            ->setRevisionUserId($current_uid);
        }
        if ($user
          ->hasPermission('moderated content bulk pin content')) {
          $this->entity
            ->save();
        }
        else {
          \Drupal::logger('moderated_content_bulk_publish')
            ->notice(utf8_encode("Bulk pin not permitted, check permissions."));
        }
        $this->entity = $entity_manager
          ->getStorage($this->entity
          ->getEntityTypeId())
          ->load($this->nid);
      }
    }
  }
  return $this->entity;
}