2021-11-03 11:47:10 +01:00
|
|
|
<?php
|
2021-11-04 10:47:03 +01:00
|
|
|
|
|
|
|
use Cake\Utility\Inflector;
|
|
|
|
|
2021-11-03 11:47:10 +01:00
|
|
|
$default_template = [
|
2021-11-04 15:19:45 +01:00
|
|
|
'inputContainer' => '<div class="row mb-1 multi-metafield-container">{{content}}</div>',
|
2021-11-08 14:08:47 +01:00
|
|
|
'inputContainerError' => '<div class="row mb-1 multi-metafield-container has-error">{{content}}</div>',
|
2021-11-04 10:47:03 +01:00
|
|
|
'formGroup' => '<label class="col-sm-2 col-form-label form-label" {{attrs}}>{{label}}</label><div class="col-sm-10 multi-metafield-input-container">{{input}}{{error}}</div>',
|
2021-11-03 11:47:10 +01:00
|
|
|
];
|
|
|
|
$form->setTemplates($default_template);
|
|
|
|
|
|
|
|
$fieldsHtml = '';
|
|
|
|
$labelPrintedOnce = false;
|
2021-11-04 08:10:32 +01:00
|
|
|
if (!empty($metaFieldsEntities)) {
|
|
|
|
foreach ($metaFieldsEntities as $i => $metaFieldsEntity) {
|
2021-11-04 10:47:03 +01:00
|
|
|
$metaFieldsEntity->label = Inflector::humanize($metaFieldsEntity->field);
|
2021-11-04 08:10:32 +01:00
|
|
|
$fieldData = [
|
2021-11-04 10:47:03 +01:00
|
|
|
'label' => $metaFieldsEntity->label,
|
2021-11-08 14:08:47 +01:00
|
|
|
'field' => sprintf(
|
|
|
|
'MetaTemplates.%s.meta_template_fields.%s.metaFields.%s.value',
|
|
|
|
$metaFieldsEntity->meta_template_id,
|
|
|
|
$metaFieldsEntity->meta_template_field_id,
|
|
|
|
$metaFieldsEntity->id
|
|
|
|
),
|
2022-11-14 09:04:35 +01:00
|
|
|
'type' => $metaTemplateField->formType,
|
2021-11-04 08:10:32 +01:00
|
|
|
];
|
2021-11-08 14:08:47 +01:00
|
|
|
if($metaFieldsEntity->isNew()) {
|
|
|
|
$fieldData['field'] = sprintf(
|
|
|
|
'MetaTemplates.%s.meta_template_fields.%s.metaFields.%s.value',
|
|
|
|
$metaFieldsEntity->meta_template_id,
|
|
|
|
$metaFieldsEntity->meta_template_field_id,
|
|
|
|
$i
|
|
|
|
);
|
|
|
|
$fieldData['class'] = 'new-metafield';
|
|
|
|
}
|
2021-11-04 08:10:32 +01:00
|
|
|
if ($labelPrintedOnce) { // Only the first input can have a label
|
2022-11-14 15:34:30 +01:00
|
|
|
$fieldData['label'] = ['text' => ''];
|
2021-11-04 08:10:32 +01:00
|
|
|
}
|
2022-11-14 09:04:35 +01:00
|
|
|
if ($metaTemplateField->formType === 'dropdown') {
|
|
|
|
$fieldData = array_merge_recursive($fieldData, $metaTemplateField->formOptions);
|
|
|
|
}
|
2021-11-04 08:10:32 +01:00
|
|
|
$labelPrintedOnce = true;
|
|
|
|
$fieldsHtml .= $this->element(
|
|
|
|
'genericElements/Form/fieldScaffold',
|
|
|
|
[
|
|
|
|
'fieldData' => $fieldData,
|
|
|
|
'form' => $form
|
|
|
|
]
|
|
|
|
);
|
2021-11-03 11:47:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($metaTemplateField) && !empty($multiple)) { // Add multiple field button
|
2021-11-04 10:47:03 +01:00
|
|
|
$metaTemplateField->label = Inflector::humanize($metaTemplateField->field);
|
2021-11-04 08:10:32 +01:00
|
|
|
$emptyMetaFieldInput = '';
|
2021-11-08 15:03:05 +01:00
|
|
|
if (empty($metaFieldsEntities)) { // Include editable field for meta-template not containing a meta-field
|
2022-11-14 09:04:35 +01:00
|
|
|
$fieldData = [
|
|
|
|
'label' => $metaTemplateField->label,
|
|
|
|
'field' => sprintf('MetaTemplates.%s.meta_template_fields.%s.metaFields.new.0', $metaTemplateField->meta_template_id, $metaTemplateField->id),
|
|
|
|
'class' => 'new-metafield',
|
|
|
|
'type' => $metaTemplateField->formType,
|
|
|
|
];
|
|
|
|
if ($metaTemplateField->formType === 'dropdown') {
|
|
|
|
$fieldData = array_merge_recursive($fieldData, $metaTemplateField->formOptions);
|
|
|
|
}
|
2021-11-08 15:03:05 +01:00
|
|
|
$emptyMetaFieldInput = $this->element(
|
|
|
|
'genericElements/Form/fieldScaffold',
|
|
|
|
[
|
2022-11-14 09:04:35 +01:00
|
|
|
'fieldData' => $fieldData,
|
2021-11-08 15:03:05 +01:00
|
|
|
'form' => $form,
|
|
|
|
]
|
|
|
|
);
|
2021-11-04 08:10:32 +01:00
|
|
|
}
|
2021-11-03 11:47:10 +01:00
|
|
|
$emptyInputForSecurityComponent = $this->element(
|
|
|
|
'genericElements/Form/fieldScaffold',
|
|
|
|
[
|
|
|
|
'fieldData' => [
|
|
|
|
'label' => false,
|
2021-11-04 08:10:32 +01:00
|
|
|
'field' => sprintf('MetaTemplates.%s.meta_template_fields.%s.metaFields.new[]', $metaTemplateField->meta_template_id, $metaTemplateField->id),
|
2021-11-08 14:08:47 +01:00
|
|
|
'value' => '',
|
2021-11-03 11:47:10 +01:00
|
|
|
],
|
|
|
|
'form' => $form,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$multiFieldButtonHtml = sprintf(
|
2021-11-04 15:19:45 +01:00
|
|
|
'<div class="row mb-1 multi-metafield-container add-input-container"><div class="col-sm-2 form-label"></div><div class="col-sm-10 multi-metafield-input-container">%s</div></div>',
|
2021-11-03 11:47:10 +01:00
|
|
|
$this->element(
|
|
|
|
'genericElements/Form/multiFieldButton',
|
|
|
|
[
|
|
|
|
'metaTemplateFieldName' => $metaTemplateField->field,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
2021-11-08 15:03:05 +01:00
|
|
|
$fieldsHtml .= $emptyMetaFieldInput;
|
2021-11-03 11:47:10 +01:00
|
|
|
$fieldsHtml .= sprintf('<div class="d-none template-container">%s</div>', $emptyInputForSecurityComponent);
|
|
|
|
$fieldsHtml .= $multiFieldButtonHtml;
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
|
|
|
|
<div class="row mb-3 multi-metafields-container">
|
|
|
|
<?= $fieldsHtml; ?>
|
|
|
|
</div>
|