3

Drupal 8's core comments module has a library "node-new-comments-link" with a file "node-new-comments-link.js".

This contains Drupal.behaviors.nodeNewCommentsLink = { ... } which calls another function in the library which in turn calls another function in the library function render(results) { ... }.

This render function controls the text used to show how many new comments a node has: .text(Drupal.formatPlural(results[nodeID].new_comment_count, '1 new comment', '@count new comments'))

I want to use different text. (I want it just to say 'New' rather than 'x new comments').

I can replace the whole js file or the whole library and that works fine, but it's very heavy handed. It requires me to copy paste lots of complex code I don't need to understand.

Is there a way to override just this one render function?

1
  • You're limited by what the language allows - If it's an anonymous/private function inside another function and it's not explicitly exposed, then no. If the function you want to replace is accessible to you, e.g. Is a method on a global object, then you can monkey patch it. Or, if it's acceptable for all instances of Drupal.formatPlural to have the same changes, just monkey patch that method instead. If you're wondering if there's a Drupal-specific helper method/API for this: very unlikely since requirements would be awkward to configure and even more awkward to actually achieve
    – Clive
    Feb 20 '17 at 18:19
2

You can enable interface translation for English in /admin/config/regional/language/edit/en

[x] Enable interface translation to English

and then you are able to change the string you are looking for in User Interface Translation.

1
  • I thought I had tried that... anyway, it works now. Thanks!
    – Jonathan
    Feb 20 '17 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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