Refined form fields HTML bindings
This commit is contained in:
@@ -142,50 +142,16 @@ class AdminUtilities extends AbstractSmartyPlugin
|
||||
));
|
||||
}
|
||||
|
||||
public function buildFormField($params, &$smarty)
|
||||
{
|
||||
$form = $this->getParam($params, 'form', false);
|
||||
$field_name = $this->getParam($params, 'name', false);
|
||||
$field_extra_class = $this->getParam($params, 'extra_class', '');
|
||||
$field_value = $this->getParam($params, 'value', '');
|
||||
|
||||
return $this->fetchSnippet($smarty, 'forms'.DS.'form-field', array(
|
||||
'form' => $form,
|
||||
'field_name' => $field_name,
|
||||
'field_extra_class' => $field_extra_class,
|
||||
'field_value' => $field_value
|
||||
));
|
||||
}
|
||||
|
||||
public function buildFormFieldLabel($params, &$smarty)
|
||||
{
|
||||
$form = $this->getParam($params, 'form', false);
|
||||
$field_name = $this->getParam($params, 'name', false);
|
||||
$label_attr = $this->getParam($params, 'label_attr', false);
|
||||
|
||||
$args = [
|
||||
'form' => $form,
|
||||
'field_name' => $field_name,
|
||||
];
|
||||
|
||||
if ($label_attr !== false)
|
||||
$args['label_attr'] = $label_attr;
|
||||
|
||||
return $this->fetchSnippet($smarty, 'forms'.DS.'form-label', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the various smarty plugins handled by this class
|
||||
*
|
||||
* @return an array of smarty plugin descriptors
|
||||
* @return array of smarty plugin descriptors
|
||||
*/
|
||||
public function getPluginDescriptors()
|
||||
{
|
||||
return array(
|
||||
new SmartyPluginDescriptor('function', 'admin_sortable_header' , $this, 'generateSortableColumnHeader'),
|
||||
new SmartyPluginDescriptor('function', 'admin_position_block' , $this, 'generatePositionChangeBlock'),
|
||||
new SmartyPluginDescriptor('function', 'admin_form_field' , $this, 'buildFormField'),
|
||||
new SmartyPluginDescriptor('function', 'admin_form_field_label' , $this, 'buildFormFieldLabel'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,17 +12,20 @@
|
||||
|
||||
namespace Thelia\Core\Template\Smarty\Plugins;
|
||||
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Thelia\Core\Form\Type\TheliaType;
|
||||
|
||||
use Thelia\Core\Template\Element\Exception\ElementNotFoundException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
|
||||
use Thelia\Core\Template\Smarty\AbstractSmartyPlugin;
|
||||
use Thelia\Core\Template\ParserContext;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
|
||||
use Symfony\Component\Form\FormConfigInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Thelia\Core\Form\Type\TheliaType;
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
use Thelia\Core\Template\Element\Exception\ElementNotFoundException;
|
||||
use Thelia\Core\Template\ParserContext;
|
||||
use Thelia\Core\Template\ParserInterface;
|
||||
use Thelia\Core\Template\Smarty\AbstractSmartyPlugin;
|
||||
use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
|
||||
use Thelia\Core\Template\TemplateHelper;
|
||||
use Thelia\Form\BaseForm;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -53,15 +56,22 @@ class Form extends AbstractSmartyPlugin
|
||||
private static $taggedFieldsStack = null;
|
||||
private static $taggedFieldsStackPosition = null;
|
||||
|
||||
/** @var Request $request */
|
||||
protected $request;
|
||||
|
||||
/** @var ParserContext $parserContext */
|
||||
protected $parserContext;
|
||||
|
||||
/** @var ParserInterface $parser */
|
||||
protected $parser;
|
||||
|
||||
protected $formDefinition = array();
|
||||
|
||||
public function __construct(Request $request, ParserContext $parserContext)
|
||||
public function __construct(Request $request, ParserContext $parserContext, ParserInterface $parser)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->parserContext = $parserContext;
|
||||
$this->parser = $parser;
|
||||
}
|
||||
|
||||
public function setFormDefinition($formDefinition)
|
||||
@@ -78,7 +88,6 @@ class Form extends AbstractSmartyPlugin
|
||||
|
||||
public function generateForm($params, $content, \Smarty_Internal_Template $template, &$repeat)
|
||||
{
|
||||
|
||||
if ($repeat) {
|
||||
|
||||
$name = $this->getParam($params, 'name');
|
||||
@@ -117,6 +126,14 @@ class Form extends AbstractSmartyPlugin
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Smarty_Internal_Template $template
|
||||
* @param string $fieldName
|
||||
* @param string $fieldValue
|
||||
* @param string $fieldType
|
||||
* @param array $fieldVars
|
||||
* @param int $total_value_count
|
||||
*/
|
||||
protected function assignFieldValues(
|
||||
$template,
|
||||
$fieldName,
|
||||
@@ -163,6 +180,11 @@ class Form extends AbstractSmartyPlugin
|
||||
$template->assign("attr_list", $fieldVars["attr"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Smarty_Internal_Template $template
|
||||
* @param FormConfigInterface $formFieldConfig
|
||||
* @param FormView $formFieldView
|
||||
*/
|
||||
protected function assignFormTypeValues($template, $formFieldConfig, $formFieldView)
|
||||
{
|
||||
$formFieldType = $formFieldConfig->getType()->getInnerType();
|
||||
@@ -204,58 +226,133 @@ class Form extends AbstractSmartyPlugin
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @param \Smarty_Internal_Template $template
|
||||
*/
|
||||
protected function processFormField($params, $template)
|
||||
{
|
||||
$formFieldView = $this->getFormFieldView($params);
|
||||
$formFieldConfig = $this->getFormFieldConfig($params);
|
||||
|
||||
$formFieldType = $formFieldConfig->getType()->getName();
|
||||
|
||||
$this->assignFormTypeValues($template, $formFieldConfig, $formFieldView);
|
||||
|
||||
$value = $formFieldView->vars["value"];
|
||||
|
||||
$key = $this->getParam($params, 'value_key', null);
|
||||
|
||||
// We (may) have a collection
|
||||
if ($key !== null) {
|
||||
|
||||
// Force array
|
||||
if (! is_array($value)) $value = array();
|
||||
|
||||
// If the field is not found, use an empty value
|
||||
$name = sprintf("%s[%s]", $formFieldView->vars["full_name"], $key);
|
||||
|
||||
$val = $value[$key];
|
||||
|
||||
$this->assignFieldValues(
|
||||
$template,
|
||||
$name,
|
||||
$val,
|
||||
$formFieldType,
|
||||
$formFieldView->vars,
|
||||
count($formFieldView->children)
|
||||
);
|
||||
} else {
|
||||
$this->assignFieldValues(
|
||||
$template,
|
||||
$formFieldView->vars["full_name"],
|
||||
$formFieldView->vars["value"],
|
||||
$formFieldType,
|
||||
$formFieldView->vars
|
||||
);
|
||||
}
|
||||
|
||||
$formFieldView->setRendered();
|
||||
}
|
||||
|
||||
public function renderFormField($params, $content, \Smarty_Internal_Template $template, &$repeat)
|
||||
{
|
||||
if ($repeat) {
|
||||
|
||||
$formFieldView = $this->getFormFieldView($params);
|
||||
$formFieldConfig = $this->getFormFieldConfig($params);
|
||||
$this->processFormField($params, $template);
|
||||
|
||||
$formFieldType = $formFieldConfig->getType()->getName();
|
||||
|
||||
$this->assignFormTypeValues($template, $formFieldConfig, $formFieldView);
|
||||
|
||||
$value = $formFieldView->vars["value"];
|
||||
|
||||
$key = $this->getParam($params, 'value_key', null);
|
||||
|
||||
// We (may) have a collection
|
||||
if ($key !== null) {
|
||||
|
||||
// Force array
|
||||
if (! is_array($value)) $value = array();
|
||||
|
||||
// If the field is not found, use an empty value
|
||||
$val = array_key_exists($key, $value) ? $value[$key] : '';
|
||||
|
||||
$name = sprintf("%s[%s]", $formFieldView->vars["full_name"], $key);
|
||||
|
||||
$val = $value[$key];
|
||||
|
||||
$this->assignFieldValues(
|
||||
$template,
|
||||
$name,
|
||||
$val,
|
||||
$formFieldType,
|
||||
$formFieldView->vars,
|
||||
count($formFieldView->children)
|
||||
);
|
||||
} else {
|
||||
$this->assignFieldValues(
|
||||
$template,
|
||||
$formFieldView->vars["full_name"],
|
||||
$formFieldView->vars["value"],
|
||||
$formFieldType,
|
||||
$formFieldView->vars
|
||||
);
|
||||
}
|
||||
|
||||
$formFieldView->setRendered();
|
||||
} else {
|
||||
} else {
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @param string $content
|
||||
* @param \Smarty_Internal_Template $template
|
||||
* @param string $templateTypeName
|
||||
* @return string
|
||||
*/
|
||||
protected function automaticFormFieldRendering($params, $content, $template, $templateTypeName)
|
||||
{
|
||||
$data = '';
|
||||
|
||||
$templateStyle = $this->getParam($params, 'template', 'standard');
|
||||
$templateFile = sprintf($templateTypeName, $templateStyle);
|
||||
|
||||
$snippet_path = sprintf('%s/%s.html',
|
||||
$this->parser->getTemplateDefinition()->getAbsolutePath(),
|
||||
$templateFile
|
||||
);
|
||||
|
||||
if (false !== $snippet_content = file_get_contents($snippet_path)) {
|
||||
|
||||
$this->processFormField($params, $template);
|
||||
|
||||
$form = $this->getParam($params, 'form', false);
|
||||
$field_name = $this->getParam($params, 'field', false);
|
||||
$field_extra_class = $this->getParam($params, 'extra_class', '');
|
||||
$field_value = $this->getParam($params, 'value', '');
|
||||
|
||||
$template->assign([
|
||||
'content' => trim($content),
|
||||
'form' => $form,
|
||||
'field_name' => $field_name,
|
||||
'field_extra_class' => $field_extra_class,
|
||||
'field_value' => $field_value,
|
||||
'field_template' => $templateStyle
|
||||
]);
|
||||
|
||||
$data = $template->fetch(sprintf('string:%s', $snippet_content));
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $params
|
||||
* @param $content
|
||||
* @param \Smarty_Internal_Template $template
|
||||
* @param $repeat
|
||||
* @return mixed
|
||||
*/
|
||||
public function customFormFieldRendering($params, $content, $template, &$repeat)
|
||||
{
|
||||
if (! $repeat) {
|
||||
return $this->automaticFormFieldRendering($params, $content, $template, 'forms'.DS.'form-field-%s-renderer');
|
||||
}
|
||||
}
|
||||
|
||||
public function standardFormFieldRendering($params, \Smarty_Internal_Template $template)
|
||||
{
|
||||
return $this->automaticFormFieldRendering($params, '', $template, 'forms'.DS.'form-field-%s-renderer');
|
||||
}
|
||||
|
||||
public function standardFormFieldAttributes($params, \Smarty_Internal_Template $template)
|
||||
{
|
||||
return $this->automaticFormFieldRendering($params, '', $template, 'forms'.DS.'form-field-attributes-%s-renderer');
|
||||
}
|
||||
|
||||
public function renderTaggedFormFields($params, $content, \Smarty_Internal_Template $template, &$repeat)
|
||||
{
|
||||
if (null === $content) {
|
||||
@@ -360,6 +457,11 @@ class Form extends AbstractSmartyPlugin
|
||||
return array_search("hidden", $formView->vars["block_prefixes"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $params
|
||||
* @return FormView
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function getFormFieldView($params)
|
||||
{
|
||||
$instance = $this->getInstanceFromParams($params);
|
||||
@@ -397,6 +499,11 @@ class Form extends AbstractSmartyPlugin
|
||||
return $viewList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $params
|
||||
* @return FormConfigInterface
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function getFormFieldConfig($params)
|
||||
{
|
||||
$instance = $this->getInstanceFromParams($params);
|
||||
@@ -416,6 +523,11 @@ class Form extends AbstractSmartyPlugin
|
||||
return $fieldData->getConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $params
|
||||
* @return BaseForm
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function getInstanceFromParams($params)
|
||||
{
|
||||
$instance = $this->getParam($params, 'form');
|
||||
@@ -445,7 +557,11 @@ class Form extends AbstractSmartyPlugin
|
||||
new SmartyPluginDescriptor("block", "form_tagged_fields", $this, "renderTaggedFormFields"),
|
||||
new SmartyPluginDescriptor("function", "form_hidden_fields", $this, "renderHiddenFormField"),
|
||||
new SmartyPluginDescriptor("function", "form_enctype", $this, "formEnctype"),
|
||||
new SmartyPluginDescriptor("block", "form_error", $this, "formError")
|
||||
new SmartyPluginDescriptor("block", "form_error", $this, "formError"),
|
||||
|
||||
new SmartyPluginDescriptor("function", "form_field_attributes" , $this, "standardFormFieldAttributes"),
|
||||
new SmartyPluginDescriptor("function", "render_form_field" , $this, "standardFormFieldRendering"),
|
||||
new SmartyPluginDescriptor("block" , "custom_render_form_field", $this, "customFormFieldRendering"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user