Commit c9164063 authored by catch's avatar catch

Issue #2187263 by droplet: Improve consistency of Tags encode.

parent d85b2383
......@@ -42,6 +42,22 @@ public static function explode($tags) {
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.
*
......@@ -54,12 +70,7 @@ public static function explode($tags) {
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;
$encoded_tags[] = self::encode($tag);
}
return implode(', ', $encoded_tags);
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\entity_reference;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\entity_reference\Plugin\Type\SelectionPluginManager;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
......@@ -97,9 +98,7 @@ public function getMatches($field, $instance, $entity_type, $entity_id = '', $pr
// tags.
$key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
// Names containing commas or quotes must be wrapped in quotes.
if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
$key = '"' . str_replace('"', '""', $key) . '"';
}
$key = Tags::encode($key);
$matches[] = array('value' => $prefix . $key, 'label' => $label);
}
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\entity_reference\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\user\EntityOwnerInterface;
......@@ -130,9 +131,7 @@ protected function getLabels(FieldItemListInterface $items, $delta) {
$label = $entity_item->label();
$key = "$label ($entity_id)";
// Labels containing commas or quotes must be wrapped in quotes.
if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
$key = '"' . str_replace('"', '""', $key) . '"';
}
$key = Tags::encode($key);
$entity_labels[] = $key;
}
return $entity_labels;
......
......@@ -7,6 +7,7 @@
namespace Drupal\entity_reference\Tests;
use Drupal\Component\Utility\Tags;
use Drupal\entity_reference\EntityReferenceController;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
......@@ -101,11 +102,9 @@ function testEntityReferenceAutocompletion() {
// Try to autocomplete a entity label with both a comma and a slash.
$input = '"label with, and / t';
$data = $this->getAutocompleteResult('single', $input);
$n = $entity_3->name->value;
$n = $entity_3->name->value . ' (3)';
// Entity labels containing commas or quotes must be wrapped in quotes.
if (strpos($entity_3->name->value, ',') !== FALSE || strpos($entity_3->name->value, '"') !== FALSE) {
$n = '"' . str_replace('"', '""', $entity_3->name->value) . ' (3)"';
}
$n = Tags::encode($n);
$target = array(
'value' => $n,
'label' => check_plain($entity_3->name->value),
......
......@@ -195,11 +195,8 @@ protected function getMatchingTerms($tags_typed, array $vids, $tag_last) {
if (!empty($tids)) {
$terms = $this->termStorage->loadMultiple(array_keys($tids));
foreach ($terms as $term) {
$name = $term->label();
// Term names containing commas or quotes must be wrapped in quotes.
if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
$name = '"' . str_replace('"', '""', $name) . '"';
}
$name = Tags::encode($term->label());
$matches[] = array('value' => $prefix . $name, 'label' => String::checkPlain($term->label()));
}
return $matches;
......
......@@ -7,6 +7,7 @@
namespace Drupal\taxonomy\Tests;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Field\FieldDefinitionInterface;
/**
......@@ -279,11 +280,8 @@ function testTermAutocompletion() {
$input = '"term with, comma and / a';
$path = 'taxonomy/autocomplete/node/taxonomy_' . $this->vocabulary->id();
$this->drupalGet($path, array('query' => array('q' => $input)));
$n = $third_term->label();
// Term names containing commas or quotes must be wrapped in quotes.
if (strpos($third_term->label(), ',') !== FALSE || strpos($third_term->label(), '"') !== FALSE) {
$n = '"' . str_replace('"', '""', $third_term->label()) . '"';
}
$n = Tags::encode($third_term->label());
$target = array(array(
'value' => $n,
'label' => check_plain($third_term->label()),
......
......@@ -5,6 +5,7 @@
* Enables the organization of content into categories.
*/
use Drupal\Component\Utility\Tags;
use Drupal\Core\Entity\FieldableDatabaseStorageController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\file\FileInterface;
......@@ -779,12 +780,7 @@ 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.
if (strpos($label, ',') !== FALSE || strpos($label, '"') !== FALSE) {
$typed_tags[] = '"' . str_replace('"', '""', $label) . '"';
}
else {
$typed_tags[] = $label;
}
$typed_tags[] = Tags::encode($label);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment