mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-22 00:32:16 +02:00
Add UIBlocks to twig (WIP)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
|
||||
namespace Combodo\iTop\Application\TwigBase\UI\Component;
|
||||
|
||||
|
||||
use Combodo\iTop\Application\TwigBase\UI\UIBlockHelper;
|
||||
use Twig\Compiler;
|
||||
use Twig\Error\SyntaxError;
|
||||
use Twig\Node\Node;
|
||||
|
||||
class UIFieldNode extends Node
|
||||
{
|
||||
public function __construct($sType, $oParams, $oBody, $lineno = 0, $tag = null)
|
||||
{
|
||||
parent::__construct(['body' => $oBody], ['type' => $sType, 'params' => $oParams], $lineno, $tag);
|
||||
}
|
||||
|
||||
public function compile(Compiler $compiler)
|
||||
{
|
||||
$sBlockVar = UIBlockHelper::GetBlockVarName('oFieldSet');
|
||||
$oParams = $this->getAttribute('params');
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write("\$aParams = ")
|
||||
->subcompile($oParams)
|
||||
->raw(";\n");
|
||||
|
||||
$sType = $this->getAttribute('type');
|
||||
switch ($sType) {
|
||||
case 'Small':
|
||||
case 'Large':
|
||||
$compiler
|
||||
->write("\$sLabel = \$aParams['label'] ?? '';\n")
|
||||
->write("\$sValueId = \$aParams['value_id'] ?? null;\n")
|
||||
->write("ob_start();\n")
|
||||
->subcompile($this->getNode('body'))
|
||||
->write("\$sValue = ob_get_contents();\n")
|
||||
->write("ob_end_clean();\n")
|
||||
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Field\\FieldFactory::Make{$sType}(\$sLabel, \$sValue);\n")
|
||||
->write("\${$sBlockVar}->SetValueId(\$sValueId);\n");
|
||||
break;
|
||||
// TODO 3.0 add other FieldFactory methods
|
||||
|
||||
default:
|
||||
throw new SyntaxError(sprintf('%s: Bad type "%s" for %s at line %d', $this->getTemplateName(), $sType, $this->tag, $this->lineno), $this->lineno, $this->getSourceContext());
|
||||
|
||||
}
|
||||
$compiler->write(UIBlockHelper::AddToParentBlock($sBlockVar));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
|
||||
namespace Combodo\iTop\Application\TwigBase\UI\Component;
|
||||
|
||||
|
||||
use Twig\Token;
|
||||
use Twig\TokenParser\AbstractTokenParser;
|
||||
|
||||
class UIFieldParser extends AbstractTokenParser
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function parse(Token $token)
|
||||
{
|
||||
$iLineno = $token->getLine();
|
||||
$oStream = $this->parser->getStream();
|
||||
|
||||
$sType = $oStream->expect(Token::NAME_TYPE)->getValue();
|
||||
|
||||
$oParams = $this->parser->getExpressionParser()->parseExpression();
|
||||
|
||||
$oStream->expect(Token::BLOCK_END_TYPE);
|
||||
|
||||
$oBody = $this->parser->subparse([$this, 'decideForEnd'], true);
|
||||
$oStream->expect(Token::BLOCK_END_TYPE);
|
||||
|
||||
|
||||
return new UIFieldNode($sType, $oParams, $oBody, $iLineno, $this->getTag());
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getTag()
|
||||
{
|
||||
return 'UIField';
|
||||
}
|
||||
|
||||
public function decideForEnd(Token $token)
|
||||
{
|
||||
return $token->test('EndUIField');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user