You are here

function disqus_node_links_alter in Disqus 8

Implements hook_node_links_alter().

File

./disqus.module, line 54
The Disqus Drupal module.

Code

function disqus_node_links_alter(array &$node_links, NodeInterface $node, array &$context) {
  $fields = \Drupal::service('disqus.manager')
    ->getFields('node');
  foreach ($fields as $field_name => $detail) {

    // Skip fields that the node does not have.
    if (!$node
      ->hasField($field_name)) {
      continue;
    }
    $links = [];
    if ($node
      ->get($field_name)->status) {
      if (\Drupal::currentUser()
        ->hasPermission('view disqus comments')) {
        if ($context['view_mode'] === 'teaser') {

          // Display the Disqus link.
          $links['disqus_comments_num'] = [
            'title' => t('Comments'),
            'url' => $node
              ->toUrl(),
            'fragment' => 'disqus_thread',
            'attributes' => [
              // Identify the node for Disqus with the unique identifier:
              // http://docs.disqus.com/developers/universal/#comment-count
              'data-disqus-identifier' => 'node/' . $node
                ->id(),
            ],
          ];
        }
        $node_links['disqus'] = [
          '#theme' => 'links',
          '#links' => $links,
          '#attributes' => [
            'class' => [
              'links',
              'inline',
            ],
          ],
        ];

        // Attach disqus library to load the Disqus comment count JavaScript.
        $node_links['#attached']['library'][] = 'disqus/disqus';
        $node_links['disqus']['#attached']['drupalSettings']['disqusComments'] = \Drupal::config('disqus.settings')
          ->get('disqus_domain');
      }
    }
  }
}