You are here

protected function NodeNewComments::renderLink in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/comment/src/Plugin/views/field/NodeNewComments.php \Drupal\comment\Plugin\views\field\NodeNewComments::renderLink()
  2. 10 core/modules/comment/src/Plugin/views/field/NodeNewComments.php \Drupal\comment\Plugin\views\field\NodeNewComments::renderLink()

Prepares the link to the first new comment.

Parameters

string $data: The XSS safe string for the link text.

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

Return value

string Returns a string for the link text.

1 call to NodeNewComments::renderLink()
NodeNewComments::render in core/modules/comment/src/Plugin/views/field/NodeNewComments.php
Renders the field.

File

core/modules/comment/src/Plugin/views/field/NodeNewComments.php, line 186

Class

NodeNewComments
Field handler to display the number of new comments.

Namespace

Drupal\comment\Plugin\views\field

Code

protected function renderLink($data, ResultRow $values) {
  if (!empty($this->options['link_to_comment']) && $data !== NULL && $data !== '') {
    $node_type = $this
      ->getValue($values, 'type');
    $node = Node::create([
      'nid' => $this
        ->getValue($values, 'nid'),
      'type' => $node_type,
    ]);

    // Because there is no support for selecting a specific comment field to
    // reference, we arbitrarily use the first such field name we find.
    // @todo Provide a means for selecting the comment field.
    //   https://www.drupal.org/node/2594201
    $field_map = $this->entityFieldManager
      ->getFieldMapByFieldType('comment');
    $comment_field_name = 'comment';
    foreach ($field_map['node'] as $field_name => $field_data) {
      foreach ($field_data['bundles'] as $bundle_name) {
        if ($node_type == $bundle_name) {
          $comment_field_name = $field_name;
          break 2;
        }
      }
    }
    $page_number = $this->entityTypeManager
      ->getStorage('comment')
      ->getNewCommentPageNumber($this
      ->getValue($values, 'comment_count'), $this
      ->getValue($values), $node, $comment_field_name);
    $this->options['alter']['make_link'] = TRUE;
    $this->options['alter']['url'] = $node
      ->toUrl();
    $this->options['alter']['query'] = $page_number ? [
      'page' => $page_number,
    ] : NULL;
    $this->options['alter']['fragment'] = 'new';
  }
  return $data;
}