Comment::getFormatted PHP Method

getFormatted() public method

Note that we won't apply this to get() when $page->outputFormatting is active in order for backwards compatibility with older installations.
public getFormatted ( $key ) : mixed | null | Page | string
$key
return mixed | null | Page | string
    public function getFormatted($key)
    {
        $value = $this->get($key);
        if ($key == 'text') {
            $textformatters = null;
            // $textformatters = $this->field ? $this->field->textformatters : null; // @todo
            if (is_array($textformatters) && count($textformatters)) {
                // output formatting with specified textformatters
                foreach ($textformatters as $name) {
                    if (!($textformatter = $this->wire('modules')->get($name))) {
                        continue;
                    }
                    $textformatter->formatValue($this->page, $this->field, $value);
                }
            } else {
                // default output formatting
                $value = $this->wire('sanitizer')->entities(trim($value));
                $value = str_replace("\n\n", "</p><p>", $value);
                $value = str_replace("\n", "<br />", $value);
            }
        } else {
            if (in_array($key, array('cite', 'email', 'user_agent', 'website'))) {
                $value = $this->wire('sanitizer')->entities(trim($value));
            }
        }
        return $value;
    }

Usage Example

コード例 #1
0
 /**
  * Render the comment
  *
  * This is the default rendering for development/testing/demonstration purposes
  *
  * It may be used for production, but only if it meets your needs already. Typically you'll want to render the comments
  * using your own code in your templates. 
  *
  * @see CommentArray::render()
  * @return string 
  *
  */
 public function renderItem(Comment $comment, $depth = 0)
 {
     $text = $comment->getFormatted('text');
     $cite = $comment->getFormatted('cite');
     $gravatar = '';
     if ($this->options['useGravatar']) {
         $imgUrl = $comment->gravatar($this->options['useGravatar'], $this->options['useGravatarImageset']);
         if ($imgUrl) {
             $gravatar = "\n\t\t<img class='CommentGravatar' src='{$imgUrl}' alt='{$cite}' />";
         }
     }
     $website = '';
     if ($comment->website) {
         $website = $comment->getFormatted('website');
     }
     if ($website) {
         $cite = "<a href='{$website}' rel='nofollow' target='_blank'>{$cite}</a>";
     }
     $created = wireDate($this->options['dateFormat'], $comment->created);
     if (empty($this->options['commentHeader'])) {
         $header = "<span class='CommentCite'>{$cite}</span> <small class='CommentCreated'>{$created}</small> ";
         if ($this->options['useVotes']) {
             $header .= $this->renderVotes($comment);
         }
     } else {
         $header = str_replace(array('{cite}', '{created}'), array($cite, $created), $this->options['commentHeader']);
         if (strpos($header, '{votes}') !== false) {
             $header = str_replace('{votes}', $this->renderVotes($comment), $header);
         }
     }
     $liClass = '';
     $replies = $this->options['depth'] > 0 ? $this->renderList($comment->id, $depth + 1) : '';
     if ($replies) {
         $liClass .= ' CommentHasReplies';
     }
     if ($comment->status == Comment::statusPending) {
         $liClass .= ' CommentStatusPending';
     } else {
         if ($comment->status == Comment::statusSpam) {
             $liClass .= ' CommentStatusSpam';
         }
     }
     $out = "\n\t<li id='Comment{$comment->id}' class='CommentListItem{$liClass}' data-comment='{$comment->id}'>" . $gravatar . "\n\t\t<p class='CommentHeader'>{$header}</p>" . "\n\t\t<div class='CommentText'>" . "\n\t\t\t<p>{$text}</p>" . "\n\t\t</div>";
     if ($this->options['usePermalink']) {
         $permalink = $comment->getPage()->httpUrl;
         $urlSegmentStr = $this->wire('input')->urlSegmentStr;
         if ($urlSegmentStr) {
             $permalink .= rtrim($permalink, '/') . $urlSegmentStr . '/';
         }
         $permalink .= '#Comment' . $comment->id;
         $permalink = "<a class='CommentActionPermalink' href='{$permalink}'>" . $this->_('Permalink') . "</a>";
     } else {
         $permalink = '';
     }
     if ($this->options['depth'] > 0 && $depth < $this->options['depth']) {
         $out .= "\n\t\t<div class='CommentFooter'>" . "\n\t\t\t<p class='CommentAction'>" . "\n\t\t\t\t<a class='CommentActionReply' data-comment-id='{$comment->id}' href='#Comment{$comment->id}'>" . $this->_('Reply') . "</a> " . ($permalink ? "\n\t\t\t\t{$permalink}" : "") . "\n\t\t\t</p>" . "\n\t\t</div>";
         if ($replies) {
             $out .= $replies;
         }
     } else {
         $out .= "\n\t\t<div class='CommentFooter'></div>";
     }
     $out .= "\n\t</li>";
     return $out;
 }