You are here

public function PublishContentPublishEntity::toggleEntityStatus in Publish Content 8

Toggle node status.

Parameters

\Drupal\node\NodeInterface $node: The node being toggled.

string $langcode: The language code of the node.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse Redirect to the previous page.

1 string reference to 'PublishContentPublishEntity::toggleEntityStatus'
publishcontent.routing.yml in ./publishcontent.routing.yml
publishcontent.routing.yml

File

src/Controller/PublishContentPublishEntity.php, line 89

Class

PublishContentPublishEntity
Toggles node status.

Namespace

Drupal\publishcontent\Controller

Code

public function toggleEntityStatus(NodeInterface $node, $langcode = '') {
  try {
    if ($referrer = $this->server
      ->get('HTTP_REFERER')) {
      $redirectUrl = Url::fromUri($referrer, [
        'absolute' => TRUE,
      ])
        ->getUri();
    }
    else {
      $redirectUrl = $node
        ->toUrl()
        ->toString();
    }
  } catch (\Exception $e) {
    $redirectUrl = Url::fromRoute('<front>')
      ->setAbsolute()
      ->toString();
  }
  if ($node
    ->isTranslatable()) {
    if ($langcode == '') {
      $langcode = $this->languageManager
        ->getCurrentLanguage()
        ->getId();
    }
    if (!$node
      ->hasTranslation($langcode)) {
      $this->messenger
        ->addError($this
        ->t("You can't @publish/@unpublish a non-existing translation.", [
        '@publish' => $this->config
          ->get('publish_text_value'),
        '@unpublish' => $this->config
          ->get('unpublish_text_value'),
      ]));
      return new RedirectResponse($redirectUrl);
    }
    $node = $node
      ->getTranslation($langcode);
  }
  $node
    ->isPublished() ? $node
    ->setUnpublished() : $node
    ->setPublished();
  $isPublished = $node
    ->isPublished();
  $status = $isPublished ? $this->config
    ->get('publish_text_value') : $this->config
    ->get('unpublish_text_value');
  if (!empty($this->config)) {
    if ($this->config
      ->get('create_log_entry')) {
      $this->logger
        ->notice($this
        ->t('@type: @action @title', [
        '@type' => $node
          ->bundle(),
        '@action' => $isPublished ? 'unpublished' : 'published',
        '@title' => $node
          ->getTitle(),
      ]));
    }
    if ($this->config
      ->get('create_revision')) {
      $node
        ->setNewRevision(TRUE);
      $node->revision_log = $this
        ->t('Changed to @status by @user', [
        '@status' => $status,
        '@user' => $this->currentUser
          ->getDisplayName(),
      ]);
      $node
        ->setRevisionCreationTime(REQUEST_TIME);
      $node
        ->setRevisionUserId($this->currentUser
        ->id());
    }
  }
  try {
    $node
      ->save();
    $this->messenger
      ->addMessage($this
      ->t('@title has been set to @status', [
      '@title' => $node
        ->getTitle(),
      '@status' => $status,
    ]));
  } catch (EntityStorageException $e) {
  }
  return new RedirectResponse($redirectUrl);
}