To make content management easier, for a large website we have allowed the content administrators to put a pipe in the title of a piece of content. Anything before the pipe is ignored.

Is there a way I can alter Drupal's $node->getTitle() function so, whenever it is called, I can strip out the text and the pipe symbol (instead of having to remove the pipe in each place in code or templates individually).

I'm basically after a Drupal version of Magento's "after" plugins.

  • 2
    If you want to classify content is much better to add a taxonomy field or another field. Then, you can create administrative views and manage nodes according to that field if you want.
    – sanzante
    Aug 15, 2019 at 17:43
  • 2
    $node->getTitle() is called every time the code needs the node title. It's not clear what you mean by instead of doing this wherever the title is called.
    – apaderno
    Aug 15, 2019 at 17:51
  • 2
    Yeah, like @sanzante suggests: Add another field, hide it from display, and use that for your editorial workflow. Could be a textfield or a Boolean, whatever. If you really want to stick to the node title being used for that, you can add a pseudo/extra field just for display, hide the real node title and use the pseudo field for displaying your stripped title instead. But that would be a little bit more advanced. Or you could also introduce a new field for the title and add a custom field formatter to sanitize the field's output before display.
    – leymannx
    Aug 15, 2019 at 17:59
  • Pipe symbol??? Sounds like your theme is adding this.
    – No Sssweat
    Aug 15, 2019 at 22:38
  • 1
    The problem is, we have "section group" and "section" content types. Sections can have the same title (e.g. "Overview" is very common). These pieces of content can't be identified in an autocomplete node reference field by anything other then their title, so taxonomy tagging or other fields wouldn't work. I'd like it all to work seamlessly off the raw node title.
    – Geat
    Aug 15, 2019 at 22:39


Your Answer

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

Browse other questions tagged or ask your own question.