You are here

protected function NodeNewComments::renderLink in Zircon Profile 8.0

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()

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 162
Contains \Drupal\comment\Plugin\views\field\NodeNewComments.

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 = entity_create('node', array(
      '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
    $entity_manager = \Drupal::entityManager();
    $field_map = $entity_manager
      ->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 = $entity_manager
      ->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
      ->urlInfo();
    $this->options['alter']['query'] = $page_number ? array(
      'page' => $page_number,
    ) : NULL;
    $this->options['alter']['fragment'] = 'new';
  }
  return $data;
}