CommentPermalinkFormatter.php
Same filename in other branches
Namespace
Drupal\comment\Plugin\Field\FieldFormatterFile
-
core/
modules/ comment/ src/ Plugin/ Field/ FieldFormatter/ CommentPermalinkFormatter.php
View source
<?php
namespace Drupal\comment\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Plugin implementation of the 'comment_permalink' formatter.
*
* All the other entities use 'canonical' or 'revision' links to link the entity
* to itself but comments use permalink URL.
*/
class CommentPermalinkFormatter extends StringFormatter {
/**
* {@inheritdoc}
*/
protected function getEntityUrl(EntityInterface $comment) {
/** @var \Drupal\comment\CommentInterface $comment */
$comment_permalink = $comment->permalink();
if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
$attributes = $comment_permalink->getOption('attributes') ?: [];
$attributes += [
'title' => Unicode::truncate($body, 128),
];
$comment_permalink->setOption('attributes', $attributes);
}
return $comment_permalink;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return parent::isApplicable($field_definition) && $field_definition->getTargetEntityTypeId() === 'comment' && $field_definition->getName() === 'subject';
}
}
Classes
Title | Deprecated | Summary |
---|---|---|
CommentPermalinkFormatter | Plugin implementation of the 'comment_permalink' formatter. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.