class Tags

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Utility/Tags.php \Drupal\Component\Utility\Tags
  2. 8.9.x core/lib/Drupal/Component/Utility/Tags.php \Drupal\Component\Utility\Tags
  3. 11.x core/lib/Drupal/Component/Utility/Tags.php \Drupal\Component\Utility\Tags

Defines a class that can explode and implode tags.

Hierarchy

  • class \Drupal\Component\Utility\Tags

Expanded class hierarchy of Tags

Related topics

10 files declare their use of Tags
EntityAutocomplete.php in core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
EntityAutocompleteController.php in core/modules/system/src/Controller/EntityAutocompleteController.php
EntityAutocompleteMatcher.php in core/lib/Drupal/Core/Entity/EntityAutocompleteMatcher.php
EntityAutocompleteTest.php in core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php
TagsTest.php in core/tests/Drupal/Tests/Core/Common/TagsTest.php

... See full list

130 string references to 'Tags'
550f86ad-aa11-4047-953f-636d42889f85.yml in core/tests/fixtures/default_content/taxonomy_term/550f86ad-aa11-4047-953f-636d42889f85.yml
core/tests/fixtures/default_content/taxonomy_term/550f86ad-aa11-4047-953f-636d42889f85.yml
AddModerationConfigActionTest::testAddEntityTypeAndBundle in core/modules/content_moderation/tests/src/Kernel/ConfigAction/AddModerationConfigActionTest.php
CommentTokenReplaceTest::testCommentTokenReplacement in core/modules/comment/tests/src/Functional/CommentTokenReplaceTest.php
Creates a comment, then tests the tokens generated from it.
ContentImportTest::assertContentWasImported in core/tests/Drupal/FunctionalTests/DefaultContent/ContentImportTest.php
Asserts that the default content was imported as expected.
ContentImportTest::setUp in core/tests/Drupal/FunctionalTests/DefaultContent/ContentImportTest.php

... See full list

File

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

Namespace

Drupal\Component\Utility
View source
class Tags {
  
  /**
   * Explodes a string of tags into an array.
   *
   * @param string $tags
   *   A string to explode.
   *
   * @return array
   *   An array of tags.
   */
  public static function explode($tags) {
    // This regexp allows the following types of user input:
    // this, "somecompany, llc", "and ""this"" w,o.rks", foo bar
    $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
    preg_match_all($regexp, $tags, $matches);
    $typed_tags = array_unique($matches[1]);
    $tags = [];
    foreach ($typed_tags as $tag) {
      // If a user has escaped a term (to demonstrate that it is a group,
      // or includes a comma or quote character), we remove the escape
      // formatting so to save the term into the database as the user intends.
      $tag = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\\1', $tag)));
      if ($tag != "") {
        $tags[] = $tag;
      }
    }
    return $tags;
  }
  
  /**
   * Encodes a tag string, taking care of special cases like commas and quotes.
   *
   * @param string $tag
   *   A tag string.
   *
   * @return string
   *   The encoded string.
   */
  public static function encode($tag) {
    if (str_contains($tag, ',') || str_contains($tag, '"')) {
      return '"' . str_replace('"', '""', $tag) . '"';
    }
    return $tag;
  }
  
  /**
   * Implodes an array of tags into a string.
   *
   * @param array $tags
   *   An array of tags.
   *
   * @return string
   *   The imploded string.
   */
  public static function implode($tags) {
    $encoded_tags = [];
    foreach ($tags as $tag) {
      $encoded_tags[] = self::encode($tag);
    }
    return implode(', ', $encoded_tags);
  }

}

Members

Title Sort descending Modifiers Object type Summary
Tags::encode public static function Encodes a tag string, taking care of special cases like commas and quotes.
Tags::explode public static function Explodes a string of tags into an array.
Tags::implode public static function Implodes an array of tags into a string.

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