Hello Ihor,
Thank you for your previous response. I’ve implemented the solution you suggested, but I’m still facing issues with the repeater field in the listing update form. I would appreciate any further guidance or insights.
Here’s a detailed overview of my implementation and the issue:
- Form Submission Filter: I’ve added the repeater field to the listing submission form using the
hivepress/v1/forms/listing_submit
filter. The field includes a custom_text
sub-field. Here’s the code snippet for this part:
add_filter('hivepress/v1/forms/listing_submit', function($form) {
$form['fields']['programme_par_jour'] = [
'label' => 'Programme par jour',
'description' => 'Ajoutez vos éléments ici',
'type' => 'repeater',
'_order' => 1000,
'_external' => true,
'fields' => [
'custom_text' => [
'label' => 'Texte Personnalisé',
'description' => 'Entrez votre texte ici',
'type' => 'textarea',
'required' => true,
'min_length' => 10,
],
],
];
return $form;
});
- Form Update Filter: Using the
hivepress/v1/forms/listing_update
filter, I fetch and deserialize the existing data for the repeater field from the post meta. The data retrieval and field display are successful, but there seems to be an issue with how the data is handled or saved. Here’s the relevant code:
add_filter('hivepress/v1/forms/listing_update', function($form, $listing_update_object) {
if ($listing_update_object) {
$listing_object = $listing_update_object->get_model();
if ($listing_object) {
$listing_id = $listing_object->get_id();
$custom_repeater_field_value = get_post_meta($listing_id, 'programme_par_jour', true);
// Désérialiser les valeurs si nécessaire
if (!is_array($custom_repeater_field_value)) {
$custom_repeater_field_value = maybe_unserialize($custom_repeater_field_value);
}
// Reconstruire les valeurs du champ répéteur
if (is_array($custom_repeater_field_value)) {
$reconstructed_values = array_map(function($item) {
return ['custom_text' => $item['custom_text']];
}, $custom_repeater_field_value);
} else {
$reconstructed_values = [];
}
$form['fields']['custom_repeater_field'] = [
'type' => 'repeater',
'label' => esc_html__('Programme par jour', 'text-domain'),
'description' => esc_html__('Ajoutez vos éléments ici', 'text-domain'),
'value' => $reconstructed_values,
'_order' => 1000,
'_external' => true,
'fields' => [
'custom_text' => [
'type' => 'textarea',
'label' => esc_html__('Texte Personnalisé', 'text-domain'),
'description' => esc_html__('Entrez votre texte ici', 'text-domain'),
'required' => true,
'min_length' => 10,
],
],
];
} else {
error_log('Objet listing non trouvé dans l\'objet de mise à jour.');
}
} else {
error_log('Objet de mise à jour de listing non trouvé.');
}
return $form;
}, 10, 2);
// Enregistrement des données lors de la création d'un listing
add_action('hivepress/v1/models/listing/create', function($listing_id) {
if (isset($_POST['programme_par_jour'])) {
// Log des données spécifiques du champ 'programme_par_jour'
error_log('Données programme_par_jour lors de la création: ' . print_r($_POST['programme_par_jour'], true));
update_post_meta($listing_id, 'programme_par_jour', $_POST['programme_par_jour']);
}
});
// Enregistrement des données lors de la mise à jour d'un listing
add_action('hivepress/v1/models/listing/update', function($listing_id) {
if (isset($_POST['programme_par_jour'])) {
// Log des données spécifiques du champ 'programme_par_jour'
error_log('Données programme_par_jour lors de la mise à jour: ' . print_r($_POST['programme_par_jour'], true));
update_post_meta($listing_id, 'programme_par_jour', $_POST['programme_par_jour']);
}
});
- Create and Update Actions: The
hivepress/v1/models/listing/create
and hivepress/v1/models/listing/update
actions are used for saving the repeater field data. The data seems to be stored correctly according to my logs, but I’m facing challenges ensuring that the updated data in the repeater field is correctly saved and persists across page reloads.
- Logs for Reference:
sqlCopy code
[Date & Time] Start of the listing update form configuration.
[Date & Time] Listing update object found.
[Date & Time] Listing object successfully retrieved.
[Date & Time] Listing ID: [listing_id]
[Date & Time] Deserialized repeater field value: Array ( [unique_id] => Array ( [custom_text] => test_content ) )
The repeater field data seems to be handled correctly on the front end, but the updates don’t appear to be consistently saved or displayed after form submission and page refresh.
Could anyone provide insights into what might be causing this issue or suggest further debugging steps? Any help or guidance would be immensely helpful.
Additionally, I suspect the issue might be related to the dynamic IDs assigned to each repeater field. While the form correctly displays the number of repeater fields, it fails to insert the data into these fields. This discrepancy seems to arise because the IDs recorded in the logs are different from those in the update form fields. This mismatch of IDs might be causing the data not to bind correctly to the respective fields during the update process.
Here’s an example from the logs showing the dynamic IDs and their associated data:
csharpCopy code
[07-Jan-2024 13:50:20 UTC] Data for programme_par_jour during update: Array
(
[659aabe83bf8c] => Array
(
[custom_text] => programme par jour 1
)
[tip7rh2lmcp] => Array
(
[custom_text] => programme par jour 2
)
)
In contrast, the IDs in the update form’s repeater fields don’t match these, potentially leading to the data not being displayed. I am looking for a way to either synchronize these IDs or handle the repeater field values irrespective of their dynamic IDs.
Any suggestions on how to address this specific aspect, or general guidance on handling dynamic IDs in repeater fields within HivePress, would be greatly appreciated.
Thank you once again for your assistance!