Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

There were 7 usages in CORE using the same code to encode Tags. It was the same code copied and pasted over and over again. It made hard to maintain the code and less possible to add a custom encode rule to the Tags.

In Drupal 8 we have added a static method encode to the \Drupal\Component\Utility\Tags class that can be used when tags encoding is needed.

New class method:: \Drupal\Component\Utility\Tags::encode(string $tag)

When you need to use tags in an autocomplete widget that your module is implementing (or any code where tags are imploded and exploded), they should be escaped to take care of special cases like comas and quotes. Example taken from taxonomy.module:

use Drupal\Component\Utility\Tags;

function taxonomy_implode_tags($tags, $vid = NULL) {
  // Make sure we have a completed loaded taxonomy term.
  if ($tag instanceof EntityInterface && $label = $tag->label()) {
    // Commas and quotes in tag names are special cases, so encode 'em.
    $typed_tags[] = Tags::encode($label);
  }
}
Impacts: 
Module developers