You are here

class Tags in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 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
Contains \Drupal\Core\Entity\Element\EntityAutocomplete.
EntityAutocompleteController.php in core/modules/system/src/Controller/EntityAutocompleteController.php
Contains \Drupal\system\Controller\EntityAutocompleteController.
EntityAutocompleteMatcher.php in core/lib/Drupal/Core/Entity/EntityAutocompleteMatcher.php
Contains \Drupal\Core\Entity\EntityAutocompleteMatcher.
EntityAutocompleteTest.php in core/modules/system/src/Tests/Entity/EntityAutocompleteTest.php
Contains \Drupal\system\Tests\Entity\EntityAutocompleteTest.
TagsTest.php in core/tests/Drupal/Tests/Core/Common/TagsTest.php
Contains \Drupal\Tests\Core\Common\TagsTest.

... See full list

18 string references to 'Tags'
BreadcrumbTest::testBreadCrumbs in core/modules/system/src/Tests/Menu/BreadcrumbTest.php
Tests breadcrumbs on node and administrative paths.
EntityReferenceFieldAttributesTest::setUp in core/modules/rdf/src/Tests/EntityReferenceFieldAttributesTest.php
Sets up a Drupal site for running functional and integration tests.
field.field.node.article.field_tags.yml in core/profiles/standard/config/install/field.field.node.article.field_tags.yml
core/profiles/standard/config/install/field.field.node.article.field_tags.yml
ForumTest::doAdminTests in core/modules/forum/src/Tests/ForumTest.php
Runs admin tests on the admin user.
LegacyTest::setUp in core/modules/taxonomy/src/Tests/LegacyTest.php
Sets up a Drupal site for running functional and integration tests.

... See full list

File

core/lib/Drupal/Component/Utility/Tags.php, line 15
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;
  }

  /**
   * 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 (strpos($tag, ',') !== FALSE || strpos($tag, '"') !== FALSE) {
      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 = array();
    foreach ($tags as $tag) {
      $encoded_tags[] = self::encode($tag);
    }
    return implode(', ', $encoded_tags);
  }

}

Members

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