Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a smarty plugin to get render from json #79

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">

<services>
<service id="theliablocks.json.block" alias="TheliaBlocks\Service\JsonBlockService" public="true">
</service>
</services>

<hooks>
<hook id="theliablocks.hook.menu" class="TheliaBlocks\Hook\TheliaBlocksMenuHook" scope="request">
<tag name="hook.event_listener" event="main.top-menu-tools" type="back" method="onMainTopMenuTools"/>
Expand Down
22 changes: 8 additions & 14 deletions Loop/BlockGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
use TheliaBlocks\Model\BlockGroupQuery;

/**
* Class BlockGroup
* @package TheliaBlocks\Loop
* Class BlockGroup.
*
* @method int[] getId()
* @method string[] getSlug()
* @method string getItemType()
* @method integer getItemId()
* @method bool|string getVisible()
* @method int[] getId()
* @method string[] getSlug()
* @method string getItemType()
* @method int getItemId()
* @method bool|string getVisible()
*/
class BlockGroup extends BaseI18nLoop implements PropelSearchLoopInterface
{
Expand Down Expand Up @@ -82,14 +81,9 @@ public function buildModelCriteria()

public function parseResults(LoopResult $loopResult)
{
/** @var \TheliaBlocks\Model\BlockGroup $entry */
/** @var \TheliaBlocks\Model\BlockGroup $entry */
foreach ($loopResult->getResultDataCollection() as $entry) {
$blockRenders = [];

foreach (json_decode($entry->getVirtualColumn('i18n_JSON_CONTENT'), true) as $block) {
$blockRenders[] = $this->container->get("thelia.parser")->render("blocks".DS.$block['type']['id'].".html", $block);
}
$htmlRender = implode(' ', $blockRenders);
$htmlRender = $this->container->get('theliablocks.json.block')->renderJsonBlocks($entry->getVirtualColumn('i18n_JSON_CONTENT'));

$content = json_decode($entry->getVirtualColumn('i18n_JSON_CONTENT'), true);

Expand Down
35 changes: 35 additions & 0 deletions Service/JsonBlockService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace TheliaBlocks\Service;

use TheliaSmarty\Template\SmartyParser;

class JsonBlockService
{
/** @var SmartyParser */
private $parser;

public function __construct(SmartyParser $parser)
{
$this->parser = $parser;
}

public function renderJsonBlocks($json): string
{
$blockRenders = array_map(function ($block) {
return $this->parser->render('blocks'.DS.$block['type']['id'].'.html', $block);
}, json_decode($json, true));

return implode(' ', $blockRenders);
}
}
44 changes: 44 additions & 0 deletions SmartyPlugin/JsonBlockRender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace TheliaBlocks\SmartyPlugin;

use TheliaBlocks\Service\JsonBlockService;
use TheliaSmarty\Template\AbstractSmartyPlugin;
use TheliaSmarty\Template\SmartyPluginDescriptor;

class JsonBlockRender extends AbstractSmartyPlugin
{
/** @var JsonBlockService */
private $jsonBlockService;

public function __construct(JsonBlockService $jsonBlockService)
{
$this->jsonBlockService = $jsonBlockService;
}

public function getPluginDescriptors()
{
return [
new SmartyPluginDescriptor('function', 'json_block_render', $this, 'renderJsonBlock'),
];
}

public function renderJsonBlock($params, $smarty): string
{
if (!isset($params['json'])) {
return '';
}

return $this->jsonBlockService->renderJsonBlocks($params['json']);
}
}