Skip to content

Instantly share code, notes, and snippets.

@ka215
Last active August 29, 2016 09:49
Show Gist options
  • Save ka215/2643df9e7452323ac7df2358a96e27d1 to your computer and use it in GitHub Desktop.
Save ka215/2643df9e7452323ac7df2358a96e27d1 to your computer and use it in GitHub Desktop.
CDBT: How to display the input fields (select list, combobox) that is related the column of foreign tables by using filter hook.
<?php
function my_shortcode_custom_forms( $elements_options, $shortcode_name, $table ){
if ( 'master_data' === $table ) {
global $cdbt;
$cities = $cdbt->get_data( 'cities' );
$_city_list = [];
$jobs = $cdbt->get_data( 'jobs' );
$_job_list = [];
foreach ( $elements_options as $_i => $_option ) {
$_default_value = $elements_options[$_i]['defaultValue'];
switch ( $_option['elementName'] ) {
case 'city_id':
$elements_options[$_i]['elementType'] = 'select';
foreach ( $cities as $_city ) {
$_city_list[] = $_city->city_name .':'. $_city->city_id;
if ( intval( $_default_value ) == intval( $_city->city_id ) )
$elements_options[$_i]['defaultValue'] = $_city->city_id;
}
$elements_options[$_i]['selectableList'] = implode( ',', $_city_list );
break;
case 'job':
$elements_options[$_i]['elementLabel'] = 'Job (combobox)';
$elements_options[$_i]['elementType'] = 'combobox';
foreach ( $jobs as $_job ) {
$_job_list[] = $_job->job_name .':'. $_job->job_name;
if ( $_default_value === $_job->job_name )
$elements_options[$_i]['defaultValue'] = $_job->job_name;
}
$elements_options[$_i]['elementSize'] = 3;
$elements_options[$_i]['selectableList'] = implode( ',', $_job_list );
break;
}
}
}
return $elements_options;
}
add_filter( 'cdbt_shortcode_custom_forms', 'my_shortcode_custom_forms', 10, 3 );
/**
* Reference of structure
* ---
CREATE TABLE `master_data` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`name` varchar(100) DEFAULT NULL,
`city_id` int(11) NOT NULL COMMENT 'City',
`job` varchar(200) NOT NULL,
`created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Created Datetime',
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Updated Datetime',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cities` (
`city_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'City ID',
`city_name` varchar(100) NOT NULL,
PRIMARY KEY (`city_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
| city_id | city_name |
|---------|-----------|
| 1 | Tokyo |
| 2 | Kyoto |
| 3 | Osaka |
CREATE TABLE `jobs` (
`job_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Job ID',
`job_name` varchar(100) NOT NULL,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
| job_id | job_name |
|--------|------------------|
| 1 | Civil Servant |
| 2 | Company Employee |
| 3 | Housewife |
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment