Permalink
Browse files

feature(views): elgg_get_excerpt output now comes from a view

fixes #8925
  • Loading branch information...
jdalsem committed Apr 15, 2016
1 parent d6f5f17 commit 4d6ec3f272ebed3ebf6abe2e0439e596e69fc6e1
Showing with 44 additions and 24 deletions.
  1. +7 −24 engine/lib/output.php
  2. +37 −0 views/default/output/excerpt.php
View
@@ -70,28 +70,11 @@ function elgg_autop($string) {
* @since 1.7.2
*/
function elgg_get_excerpt($text, $num_chars = 250) {
$text = trim(elgg_strip_tags($text));
$string_length = elgg_strlen($text);
if ($string_length <= $num_chars) {
return $text;
}
// handle cases
$excerpt = elgg_substr($text, 0, $num_chars);
$space = elgg_strrpos($excerpt, ' ', 0);
// don't crop if can't find a space.
if ($space === false) {
$space = $num_chars;
}
$excerpt = trim(elgg_substr($excerpt, 0, $space));
if ($string_length != elgg_strlen($excerpt)) {
$excerpt .= '...';
}
return $excerpt;
$vars = [
'text' => $text,
'num_chars' => $num_chars,
];
return elgg_view('output/excerpt', $vars);
}
/**
@@ -122,7 +105,7 @@ function elgg_format_bytes($size, $precision = 2) {
}
$base = log($size) / log(1024);
$suffixes = array('B', 'kB', 'MB', 'GB', 'TB');
$suffixes = array('B', 'kB', 'MB', 'GB', 'TB');
return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
}
@@ -517,7 +500,7 @@ function _elgg_html_decode($string) {
/**
* Prepares query string for output to prevent CSRF attacks.
*
*
* @param string $string
* @return string
*
@@ -0,0 +1,37 @@
<?php
/**
* Output view for elgg_get_excerpt
*
* @uses $vars['text'] The text to get the excerpt for
* @uses $vars['num_chars'] The max number of characters of the excerpt
* @uses $vars['suffix'] The suffix to be added if text is cut
*/
$text = elgg_extract('text', $vars);
$text = trim(elgg_strip_tags($text));
$suffix = elgg_extract('suffix', $vars, '...');
$string_length = elgg_strlen($text);
$num_chars = (int) elgg_extract('num_chars', $vars, 250);
if ($string_length <= $num_chars) {
echo $text;
return;
}
// handle cases
$excerpt = elgg_substr($text, 0, $num_chars);
$space = elgg_strrpos($excerpt, ' ', 0);
// don't crop if can't find a space.
if ($space === false) {
$space = $num_chars;
}
$excerpt = trim(elgg_substr($excerpt, 0, $space));
if ($string_length != elgg_strlen($excerpt)) {
$excerpt .= $suffix;
}
echo $excerpt;

0 comments on commit 4d6ec3f

Please sign in to comment.