PublishContentPublishEntity.php in Publish Content 8
File
src/Controller/PublishContentPublishEntity.php
View source
<?php
namespace Drupal\publishcontent\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Url;
class PublishContentPublishEntity implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $server;
protected $languageManager;
protected $messenger;
protected $currentUser;
protected $config;
protected $logger;
public static function create(ContainerInterface $container) {
$instance = new static();
$instance->server = $container
->get('request_stack')
->getCurrentRequest()->server;
$instance->languageManager = $container
->get('language_manager');
$instance->messenger = $container
->get('messenger');
$instance->config = $container
->get('config.factory')
->get('publishcontent.settings');
$instance->currentUser = $container
->get('current_user');
$instance->logger = $container
->get('logger.factory')
->get('publishcontent');
return $instance;
}
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);
}
public function hasUILocalTask() {
return AccessResult::allowedIf(!empty($this->config) && !empty($this->config
->get('ui_localtask')));
}
}