class Tags

Defines a class that can explode and implode tags.

Hierarchy

  • class \Drupal\Component\Utility\Tags

Expanded class hierarchy of Tags

2 files declare their use of Tags
common.inc in drupal/core/includes/common.inc
Common functions that many Drupal modules will need to reference.
TagsTest.php in drupal/core/tests/Drupal/Tests/Core/Common/TagsTest.php
Contains \Drupal\Tests\Core\Common\TagsTest.
9 string references to 'Tags'
BreadcrumbTest::testBreadCrumbs in drupal/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php
Tests breadcrumbs on node and administrative paths.
field.instance.node.article.field_tags.yml in drupal/core/profiles/standard/config/field.instance.node.article.field_tags.yml
drupal/core/profiles/standard/config/field.instance.node.article.field_tags.yml
ForumTest::doAdminTests in drupal/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php
Runs admin tests on the admin user.
ManageFieldsTest::setUp in drupal/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php
Sets up a Drupal site for running functional and integration tests.
ManageFieldsTest::testDeleteTaxonomyField in drupal/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php
Tests that deletion removes fields and instances as expected for a term.

... See full list

File

drupal/core/lib/Drupal/Component/Utility/Tags.php, line 13
Contains \Drupal\Component\Utility\Tags.

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 = array();
    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;
  }

  /**
   * 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 = array();
    foreach ($tags as $tag) {

      // Commas and quotes in tag names are special cases, so encode them.
      if (strpos($tag, ',') !== FALSE || strpos($tag, '"') !== FALSE) {
        $tag = '"' . str_replace('"', '""', $tag) . '"';
      }
      $encoded_tags[] = $tag;
    }
    return implode(', ', $encoded_tags);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.