Skip to content

Commit

Permalink
Merge pull request #6972 from rossriley/fix/move-ishome-to-controller
Browse files Browse the repository at this point in the history
New Storage Layer - Add Compatibility features to allow both systems to work concurrently
  • Loading branch information
GwendolenLynch committed Sep 9, 2017
2 parents 727da30 + 0d79537 commit b556ab3
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 49 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 %}
{% for type, values in record|taxonomy %}
<em>
{% if values|length < 2 %}
{{ config.get('taxonomy')[type].singular_name }}:
Expand Down
33 changes: 33 additions & 0 deletions src/Controller/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ protected function render($template, array $context = [], array $globals = [])
}
$context += $globals;

$this->addResolvedRoute($context, $template->getTemplateName());

if ($this->getOption('general/compatibility/template_view', false)) {
return new TemplateView($template->getTemplateName(), $context);
}
Expand All @@ -104,6 +106,37 @@ protected function render($template, array $context = [], array $globals = [])
return $response;
}

/**
* Update the route attributes to change the canonical URL generated.
*
* @param array $context
* @param string $template
*/
private function addResolvedRoute(array $context, $template)
{
if (!isset($context['record'])) {
return;
}

$content = $context['record'];
$request = $this->app['request_stack']->getCurrentRequest();

$homepage = $this->getOption('theme/homepage') ?: $this->getOption('general/homepage');
$uriID = $content->contenttype['singular_slug'] . '/' . $content->get('id');
$uriSlug = $content->contenttype['singular_slug'] . '/' . $content->get('slug');

if (($uriID === $homepage || $uriSlug === $homepage) && ($template === $this->getOption('general/homepage_template'))) {
$request->attributes->add(['_route' => 'homepage', '_route_params' => []]);

return;
}

list($routeName, $routeParams) = $content->getRouteNameAndParams();
if ($routeName) {
$request->attributes->add(['_route' => $routeName, '_route_params' => $routeParams]);
}
}

/**
* Convert some data into a JSON response.
*
Expand Down
12 changes: 1 addition & 11 deletions src/Controller/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function record(Request $request, $contenttypeslug, $slug = '')
// First, try to get it by slug.
$content = $this->getContent($contenttype['slug'], ['slug' => $slug, 'returnsingle' => true, 'log_not_found' => !is_numeric($slug)]);

if (!$content && is_numeric($slug)) {
if (is_numeric($slug) && (!$content || count($content) === 0)) {
// And otherwise try getting it by ID
$content = $this->getContent($contenttype['slug'], ['id' => $slug, 'returnsingle' => true]);
}
Expand All @@ -165,16 +165,6 @@ public function record(Request $request, $contenttypeslug, $slug = '')
// Then, select which template to use, based on our 'cascading templates rules'
$template = $this->templateChooser()->record($content);

// Update the route attributes to change the canonical URL generated.
if ($content->isHome() && ($template === $this->getOption('general/homepage_template'))) {
$request->attributes->add(['_route' => 'homepage', '_route_params' => []]);
} else {
list($routeName, $routeParams) = $content->getRouteNameAndParams();
if ($routeName) {
$request->attributes->add(['_route' => $routeName, '_route_params' => $routeParams]);
}
}

// Setting the editlink
$this->app['editlink'] = $this->generateUrl('editcontent', ['contenttypeslug' => $contenttype['slug'], 'id' => $content->id]);
$this->app['edittitle'] = $content->getTitle();
Expand Down
36 changes: 3 additions & 33 deletions src/Legacy/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
class Content implements \ArrayAccess
{
use Entity\ContentTypeTrait;
use Entity\ContentRelationTrait;
use Entity\ContentRouteTrait;
use Entity\ContentSearchTrait;
Expand Down Expand Up @@ -143,8 +144,8 @@ public function getDecodedValue($name)
$value = null;

if (isset($this->values[$name])) {
$fieldtype = $this->fieldtype($name);
$fieldinfo = $this->fieldinfo($name);
$fieldtype = $this->fieldType($name);
$fieldinfo = $this->fieldInfo($name);
$allowtwig = !empty($fieldinfo['allowtwig']);

switch ($fieldtype) {
Expand Down Expand Up @@ -325,37 +326,6 @@ public function next($field = 'datepublish', $where = [])
return $next;
}

/**
* Get field information for the given field.
*
* @param string $key
*
* @return array An associative array containing at least the key 'type',
* and, depending on the type, other keys.
*/
public function fieldinfo($key)
{
if (isset($this->contenttype['fields'][$key])) {
return $this->contenttype['fields'][$key];
} else {
return ['type' => ''];
}
}

