class PublishContentPublishEntity in Publish Content 8
Toggles node status.
Hierarchy
- class \Drupal\publishcontent\Controller\PublishContentPublishEntity implements ContainerInjectionInterface uses StringTranslationTrait
Expanded class hierarchy of PublishContentPublishEntity
File
- src/
Controller/ PublishContentPublishEntity.php, line 17
Namespace
Drupal\publishcontent\ControllerView source
class PublishContentPublishEntity implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The ServerBag object from the current request.
*
* @var \Symfony\Component\HttpFoundation\ServerBag
*/
protected $server;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The current user service.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The module configuration for reading.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* The logging channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* {@inheritdoc}
*/
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;
}
/**
* Toggle node status.
*
* @param \Drupal\node\NodeInterface $node
* The node being toggled.
* @param string $langcode
* The language code of the node.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Redirect to the previous page.
*/
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);
}
/**
* A custom route access callback for the Publish/Unpublish local task UI.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function hasUILocalTask() {
return AccessResult::allowedIf(!empty($this->config) && !empty($this->config
->get('ui_localtask')));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PublishContentPublishEntity:: |
protected | property | The module configuration for reading. | |
PublishContentPublishEntity:: |
protected | property | The current user service. | |
PublishContentPublishEntity:: |
protected | property | The language manager. | |
PublishContentPublishEntity:: |
protected | property | The logging channel. | |
PublishContentPublishEntity:: |
protected | property | The messenger. | |
PublishContentPublishEntity:: |
protected | property | The ServerBag object from the current request. | |
PublishContentPublishEntity:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
PublishContentPublishEntity:: |
public | function | A custom route access callback for the Publish/Unpublish local task UI. | |
PublishContentPublishEntity:: |
public | function | Toggle node status. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |