How to add custom class in custom module? I am not able to add custom class in custom module, I need to add class "ajax-link" in the anchor link to make it ajax based page load  .    

function hook_node_view(array &amp;$build, \Drupal\node\Entity\Node 
$node, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, 
 $view_mode) {

 if ($display-&gt;getComponent('prevnext_next')) {
$build['prevnext_next'] = [
  '#theme' =&gt; 'prevnext',
  '#direction' =&gt; 'next',
  '#text' =&gt; t('Next'),
  '#nid' =&gt; $previous_next['next'],
  '#attributes' =&gt; array(
  'class' =&gt; array(
  'ajax-link' 
      ),
    ),
  '#url' =&gt; Url::fromUserInput('/node/' . $previous_next['next']."?an=left")-&gt;toString(),
  '#void' =&gt; empty($previous_next['next']),
];
 }
   }


What am I doing wrong in the code ?

I want to create link something like this:

&lt;a class="ajax-link" href="node/nid?an=left"&gt;Next&lt;/a&gt;
you can try code :

  $url = Url::fromRoute('entity.node.canonical', ['node' =&gt; $previous_next['next'],'an'=&gt;'left']);
  $url-&gt;setOptions([
      'attributes' =&gt; [
          'class' =&gt; ['ajax-link custom-class']
      ],
  ]);

  $build['prevnext_next'] = [
      '#type' =&gt; 'link',
      '#title' =&gt; t('Next'),
      '#url' =&gt;  $url,
  ];