Bolt\Helpers\Excerpt::getExcerpt PHP Method

getExcerpt() public method

Get the excerpt of a given piece of text.
public getExcerpt ( integer $length = 200, boolean $includeTitle = false, array | string | null $focus = null ) : string | null
$length integer
$includeTitle boolean
$focus array | string | null
return string | null
    public function getExcerpt($length = 200, $includeTitle = false, $focus = null)
    {
        $title = null;
        if ($includeTitle && $this->title !== null) {
            $title = Html::trimText(strip_tags($this->title), $length);
            $length = $length - strlen($title);
        }
        if ($this->body instanceof Content) {
            $this->body = $this->body->getValues();
        }
        if (is_array($this->body)) {
            // Assume it's an array, strip some common fields that we don't need, implode the rest.
            $stripKeys = ['id', 'slug', 'datepublish', 'datedepublish', 'datecreated', 'datechanged', 'username', 'ownerid', 'title', 'contenttype', 'status', 'taxonomy', 'templatefields'];
            $excerpt = '';
            array_walk($this->body, function ($value, $key) use(&$excerpt, $stripKeys) {
                if (is_string($value) && !in_array($key, $stripKeys)) {
                    $excerpt .= $value . ' ';
                }
            });
        } elseif (is_string($this->body) || is_object($this->body) && method_exists($this->body, '__toString')) {
            // otherwise we just use the string.
            $excerpt = (string) $this->body;
        } else {
            // Nope, got nothing.
            $excerpt = '';
        }
        $excerpt = str_replace('>', '> ', $excerpt);
        if (empty($focus)) {
            $excerpt = Html::trimText(strip_tags($excerpt), $length);
        } else {
            $excerpt = $this->extractRelevant($focus, strip_tags($excerpt), $length);
        }
        if (!empty($title)) {
            $excerpt = '<b>' . $title . '</b> ' . $excerpt;
        }
        return trim($excerpt);
    }

Usage Example

コード例 #1
0
ファイル: ContentValuesTrait.php プロジェクト: archayl/bolt
 /**
  * Create an excerpt for the content.
  *
  * @param integer      $length
  * @param boolean      $includeTitle
  * @param string|array $focus
  *
  * @return \Twig_Markup
  */
 public function getExcerpt($length = 200, $includeTitle = false, $focus = null)
 {
     $excerptParts = [];
     if (!empty($this->contenttype['fields'])) {
         foreach ($this->contenttype['fields'] as $key => $field) {
             // Skip empty fields, and fields used as 'title'.
             if (!isset($this->values[$key]) || in_array($key, $this->getTitleColumnName())) {
                 continue;
             }
             // add 'text', 'html' and 'textarea' fields.
             if (in_array($field['type'], ['text', 'html', 'textarea'])) {
                 $excerptParts[] = $this->values[$key];
             }
             // add 'markdown' field
             if ($field['type'] === 'markdown') {
                 $excerptParts[] = $this->app['markdown']->text($this->values[$key]);
             }
         }
     }
     $excerpter = new Excerpt(implode(' ', $excerptParts), $this->getTitle());
     $excerpt = $excerpter->getExcerpt($length, $includeTitle, $focus);
     return new \Twig_Markup($excerpt, 'UTF-8');
 }
All Usage Examples Of Bolt\Helpers\Excerpt::getExcerpt