SDK Form demonstrator improve delegation and hide fields when not needed

This commit is contained in:
Eric Espie
2025-04-16 16:38:13 +02:00
parent 4603fcaa4f
commit 5466b506ee
2 changed files with 55 additions and 24 deletions

View File

@@ -12,7 +12,6 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AttCodeGroupByType extends AbstractType
@@ -39,24 +38,19 @@ class AttCodeGroupByType extends AbstractType
->setAllowedTypes('hook_type', 'string');
}
public static function BuildSubField(FormInterface $oForm, string $sName, array $aData, array $aFormOptions = []): void
{
\IssueLog::Info('AttCodeGroupByType BuildSubField data: '.var_export($aData, true));
$aFormOptions['choices'] = self::GetGroupByOptions($aData['query']);
$aFormOptions['multiple'] = false;
$oForm->add($sName, AttCodeGroupByType::class, $aFormOptions);
}
protected static function GetGroupByOptions($sOql)
protected function GetGroupByOptions($sClassOrOql)
{
$oModelReflection = new \ModelReflectionRuntime();
$aGroupBy = array();
try
{
$oQuery = $oModelReflection->GetQuery($sOql);
$sClass = $oQuery->GetClass();
if ($oModelReflection->IsValidClass($sClassOrOql)) {
$sClass = $sClassOrOql;
} else {
$oQuery = $oModelReflection->GetQuery($sClassOrOql);
$sClass = $oQuery->GetClass();
}
foreach($oModelReflection->ListAttributes($sClass) as $sAttCode => $sAttType)
{
// For external fields, find the real type of the target
@@ -110,4 +104,29 @@ class AttCodeGroupByType extends AbstractType
}
return array_flip($aGroupBy);
}
public function BuildOptions(array $aUserOptions, array $aModelData): ?array
{
$oModelReflection = new \ModelReflectionRuntime();
$sClassOrOql = $aModelData[$aUserOptions['source_class']];
if ($oModelReflection->IsValidClass($sClassOrOql)) {
$sClass = $sClassOrOql;
} else {
try {
$oQuery = $oModelReflection->GetQuery($sClassOrOql);
} catch (Exception $e) {
return null;
}
$sClass = $oQuery->GetClass();
}
$aFormOptions['choices'] = $this->GetGroupByOptions($sClass);
$aFormOptions['multiple'] = false;
return $aFormOptions;
}
public function GetPrerequisites(array $aUserOptions): ?array
{
return [$aUserOptions['source_class']];
}
}

View File

@@ -11,12 +11,13 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use utils;
class ValuesFromAttcodeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
@@ -44,18 +45,22 @@ class ValuesFromAttcodeType extends AbstractType
$resolver->setDefault('attr', ['class' => 'ibo-input ibo-input-select']);
}
public static function BuildSubField(FormInterface $oForm, string $sName, array $aData, array $aFormOptions = []): void
public function BuildOptions(array $aUserOptions, array $aModelData): ?array
{
\IssueLog::Info("ValuesFromAttcodeType BuildSubField data: ".var_export($aData, true));
if (utils::IsNullOrEmptyString($aData['group_by'] ?? null)) {
return;
$sAttCode = $aModelData[$aUserOptions['source_attcode']] ?? null;
if (utils::IsNullOrEmptyString($sAttCode)) {
return null;
} else {
$oModelReflection = new \ModelReflectionRuntime();
$oQuery = $oModelReflection->GetQuery($aData['query']);
$sClass = $oQuery->GetClass();
$sAttCode = $aData['group_by'];
$sClass = $aModelData[$aUserOptions['source_class']];
if (! $oModelReflection->IsValidClass($sClass)) {
try {
$oQuery = $oModelReflection->GetQuery($sClass);
} catch (\Exception $e) {
return null;
}
$sClass = $oQuery->GetClass();
}
if (\MetaModel::IsValidAttCode($sClass, $sAttCode)) {
$aAllowed = $oModelReflection->GetAllowedValues_att($sClass, $sAttCode);
if (is_array($aAllowed))
@@ -72,7 +77,14 @@ class ValuesFromAttcodeType extends AbstractType
$aFormOptions['multiple'] = true;
$aFormOptions['required'] = false;
$oForm->add($sName, ValuesFromAttcodeType::class, $aFormOptions);
return $aFormOptions;
}
public function GetPrerequisites(array $aUserOptions): ?array
{
return [
$aUserOptions['source_class'],
$aUserOptions['source_attcode'],
];
}
}