Commit a1af9fdc authored by alexpott's avatar alexpott

Issue #2468375 by tduong, joaquin.solino, Berdir, David4514: Status Reports:...

Issue #2468375 by tduong, joaquin.solino, Berdir, David4514: Status Reports: The following token types are not defined but have tokens - $info['types']['entity']
parent e5908d6b
......@@ -7,6 +7,8 @@
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Render\BubbleableMetadata;
/**
......@@ -19,15 +21,28 @@ function comment_token_info() {
'needs-data' => 'comment',
);
// @todo Make this work per field. See https://www.drupal.org/node/2031903.
$entity['comment-count'] = array(
'name' => t("Comment count"),
'description' => t("The number of comments posted on an entity."),
);
$entity['comment-count-new'] = array(
'name' => t("New comment count"),
'description' => t("The number of comments posted on an entity since the reader last viewed it."),
);
$tokens = [];
// Provide a integration for each entity type except comment.
foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type_id == 'comment' || !$entity_type->isSubclassOf(ContentEntityInterface::class)) {
continue;
}
if (\Drupal::service('comment.manager')->getFields($entity_type_id)) {
// Get the correct token type.
$token_type = ($entity_type_id == 'taxonomy_term') ? 'term' : $entity_type_id;
// @todo Make this work per field. See https://www.drupal.org/node/2031903.
$tokens[$token_type]['comment-count'] = [
'name' => t("Comment count"),
'description' => t("The number of comments posted on an entity."),
];
$tokens[$token_type]['comment-count-new'] = [
'name' => t("New comment count"),
'description' => t("The number of comments posted on an entity since the reader last viewed it."),
];
}
}
// Core comment tokens
$comment['cid'] = array(
......@@ -97,9 +112,8 @@ function comment_token_info() {
return array(
'types' => array('comment' => $type),
'tokens' => array(
'entity' => $entity,
'comment' => $comment,
),
) + $tokens,
);
}
......@@ -235,9 +249,10 @@ function comment_tokens($type, $tokens, array $data, array $options, BubbleableM
$replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options, $bubbleable_metadata);
}
}
elseif ($type == 'entity' & !empty($data['entity'])) {
// Replacement tokens for any content entities that have comment field.
elseif (!empty($data[$type]) && $data[$type] instanceof FieldableEntityInterface) {
/** @var $entity \Drupal\Core\Entity\FieldableEntityInterface */
$entity = $data['entity'];
$entity = $data[$type];
foreach ($tokens as $name => $original) {
switch ($name) {
......
......@@ -111,7 +111,7 @@ public function postComment($entity, $comment, $subject = '', $contact = NULL, $
$edit['comment_body[0][value]'] = $comment;
if ($entity !== NULL) {
$field = FieldConfig::loadByName('node', $entity->bundle(), $field_name);
$field = FieldConfig::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name);
}
else {
$field = FieldConfig::loadByName('node', 'article', $field_name);
......@@ -120,7 +120,7 @@ public function postComment($entity, $comment, $subject = '', $contact = NULL, $
// Must get the page before we test for fields.
if ($entity !== NULL) {
$this->drupalGet('comment/reply/node/' . $entity->id() . '/' . $field_name);
$this->drupalGet('comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name);
}
// Determine the visibility of subject form field.
......
......@@ -2,12 +2,16 @@
namespace Drupal\comment\Tests;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
/**
* Generates text using placeholders for dummy content to check comment token
......@@ -16,6 +20,12 @@
* @group comment
*/
class CommentTokenReplaceTest extends CommentTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['taxonomy'];
/**
* Creates a comment, then tests the tokens generated from it.
*/
......@@ -27,6 +37,12 @@ function testCommentTokenReplacement() {
'language' => $language_interface,
);
// Setup vocabulary.
Vocabulary::create([
'vid' => 'tags',
'name' => 'Tags',
])->save();
// Change the title of the admin user.
$this->adminUser->name->value = 'This is a title with some special & > " stuff.';
$this->adminUser->save();
......@@ -122,18 +138,43 @@ function testCommentTokenReplacement() {
$input = '[comment:author]';
$output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId()));
$this->assertEqual($output, Html::escape($author_name), format_string('Comment author token %token replaced.', array('%token' => $input)));
// Load node so comment_count gets computed.
// Add comment field to user and term entities.
$this->addDefaultCommentField('user', 'user', 'comment', CommentItemInterface::OPEN, 'comment_user');
$this->addDefaultCommentField('taxonomy_term', 'tags', 'comment', CommentItemInterface::OPEN, 'comment_term');
// Create a user and a comment.
$user = User::create(['name' => 'alice']);
$user->save();
$this->postComment($user, 'user body', 'user subject', TRUE);
// Create a term and a comment.
$term = Term::create([
'vid' => 'tags',
'name' => 'term',
]);
$term->save();
$this->postComment($term, 'term body', 'term subject', TRUE);
// Load node, user and term again so comment_count gets computed.
$node = Node::load($node->id());
$user = User::load($user->id());
$term = Term::load($term->id());
// Generate comment tokens for the node (it has 2 comments, both new).
// Generate comment tokens for node (it has 2 comments, both new),
// user and term.
$tests = array();
$tests['[entity:comment-count]'] = 2;
$tests['[entity:comment-count-new]'] = 2;
$tests['[node:comment-count]'] = 2;
$tests['[node:comment-count-new]'] = 2;
$tests['[user:comment-count]'] = 1;
$tests['[user:comment-count-new]'] = 1;
$tests['[term:comment-count]'] = 1;
$tests['[term:comment-count-new]'] = 1;
foreach ($tests as $input => $expected) {
$output = $token_service->replace($input, array('entity' => $node, 'node' => $node), array('langcode' => $language_interface->getId()));
$this->assertEqual($output, $expected, format_string('Node comment token %token replaced.', array('%token' => $input)));
$output = $token_service->replace($input, ['entity' => $node, 'node' => $node, 'user' => $user, 'term' => $term], ['langcode' => $language_interface->getId()]);
$this->assertEqual($output, $expected, format_string('Comment token %token replaced.', ['%token' => $input]));
}
}
......
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