You are here

public function PublishContentNode::render in Publish Content 8

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/PublishContentNode.php, line 63

Class

PublishContentNode
Field handler to flag the node type.

Namespace

Drupal\publishcontent\Plugin\views\field

Code

public function render(ResultRow $values) {

  /* @var \Drupal\node\NodeInterface $node */
  $node = $values->_entity;

  // Sanity check.
  if (!$node instanceof NodeInterface) {
    return '';
  }

  // Don't bother adding the link if the access is forbidden.
  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);
}