diff --git a/metatag.module b/metatag.module index 9f4e0e0..0a53c0d 100644 --- a/metatag.module +++ b/metatag.module @@ -16,6 +16,10 @@ use Drupal\Core\Url; use Drupal\taxonomy\TermInterface; use Drupal\Component\Utility\Html; +// Multiple value token delimiter, to be used instead of the default comma +// delimiter, to help distinguish multiple value delimiters from normal commas. +define('METATAG_MULTIPLE_VALUE_DELIMITER', '||'); + /** * Implements hook_help(). */ @@ -132,6 +136,11 @@ function metatag_page_attachments(array &$attachments) { $head_links = []; foreach ($metatag_attachments['#attached']['html_head'] as $item) { + // Replace multiple value delimiter with commas if the delimiter hasn't + // already been replaced by some other module. + if (array_key_exists('#attributes', $item) && is_string($item['#attributes']['content'])) { + $items['#attributes']['content'] = str_replace(METATAG_MULTIPLE_VALUE_DELIMITER, ', ', $item['#attributes']['content']); + } $attachments['#attached']['html_head'][] = $item; // Also add a HTTP header "Link:" for canonical URLs and shortlinks. diff --git a/src/MetatagManager.php b/src/MetatagManager.php index 35b375b..a812b4f 100644 --- a/src/MetatagManager.php +++ b/src/MetatagManager.php @@ -543,9 +543,15 @@ class MetatagManager implements MetatagManagerInterface { // that needs to be filtered and converted to a string. // @see Robots::setValue() $tag->setValue($value); - $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); - $processed_value = PlainTextOutput::renderFromHtml(htmlspecialchars_decode($this->tokenService->replace($tag->value(), $token_replacements, ['langcode' => $langcode]))); + // Create options for handling token replacements, setting the current + // language and a custom delimiter for multiple value fields in tokens. + $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); + $options = [ + 'langcode' => $langcode, + 'join' => METATAG_MULTIPLE_VALUE_DELIMITER, + ]; + $processed_value = PlainTextOutput::renderFromHtml(htmlspecialchars_decode($this->tokenService->replace($tag->value(), $token_replacements, $options))); // Now store the value with processed tokens back into the plugin. $tag->setValue($processed_value); diff --git a/src/Plugin/metatag/Tag/MetaNameBase.php b/src/Plugin/metatag/Tag/MetaNameBase.php index 9212571..8f09348 100644 --- a/src/Plugin/metatag/Tag/MetaNameBase.php +++ b/src/Plugin/metatag/Tag/MetaNameBase.php @@ -254,7 +254,7 @@ abstract class MetaNameBase extends PluginBase { // Optional handling for items that allow multiple values. if (!empty($this->multiple)) { - $form['#description'] .= ' ' . $this->t('Multiple values may be used, separated by a comma. Note: Tokens that return multiple values will be handled automatically.'); + $form['#description'] .= ' ' . $this->t('Multiple values may be used, separated by ":delimiter". Note: Tokens that return multiple values will be handled automatically.', [':delimiter' => METATAG_MULTIPLE_VALUE_DELIMITER]); } // Optional handling for images. @@ -321,7 +321,7 @@ abstract class MetaNameBase extends PluginBase { // Parse out the image URL, if needed. $value = $this->parseImageUrl(); - $values = $this->multiple() ? explode(',', $value) : [$value]; + $values = $this->multiple() ? explode(METATAG_MULTIPLE_VALUE_DELIMITER, $value) : [$value]; $elements = []; foreach ($values as $value) { $value = $this->tidy($value); @@ -387,7 +387,7 @@ abstract class MetaNameBase extends PluginBase { if ($this->multiple()) { // Split the string into an array, remove empty items. - $values = array_filter(explode(',', $value)); + $values = array_filter(explode(METATAG_MULTIPLE_VALUE_DELIMITER, $value)); } else { $values = [$value]; @@ -401,7 +401,7 @@ abstract class MetaNameBase extends PluginBase { $values[$key] = $matches[1]; } } - $value = implode(',', $values); + $value = implode(METATAG_MULTIPLE_VALUE_DELIMITER, $values); // Remove any HTML tags that might remain. $value = strip_tags($value); diff --git a/tests/src/Kernel/MetatagManagerTest.php b/tests/src/Kernel/MetatagManagerTest.php index f086bef..f2cbe0e 100644 --- a/tests/src/Kernel/MetatagManagerTest.php +++ b/tests/src/Kernel/MetatagManagerTest.php @@ -81,7 +81,7 @@ class MetatagManagerTest extends KernelTestBase { $tags = $metatag_manager->generateElements([ 'og_image_width' => 100, 'og_image_height' => 100, - 'og_image_url' => 'http://www.example.com/example/foo.png, http://www.example.com/example/foo2.png', + 'og_image_url' => 'http://www.example.com/example/foo.png' . METATAG_MULTIPLE_VALUE_DELIMITER . 'http://www.example.com/example/foo2.png', ]); $expected = [