PublishContentNode.php in Publish Content 8
File
src/Plugin/views/field/PublishContentNode.php
View source
<?php
namespace Drupal\publishcontent\Plugin\views\field;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PublishContentNode extends FieldPluginBase {
protected $publishContentAccess;
protected $currentUser;
protected $config;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->publishContentAccess = $container
->get('publishcontent.access');
$instance->currentUser = $container
->get('current_user');
$instance->config = $container
->get('config.factory')
->get('publishcontent.settings');
return $instance;
}
public function query() {
}
public function render(ResultRow $values) {
$node = $values->_entity;
if (!$node instanceof NodeInterface) {
return '';
}
if ($this->publishContentAccess
->access($this->currentUser, $node)
->isForbidden()) {
return '';
}
$langcode = $values->{'node_field_data_langcode'} ?? '';
$id = $node
->id();
if ($node
->isTranslatable() && !empty($langcode) && $node
->hasTranslation($langcode)) {
$url = Url::fromRoute('entity.node.publish_translation', [
'node' => $id,
'langcode' => $langcode,
]);
$text = $node
->getTranslation($langcode)
->isPublished() ? $this->config
->get('unpublish_text_value') : $this->config
->get('publish_text_value');
}
else {
$url = Url::fromRoute('entity.node.publish', [
'node' => $id,
]);
$text = $node
->isPublished() ? $this->config
->get('unpublish_text_value') : $this->config
->get('publish_text_value');
}
$link = Link::fromTextAndUrl($text, $url);
$render_array = $link
->toRenderable();
return $this
->getRenderer()
->render($render_array);
}
}