/**
* Get the fieldtype for a given fieldname.
*
* @param string $key
*
* @return string
*/
public function fieldtype($key)
{
$field = $this->fieldinfo($key);

return $field['type'];
}

/**
* ArrayAccess support.
*
Expand Down
1 change: 1 addition & 0 deletions src/Storage/Entity/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class Content extends Entity
{
use ContentRouteTrait;
use ContentTypeTrait;
use ContentTypeTitleTrait;

/** @var string|Mapping\ContentType */
Expand Down
45 changes: 45 additions & 0 deletions src/Storage/Entity/ContentTypeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Bolt\Storage\Entity;

/**
* Trait class for ContentType definitions.
*
* These traits should be considered transitional, the functionality in the
* process of refactor, and not representative of a valid approach.
*
* @author Ross Riley <riley.ross@gmail.com>
*/
trait ContentTypeTrait
{
/**
* Get field information for the given field.
*
* @param string $key
*
* @return array An associative array containing at least the key 'type',
* and, depending on the type, other keys.
*/
public function fieldInfo($key)
{
if (isset($this->contenttype['fields'][$key])) {
return $this->contenttype['fields'][$key];
} else {
return ['type' => ''];
}
}

/**
* Get the field type for a given field name.
*
* @param string $key
*
* @return string
*/
public function fieldType($key)
{
$field = $this->fieldInfo($key);

return $field['type'];
}
}
8 changes: 4 additions & 4 deletions src/Storage/Entity/ContentValuesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public function setValues(array $values)
];
// Check if the values need to be unserialized, and pre-processed.
foreach ($this->values as $key => $value) {
if ((in_array($this->fieldtype($key), $serializedFieldTypes)) || ($key === 'templatefields')) {
if ((in_array($this->fieldType($key), $serializedFieldTypes)) || ($key === 'templatefields')) {
if (!empty($value) && is_string($value) && (substr($value, 0, 2) === 'a:' || $value[0] === '[' || $value[0] === '{')) {
try {
$unserdata = Lib::smartUnserialize($value);
Expand All @@ -390,7 +390,7 @@ public function setValues(array $values)
}
}

if ($this->fieldtype($key) === 'video' && is_array($this->values[$key]) && !empty($this->values[$key]['url'])) {
if ($this->fieldType($key) === 'video' && is_array($this->values[$key]) && !empty($this->values[$key]['url'])) {
$defaultValues = [
'html' => '',
'responsive' => '',
Expand Down Expand Up @@ -427,7 +427,7 @@ public function setValues(array $values)
$this->values[$key] = $video;
}

if ($this->fieldtype($key) === 'repeater' && is_array($this->values[$key]) && !$this->isRootType) {
if ($this->fieldType($key) === 'repeater' && is_array($this->values[$key]) && !$this->isRootType) {
$originalMapping = null;
$originalMapping[$key]['fields'] = $this->contenttype['fields'][$key]['fields'];
$originalMapping[$key]['type'] = 'repeater';
Expand All @@ -443,7 +443,7 @@ public function setValues(array $values)
$this->values[$key] = $repeater;
}

if ($this->fieldtype($key) === 'date' || $this->fieldtype($key) === 'datetime') {
if ($this->fieldType($key) === 'date' || $this->fieldType($key) === 'datetime') {
if ($this->values[$key] === '') {
$this->values[$key] = null;
}
Expand Down
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
42 changes: 42 additions & 0 deletions src/Twig/Runtime/RecordRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use Bolt\Filesystem\Handler\DirectoryInterface;
use Bolt\Filesystem\Handler\FileInterface;
use Bolt\Helpers\Excerpt;
use Bolt\Storage\Entity;
use Bolt\Legacy;
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 @@ -311,4 +314,43 @@ public function selectField($content, $fieldName, $startempty = false, $keyName
}
return $retval;
}

/**
* @param array|Taxonomy|Entity\Content|Legacy\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 Legacy\Content) {
$candidate = $candidate->taxonomy;
}

// If it's a content entity then fetch the taxonomy field
if ($candidate instanceof 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 an empty array so we can guarantee the return type.
if (!$candidate instanceof Taxonomy) {
return [];
}

$compiled = [];
foreach ($candidate as $el) {
$type = $el->getTaxonomytype();
$slug = $el->getSlug();
$compiled[$type]["/$type/$slug"] = $slug;
}

return $compiled;
}
}

0 comments on commit b556ab3

Please sign in to comment.