Edit "Read more" and "Add new comment" links in teaser I'd like to add a couple classes to the "Read more" and "Add new comment" links in my article teasers in Drupal 8.

I'd do this in Drupal 7 by doing something like this in my theme file.

function mytheme_preprocess_node(&amp;$variables) {
  $variables['content']['links']['node']['#links']['node-readmore']['attributes']['class'][] = 'btn btn-primary';
  $variables['content']['links']['node']['#links']['comment-add']['attributes']['class'][] = 'btn btn-primary';
}


But this doesn't work in Drupal 8. I dumped the contents of $variables['content']['links'] and all I got was this:

$variables['...']['...'] array(2)
    '#lazy_builder' =&gt; array(2)
        string(40) "Drupal\node\NodeViewBuilder::renderLinks"
        array(4)
            string(1) "4"
            string(6) "teaser"
            string(2) "en"
            boolFALSE
    '#weight' =&gt; integer100


So how would I go about adding classes to these links in Drupal 8?
You can do this in a couple of different ways. By looking up the Drupal\node\NodeViewBuilder::renderLinks method, you can see how it works.

1. Implement hook_node_links_alter()

This should work for a module.

function mymodule_node_links_alter(array &amp;$links, NodeInterface $entity, array &amp;$context) {
  $links['node']['#links']['node-readmore']['#attributes']['class'][] = 'btn btn-primary';
}


2. Implement template_preprocess_links()

And I think this should work for the theme...

function mytheme_preprocess_links(&amp;$variables) {
  // some logic enclosing this block
  $variables['node']['#links']['node-readmore']['#attributes']['class'][] = 'btn btn-primary';
}