function Tags::encode

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Utility/Tags.php \Drupal\Component\Utility\Tags::encode()
  2. 8.9.x core/lib/Drupal/Component/Utility/Tags.php \Drupal\Component\Utility\Tags::encode()
  3. 10 core/lib/Drupal/Component/Utility/Tags.php \Drupal\Component\Utility\Tags::encode()

Encodes a tag string, taking care of special cases like commas and quotes.

Parameters

string $tag: A tag string.

Return value

string The encoded string.

4 calls to Tags::encode()
EntityAutocomplete::getEntityLabels in core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
Converts an array of entity objects into a string of entity labels.
EntityAutocompleteMatcher::getMatches in core/lib/Drupal/Core/Entity/EntityAutocompleteMatcher.php
Gets matched labels based on a given search string.
EntityAutocompleteTest::testEntityReferenceAutocompletion in core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php
Tests autocompletion edge cases with slashes in the names.
Tags::implode in core/lib/Drupal/Component/Utility/Tags.php
Implodes an array of tags into a string.

File

core/lib/Drupal/Component/Utility/Tags.php, line 51

Class

Tags
Defines a class that can explode and implode tags.

Namespace

Drupal\Component\Utility

Code

public static function encode($tag) {
    if (str_contains($tag, ',') || str_contains($tag, '"')) {
        return '"' . str_replace('"', '""', $tag) . '"';
    }
    return $tag;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.