2

Building on this answer:

/** * Implements hook_preprocess_links(). */ function bartik_preprocess_links(&$variables) { if (isset($variables['links']['node-readmore'])) { $variables['links']['node-readmore']['link']['#title'] = t('Read more custom text'); } } 

does anyone know how to build the if statement to further limit the change in the read more link to a particular content type? I'm working in Drupal 8, not 7.

2 Answers 2

2

Add following to your THEMENAME.theme file

/** * Implements hook_preprocess_HOOK() for node.html.twig. */ function THEMENAME_preprocess_node(&$variables) { // ... if ($variables['teaser']) { $node_type = &drupal_static('node_type'); $node_type = $variables['node']->getType(); } } /** * Implements hook_preprocess_links(). */ function THEMENAME_preprocess_links(&$variables) { $node_type = &drupal_static('node_type'); if (isset($variables['links']['node-readmore'])) { // Change link title according to machine name of node_type if ($node_type == 'article') { $variables['links']['node-readmore']['link']['#title'] = t('Custom Text'); } } } 

Clear Cache.

0

I was able to make this work by getting the bundle from the URL object that's part of the link:

function THEMENAME_preprocess_links(&$variables) { if (isset($variables['links']) && (isset($variables['links']['node-readmore'])) && (isset($variables['links']['node-readmore']['link'])) && (isset($variables['links']['node-readmore']['link']['#url']))) { if ($entity = $variables['links']['node-readmore']['link']['#url']->getOption('entity')) { if (($bundle = $entity->bundle()) && ($bundle == 'article')) $variables['links']['node-readmore']['link']['#title'] = t('Read the article'); } } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.