How to alter Read more link for a node

Components

If you want to alter the title of the Read more link or perhaps add some CSS classes, you can do that by using the hook_node_links_alter() hook.

use Drupal\node\NodeInterface;

function MY_MODULE_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
  if (!empty($links['node']['#links']['node-readmore']['title'])) {
    $links['node']['#links']['node-readmore']['title'] = t('View Details');
    $links['node']['#links']['node-readmore']['attributes']['class'][] = 'view-details-link';
  }
}

You can do this just for some content types, by checking the bundle value: if ($entity->bundle() == 'article') {}

About the Author

Goran Nikolovski is a web and AI developer with over 10 years of expertise in PHP, Drupal, Python, JavaScript, React, and React Native. He founded this website and enjoys sharing his knowledge.