Created
May 15, 2019 11:30
-
-
Save N-Parsons/faa3d85a64118c1e4a1f0125e2a56e87 to your computer and use it in GitHub Desktop.
Grav plugin/function to determine whether there is an explicit summary in the page content. This allows you to safely slice the summary off using "{{ content|slice(page.summary|length)|raw }}" if "page.header.has_explicit_summary" is true.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Grav\Plugin; | |
use Grav\Common\Plugin; | |
use \RocketTheme\Toolbox\Event\Event; | |
class SummaryExplicitnessPlugin extends Plugin | |
{ | |
public static function getSubscribedEvents() | |
{ | |
return [ | |
'onPageContentRaw' => ['setSummaryExplicitness', 0], | |
]; | |
} | |
/** | |
* Check whether the page has an explicit summary | |
* | |
* Based on: https://discourse.getgrav.org/t/page-with-or-without-summary/2611/12 | |
*/ | |
public function setSummaryExplicitness(Event $event) | |
{ | |
// Get the current raw content | |
$page = $event['page']; | |
$raw = $page->getRawContent(); | |
$summaryseparator = $this->config->get("site.summary.delimiter"); | |
$summaryseparatorpos = mb_strpos($raw, $summaryseparator); | |
if ($summaryseparatorpos) { | |
$page->header()->has_explicit_summary = true; | |
} | |
else { | |
$page->header()->has_explicit_summary = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've reformatted this as a plugin to share here, but I actually include the function within my theme, since the theme relies on it.
To utilise this in a page template, use something like: