Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / comment / src / Plugin / Field / FieldFormatter / CommentPermalinkFormatter.php
1 <?php
2
3 namespace Drupal\comment\Plugin\Field\FieldFormatter;
4
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;
9
10 /**
11  * Plugin implementation of the 'comment_permalink' formatter.
12  *
13  * All the other entities use 'canonical' or 'revision' links to link the entity
14  * to itself but comments use permalink URL.
15  *
16  * @FieldFormatter(
17  *   id = "comment_permalink",
18  *   label = @Translation("Comment Permalink"),
19  *   field_types = {
20  *     "string",
21  *     "uri",
22  *   },
23  *   quickedit = {
24  *     "editor" = "plain_text"
25  *   }
26  * )
27  */
28 class CommentPermalinkFormatter extends StringFormatter {
29
30   /**
31    * {@inheritdoc}
32    */
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);
40     }
41     return $comment_permalink;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function isApplicable(FieldDefinitionInterface $field_definition) {
48     return parent::isApplicable($field_definition) && $field_definition->getTargetEntityTypeId() === 'comment' && $field_definition->getName() === 'subject';
49   }
50
51 }