Skip to content

Commit

Permalink
Added version shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Oct 16, 2020
1 parent fff8aba commit b5c4759
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
31 changes: 31 additions & 0 deletions classes/Shortcodes/VersionShortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Grav\Plugin\Shortcodes;

use Grav\Common\Language\Language;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;

class VersionShortcode extends Shortcode
{

public function init()
{
$this->shortcode->getHandlers()->add('version', function(ShortcodeInterface $sc) {
$lang = $this->getBbCode($sc);

if ($lang) {
$list = explode(',', $lang);
array_walk($list, 'trim');

/** @var Language $language */
$language = $this->grav['language'];
$current = $language->getLanguage();

if (in_array($current, $list, true)) {
return $sc->getContent();
}
}

return '';
});
}
}
23 changes: 13 additions & 10 deletions learn4.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
namespace Grav\Theme;

use Grav\Common\Theme;
use Grav\Plugin\ShortcodeManager;
use Grav\Theme\Learn4\Shortcodes\VersionShortcode;
use Pimple\Exception\UnknownIdentifierException;
use RocketTheme\Toolbox\Event\Event;

class Learn4 extends Theme
{
public static function getSubscribedEvents() {
public static function getSubscribedEvents(): array
{
return [
'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
'onTwigInitialized' => ['onTwigInitialized', 0],
'onTwigPageVariables' => ['onTwigPageVariables', 0],
'onTNTSearchQuery' => ['onTNTSearchQuery', 1000],
Expand All @@ -20,7 +24,7 @@ public function onTwigPageVariables()
$this->grav['twig']->twig_vars['grav_version'] = GRAV_VERSION;
}

public function onTNTSearchQuery(Event $e)
public function onTNTSearchQuery(Event $e): void
{
$query = $this->grav['uri']->param('q');

Expand All @@ -33,16 +37,15 @@ public function onTNTSearchQuery(Event $e)
}
}

public function onTwigInitialized()
public function onShortcodeHandlers(): void
{
try {
$sc = $this->grav['shortcode'];
$sc->getHandlers()->addAlias('version', 'lang');
} catch (UnknownIdentifierException $e) {

}

/** @var ShortcodeManager $sc */
$sc = $this->grav['shortcode'];
$sc->registerAllShortcodes(__DIR__ . '/classes/Shortcodes');
}

public function onTwigInitialized(): void
{
$twig = $this->grav['twig'];

$form_class_variables = [
Expand Down

0 comments on commit b5c4759

Please sign in to comment.