I am trying to add some markup around the number of comments link in the standard node links. I want to do this so I can theme the number of comments as a little comment bubble and hide the text (possibly only on mobile).

I can get this far:

function mytheme_preprocess_links__node(&$variables) {

 if (isset($variables['links']['comment-comments'])) {
    $title = $variables['links']['comment-comments']['text'];
    $count = 7 // need to get the actual count in here.

    $variables['links']['comment-comments']['link']["#title"] = \Drupal::translation()->formatPlural($count, ''<span class="comment-count">1</span> <span class="comment-text">comment</span>', '<span class="commment-count">@count</span> <span class="comment-text">comments</span>');

When I dsm the $title variable I can see that the comment count is in there, I just can't figure out how to get it out into the $count variable. Any help?

1 Answer 1


Unfortunately the object PluralTranslatableMarkup has no method to get the protected value count. The serialization was fixed to provide the count value, see issue https://www.drupal.org/node/2844181, but the class is still missing it's own getArguments() to extend it with the count value. You could file an issue and wait for a fix or use this work around with reflection:

  if ($title instanceof \Drupal\Core\StringTranslation\PluralTranslatableMarkup) {
    $reflection = new ReflectionClass($title);
    $property = $reflection->getProperty('count');
    $count = $property->getValue($title);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.