Commit f9ba6216 authored by anon's avatar anon

Issue #2827154 by jonathanshaw, anon: Stop rendering field token as plain text if field if empty

parent 36cfee19
......@@ -379,7 +379,7 @@ class EntityMatcher extends ConfigurableMatcherBase {
* The metadata for this entity.
*/
protected function buildDescription(EntityInterface $entity) {
$description = \Drupal::token()->replace($this->configuration['metadata'], [$this->targetType => $entity], []);
$description = \Drupal::token()->replace($this->configuration['metadata'], [$this->targetType => $entity], ['clear' => TRUE]);
return LinkitXss::descriptionFilter($description);
}
......
......@@ -86,4 +86,24 @@ class FileMatcherTest extends LinkitKernelTestBase {
$this->assertEquals(2, count($suggestions->getSuggestions()), 'Correct number of suggestions with multiple file extension filter.');
}
/**
* Tests file matcher with tokens in the matcher metadata.
*/
public function testTermMatcherWidthMetadataTokens() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:file', [
'settings' => [
'metadata' => '[file:fid] [file:field_with_no_value]',
],
]);
$suggestionCollection = $plugin->execute('Lorem');
/** @var \Drupal\linkit\Suggestion\EntitySuggestion[] $suggestions */
$suggestions = $suggestionCollection->getSuggestions();
foreach ($suggestions as $suggestion) {
$this->assertNotContains('[file:fid]', $suggestion->getDescription(), 'Raw token "[file:fid]" is not present in the description');
$this->assertNotContains('[file:field_with_no_value]', $suggestion->getDescription(), 'Raw token "[file:field_with_no_value]" is not present in the description');
}
}
}
......@@ -136,4 +136,25 @@ class NodeMatcherTest extends LinkitKernelTestBase {
$this->assertEquals(4, count($suggestions->getSuggestions()), 'Correct number of suggestions');
}
/**
* Tests node matcher with tokens in the matcher metadata.
*/
public function testNodeMatcherWidthMetadataTokens() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:node', [
'settings' => [
'metadata' => '[node:nid] [node:field_with_no_value]',
],
]);
$suggestionCollection = $plugin->execute('Lorem');
/** @var \Drupal\linkit\Suggestion\EntitySuggestion[] $suggestions */
$suggestions = $suggestionCollection->getSuggestions();
foreach ($suggestions as $suggestion) {
$this->assertNotContains('[node:nid]', $suggestion->getDescription(), 'Raw token "[node:nid]" is not present in the description');
$this->assertNotContains('[node:field_with_no_value]', $suggestion->getDescription(), 'Raw token "[node:field_with_no_value]" is not present in the description');
}
}
}
......@@ -78,6 +78,27 @@ class TermMatcherTest extends LinkitKernelTestBase {
$this->assertEquals(3, count($suggestions->getSuggestions()), 'Correct number of suggestions');
}
/**
* Tests term matcher with tokens in the matcher metadata.
*/
public function testTermMatcherWidthMetadataTokens() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:taxonomy_term', [
'settings' => [
'metadata' => '[term:tid] [term:field_with_no_value]',
],
]);
$suggestionCollection = $plugin->execute('Lorem');
/** @var \Drupal\linkit\Suggestion\EntitySuggestion[] $suggestions */
$suggestions = $suggestionCollection->getSuggestions();
foreach ($suggestions as $suggestion) {
$this->assertNotContains('[term:nid]', $suggestion->getDescription(), 'Raw token "[term:nid]" is not present in the description');
$this->assertNotContains('[term:field_with_no_value]', $suggestion->getDescription(), 'Raw token "[term:field_with_no_value]" is not present in the description');
}
}
/**
* Creates and saves a vocabulary.
*
......
......@@ -109,4 +109,25 @@ class UserMatcherTest extends LinkitKernelTestBase {
$this->assertEquals(1, count($suggestions->getSuggestions()), 'Correct number of suggestions');
}
/**
* Tests user matcher with tokens in the matcher metadata.
*/
public function testTermMatcherWidthMetadataTokens() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:user', [
'settings' => [
'metadata' => '[user:uid] [term:field_with_no_value]',
],
]);
$suggestionCollection = $plugin->execute('Lorem');
/** @var \Drupal\linkit\Suggestion\EntitySuggestion[] $suggestions */
$suggestions = $suggestionCollection->getSuggestions();
foreach ($suggestions as $suggestion) {
$this->assertNotContains('[user:uid]', $suggestion->getDescription(), 'Raw token "[user:nid]" is not present in the description');
$this->assertNotContains('[user:field_with_no_value]', $suggestion->getDescription(), 'Raw token "[user:field_with_no_value]" is not present in the description');
}
}
}
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