Well,
i frequently use cf7 hooks to get submitted data and save it to user metas or external repositories of data. Sometimes the forms have fields that involve long list of options that i dont want to write again and again in the templates: is prone to errors in copy/paste. So i decide to use listo to supply this options to cf7. I enjoy the simplicity of listo, but for my purpouses the list available list is short. No problem, i use the filter listo_list_types
of listo to add more lists pointing new lists names in listo to a set of clases loaded with an autoloader that implement Listo interface. The problem is that i also use frequently the pipe syntax for select values. I haven’t found a way to use pipe syntax when setting listo as values resource, so i ended making more hacks to get it work: I need to use wpcf7_add_form_tag
and wpcf7_remove_form_tag
in a function hooked on wpcf7_init
to replace select module. The new module is just a verbatim copy of the standard adding just 3 lines to use a custom option named data_values_as_keys
that enable the use of values or keys of listo returned items if it values is true or false respectively. I wish this feature were in cf7 instead the need to do hacks. I have to review any update of cf7 to see if select module is modified. Although im not doing modifications on original code, in practice it is coz i’m totally replacing this module.
I hope i have explained.
some code to illustrate:
– Somewhere in a extension plugin or in a theme:
...
add_action( 'wpcf7_init', 'custom_wpcf7_add_form_tag_select', 9, 0 );
function custom_wpcf7_add_form_tag_select() {
wpcf7_remove_form_tag('select');
wpcf7_remove_form_tag('select*');
wpcf7_add_form_tag( array( 'select', 'select*' ),
'custom_wpcf7_select_form_tag_handler',
array(
'name-attr' => true,
'selectable-values' => true,
)
);
}
function custom_wpcf7_select_form_tag_handler( $tag ) {
...
...
$valuesAsKeys = $tag->get_option( 'data_values_as_keys', '', true);
if ($valuesAsKeys == 'true') {
$tag->values = array_merge( $tag->values, array_keys( $data ) );
} else {
$tag->values = array_merge( $tag->values, array_values( $data ) );
}
...
...
– Exceprt of custom listo extension plugin
...
...
define( 'CUSTOM_LISTO_VERSION', '0.1' );
define( 'CUSTOM_LISTO_PLUGIN_DIR', dirname( __FILE__ ) );
define( 'CUSTOM_LISTO_LISTS_DIR', path_join( CUSTOM_LISTO_PLUGIN_DIR, 'lists' ) );
add_filter('listo_list_types','custom_listo_add_custom_types',10,1);
function custom_listo_add_custom_types ( $list_types ) {
$lists = array_filter(scandir(CUSTOM_LISTO_LISTS_DIR), function($item) {
return (is_file(path_join(CUSTOM_LISTO_LISTS_DIR, $item)) && preg_match('/^Listo_.+\.php$/', $item));
});
foreach($lists as $list) {
$class = substr($list,0,-4);
$type = substr($class,6);
$list_types[$type] = $class;
}
return $list_types;
}
spl_autoload_register(
function($class) {
if (substr($class,0,10) != 'Listo_ctm_') {
return;
}
$classPath = path_join( CUSTOM_LISTO_LISTS_DIR, $class . '.php' );
if ( file_exists( $classPath ) ) {
require_once $classPath;
}
}
);
...
Inside a lists dir of listo extension plugin would be n files with Listo_cstm_xxxxxxxx.php with a class in each file implementing Listo interface.
I wish this option in select tag enable this feature avoiding hacks. Maybe would be a hook triggered in modules to give hooked functions a chance to change something, altough it maybe would involve more coupling, i dont know.
Thanks, in advance and sorry for poor english.
-
This reply was modified 5 years, 9 months ago by
angarf.