3 namespace Drupal\comment\Plugin\Field\FieldFormatter;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Field\FieldDefinitionInterface;
8 use Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter;
11 * Plugin implementation of the 'comment_permalink' formatter.
13 * All the other entities use 'canonical' or 'revision' links to link the entity
14 * to itself but comments use permalink URL.
17 * id = "comment_permalink",
18 * label = @Translation("Comment Permalink"),
24 * "editor" = "plain_text"
28 class CommentPermalinkFormatter extends StringFormatter {
33 protected function getEntityUrl(EntityInterface $comment) {
34 /* @var $comment \Drupal\comment\CommentInterface */
35 $comment_permalink = $comment->permalink();
36 if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
37 $attributes = $comment_permalink->getOption('attributes') ?: [];
38 $attributes += ['title' => Unicode::truncate($body, 128)];
39 $comment_permalink->setOption('attributes', $attributes);
41 return $comment_permalink;
47 public static function isApplicable(FieldDefinitionInterface $field_definition) {
48 return parent::isApplicable($field_definition) && $field_definition->getTargetEntityTypeId() === 'comment' && $field_definition->getName() === 'subject';