How To Add Custom Toggle\Change Toggle Props In Parent Module Of Exisiting Divi 5 Module #71
Open
Description
If we want add custom toggle or change toggle props in the contact form module, we can achieve this in Divi 4 using add_filter('et_builder_get_parent_modules', 'my_callback_function').
How can we achieve this in Divi 5.0? Is there any alternate hook for this?
Hook
apply_filters( 'et_builder_get_parent_modules', $modules, $post_type )
Code Example
/**
* Adds a custom toggle to the settings modal of the parent module for the Divi contact form.
* This function modifies the settings modal toggles for the contact form module by adding a new custom toggle.
*
* @param array $modules An array of existing modules.
* @param string $post_type The post-type of the current post.
*
* @return array The modified array of modules.
*/
if (!function_exists('add_custom_toggle_parent_module')) :
function add_custom_toggle_parent_module($modules, $post_type)
{
static $is_applied = false;
// Prevent re-applying the modifications
if ($is_applied) {
return $modules;
}
// Return early if a module array is empty
if (empty($modules)) {
return $modules;
}
// Iterate over each module
foreach ($modules as $module_slug => $module) {
// Target the contact form module
if ('et_pb_contact_form' === $module_slug) {
// Ensure required properties are set
if (!isset($module->settings_modal_toggles) || !isset($module->advanced_fields) || !isset($module->fields_unprocessed)) {
continue;
}
$settings_modal_toggles = $module->settings_modal_toggles;
// Modify the 'main_content' toggle title and priority if it exists
if (isset($settings_modal_toggles['general']['toggles']['main_content'])) {
$settings_modal_toggles['general']['toggles']['main_content'] = [
'title' => et_builder_i18n('Title To New Title'),
'priority' => 5,
];
}
// Add a new custom toggle under 'general' if it exists
if (isset($settings_modal_toggles['general']) && !empty($settings_modal_toggles['general']['toggles'])) {
$settings_modal_toggles['general']['toggles']['new_custom_toggle'] = [
'title' => __('Custom Toggle', 'domain'),
'priority' => 5,
];
$module->settings_modal_toggles = $settings_modal_toggles;
}
}
}
// Set the flag to prevent re-application
$is_applied = true;
return $modules;
}
// Apply the filter to modify the parent modules
add_filter('et_builder_get_parent_modules', 'add_custom_toggle_parent_module', 10, 2);
endif;
/**
* Add custom field to unprocessed fields of ET Builder contact form.
*
* @param array $fields_unprocessed The array of unprocessed fields for the contact form.
*
* @return array Modified array of fields including custom 'sub_title' field.
*/
if (!function_exists('add_custom_et_pb_contact_form_field')) {
function add_custom_et_pb_contact_form_field($fields_unprocessed)
{
$custom_fields['sub_title'] = [
'label' => __('Sub Title', 'domain'),
'type' => 'text',
'description' => __('Description Of Field', 'domain'),
'default' => '',
'toggle_slug' => 'new_custom_toggle',
'dynamic_content' => 'text',
];
// Merge custom fields with existing fields
return wp_parse_args($custom_fields, $fields_unprocessed);
}
// Add filter to modify ET Builder contact form fields
add_filter('et_pb_all_fields_unprocessed_et_pb_contact_form', 'add_custom_et_pb_contact_form_field');
}