Skip to content

Commit

Permalink
move the compat stuff to a dedicated taxonomy filter and function
Browse files Browse the repository at this point in the history
  • Loading branch information
rossriley committed Sep 8, 2017
1 parent aed31e0 commit 7600cfb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 90 deletions.
2 changes: 1 addition & 1 deletion app/theme_defaults/_sub_taxonomylinks.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if record.taxonomy is defined %}
{% for type, values in record.taxonomy|taxonomy_compat %}
{% for type, values in record|taxonomy %}
<em>
{% if values|length < 2 %}
{{ config.get('taxonomy')[type].singular_name }}:
Expand Down
9 changes: 0 additions & 9 deletions src/Provider/TwigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ public function register(Application $app)
$app['twig.runtime.bolt'] = function ($app) {
return new Twig\Runtime\BoltRuntime($app['query']);
};
$app['twig.runtime.bolt_compat'] = function ($app) {
return new Twig\Runtime\CompatRuntime($app['config']);
};
$app['twig.runtime.bolt_html'] = function ($app) {
return new Twig\Runtime\HtmlRuntime(
$app['config'],
Expand Down Expand Up @@ -112,7 +109,6 @@ function () {
return [
Twig\Runtime\AdminRuntime::class => 'twig.runtime.bolt_admin',
Twig\Runtime\BoltRuntime::class => 'twig.runtime.bolt',
Twig\Runtime\CompatRuntime::class => 'twig.runtime.bolt_compat',
Twig\Runtime\HtmlRuntime::class => 'twig.runtime.bolt_html',
Twig\Runtime\ImageRuntime::class => 'twig.runtime.bolt_image',
Twig\Runtime\RecordRuntime::class => 'twig.runtime.bolt_record',
Expand Down Expand Up @@ -181,7 +177,6 @@ function (Environment $twig, $app) {
$twig->addExtension($app['twig.extension.bolt']);
$twig->addExtension($app['twig.extension.bolt_admin']);
$twig->addExtension($app['twig.extension.bolt_array']);
$twig->addExtension($app['twig.extension.bolt_compat']);
$twig->addExtension($app['twig.extension.bolt_html']);
$twig->addExtension($app['twig.extension.bolt_image']);
$twig->addExtension($app['twig.extension.bolt_record']);
Expand Down Expand Up @@ -224,10 +219,6 @@ function (Environment $twig, $app) {
return new Extension\ArrayExtension();
};

$app['twig.extension.bolt_compat'] = function () {
return new Extension\CompatExtension();
};

$app['twig.extension.bolt_html'] = function () {
return new Extension\HtmlExtension();
};
Expand Down
32 changes: 0 additions & 32 deletions src/Twig/Extension/CompatExtension.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/Twig/Extension/RecordExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getFunctions()
new TwigFunction('listtemplates', [Runtime\RecordRuntime::class, 'listTemplates']),
new TwigFunction('pager', [Runtime\RecordRuntime::class, 'pager'], $env + $safe),
new TwigFunction('trimtext', [Runtime\RecordRuntime::class, 'excerpt'], $safe + $deprecated + ['alternative' => 'excerpt']),
new TwigFunction('taxonomy', [Runtime\RecordRuntime::class, 'taxonomy']),
// @codingStandardsIgnoreEnd
];
}
Expand All @@ -51,6 +52,7 @@ public function getFilters()
new TwigFilter('excerpt', [Runtime\RecordRuntime::class, 'excerpt'], $safe),
new TwigFilter('selectfield', [Runtime\RecordRuntime::class, 'selectField']),
new TwigFilter('trimtext', [Runtime\RecordRuntime::class, 'excerpt'], $safe + $deprecated + ['alternative' => 'excerpt']),
new TwigFilter('taxonomy', [Runtime\RecordRuntime::class, 'taxonomy']),
// @codingStandardsIgnoreEnd
];
}
Expand Down
48 changes: 0 additions & 48 deletions src/Twig/Runtime/CompatRuntime.php

This file was deleted.

48 changes: 48 additions & 0 deletions src/Twig/Runtime/RecordRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Bolt\Helpers\Excerpt;
use Bolt\Legacy\Content;
use Bolt\Pager\PagerManager;
use Bolt\Storage\Collection\Taxonomy;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\Glob;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -312,4 +313,51 @@ public function selectField($content, $fieldName, $startempty = false, $keyName
}
return $retval;
}

/**
*
*
* @param array|Taxonomy|Content|\Bolt\Storage\Entity\Content $candidate
* @return array
*
*/
public function taxonomy($candidate)
{
/**
* If this is a legacy content object then we set the candidate to the taxonomy array
*/
if ($candidate instanceof Content) {
$candidate = $candidate->taxonomy;
}

/**
* If it's a content entity then fetch the taxonomy field
*/
if ($candidate instanceof \Bolt\Storage\Entity\Content) {
$candidate = $candidate->getTaxonomy();
}

/**
* By this point we should have either an old-style array of taxonomies or a new-style Taxonomy Collection
* if the former then we can return at this point
*/
if (is_array($candidate)) {
return $candidate;
}

/**
* Finally just as a safeguard, if for any reason we don't have at this point a Taxonomy collection we return
* and empty array so we can guarantee the return type.
*/
if (!$candidate instanceof Taxonomy) {
return [];
}

$compiled = [];
foreach ($candidate as $el) {
$compiled[$el->getTaxonomytype()]['/' . $el->getTaxonomytype() . '/' . $el->getSlug()] = $el->getSlug();
}

return $compiled;
}
}

0 comments on commit 7600cfb

Please sign in to comment.