Merge pull request #80 from bolt/jsonrecords

Adding `jsonrecords()` twig function.
This commit is contained in:
Bob den Otter
2018-11-06 06:55:19 +01:00
committed by GitHub
5 changed files with 60 additions and 5 deletions
+27
View File
@@ -168,6 +168,33 @@ class Content
return $this->contentTypeDefinition;
}
/**
* @return array
*/
public function getSummary(): array
{
$summary = [
'title' => $this->magicTitle(),
'excerpt' => $this->magicexcerpt(),
'image' => $this->magicImage(),
'link' => $this->magicLink(),
'editlink' => $this->magicEditLink(),
'author' => [
'id' => $this->getAuthor()->getid(),
'fullName' => $this->getAuthor()->getfullName(),
'username' => $this->getAuthor()->getusername(),
'email' => $this->getAuthor()->getemail(),
'roles' => $this->getAuthor()->getroles(),
],
'createdAt' => $this->getCreatedAt(),
'modifiedAt' => $this->modifiedAt(),
'publishedAt' => $this->getPublishedAt(),
'depublishedAt' => $this->depublishedAt(),
];
return $summary;
}
/**
* @return string
*/
+7 -1
View File
@@ -129,7 +129,13 @@ trait ContentMagicTraits
public function magicImage()
{
return 'magic image';
foreach ($this->getFields() as $field) {
if ($field->getDefinition()->get('type') === 'image') {
return $field->getValue();
}
}
return null;
}
public function magicExcerpt($length = 200, $includeTitle = false, $focus = null)
+24 -2
View File
@@ -50,6 +50,7 @@ class ContentHelperExtension extends AbstractExtension
return [
new TwigFunction('sidebarmenu', [$this, 'sidebarmenu']),
new TwigFunction('jsonlabels', [$this, 'jsonlabels']),
new TwigFunction('jsonrecords', [$this, 'jsonrecords']),
new TwigFunction('fieldfactory', [$this, 'fieldfactory']),
new TwigFunction('selectoptionsfromarray', [$this, 'selectoptionsfromarray']),
new TwigFunction('icon', [$this, 'icon'], $safe),
@@ -87,18 +88,39 @@ class ContentHelperExtension extends AbstractExtension
/**
* @param array $labels
* @param bool $pretty
*
* @return string
*/
public function jsonlabels(array $labels): string
public function jsonlabels(array $labels, $pretty = false): string
{
$result = [];
$options = $pretty ? JSON_PRETTY_PRINT : 0;
foreach ($labels as $label) {
$key = is_array($label) ? $label[0] : $label;
$result[$key] = $this->translator->trans(...(array) $label);
}
return json_encode($result);
return json_encode($result, $options);
}
/**
* @param $records
* @param bool $pretty
*
* @return string
*/
public function jsonrecords($records, $pretty = false): string
{
$result = [];
$options = $pretty ? JSON_PRETTY_PRINT : 0;
foreach ($records as $record) {
$result[] = $record->getSummary();
}
return json_encode($result, $options);
}
public function selectoptionsfromarray(Field $field)
@@ -1,3 +1,5 @@
{# @Jack: This twig function gives JSON for the current `records` #}
{{ jsonrecords(records) }}
{% if records %}
<nav class="listing__filter">
-2
View File
@@ -1,7 +1,5 @@
{% extends '@bolt/_base/layout.twig' %}
{% block title %}
{% if contenttype %}
<i class='fas mr-2 {{contenttype.icon_one}}'></i> <strong>{{ contenttype.name }}</strong>