Skip to content

Commit

Permalink
#5 - Enable conditional logic on ACF Country (v4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed May 3, 2017
1 parent 238db10 commit 97a7a68
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
156 changes: 156 additions & 0 deletions assets/js/acf-country-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,162 @@
}
});

} else {

acf.conditional_logic = $.extend(acf.conditional_logic, {
render : function( $field ){

// reference
var _this = this;


// vars
var choices = [],
key = $field.attr('data-id'),
$ancestors = $field.parents('.fields'),
$tr = $field.find('> .field_form_mask > .field_form > table > tbody > tr.conditional-logic');


$.each( $ancestors, function( i ){

var group = (i == 0) ? acf.l10n.sibling_fields : acf.l10n.parent_fields;

$(this).children('.field').each(function(){


// vars
var $this_field = $(this),
this_id = $this_field.attr('data-id'),
this_type = $this_field.attr('data-type'),
this_label = $this_field.find('tr.field_label input').val();


// validate
if( this_id == 'field_clone' )
{
return;
}

if( this_id == key )
{
return;
}


// add this field to available triggers
if( this_type == 'country' || this_type == 'select' || this_type == 'checkbox' || this_type == 'true_false' || this_type == 'radio' )
{
choices.push({
value : this_id,
label : this_label,
group : group
});
}


});

});


// empty?
if( choices.length == 0 )
{
choices.push({
'value' : 'null',
'label' : acf.l10n.no_fields
});
}


// create select fields
$tr.find('.conditional-logic-field').each(function(){

var val = $(this).val(),
name = $(this).attr('name');


// create select
var $select = acf.helpers.create_field({
'type' : 'select',
'classname' : 'conditional-logic-field',
'name' : name,
'value' : val,
'choices' : choices
});


// update select
$(this).replaceWith( $select );


// trigger change
$select.trigger('change');

});

},
change_trigger : function( $select ){

// vars
var val = $select.val(),
$trigger = $('.field_key-' + val),
type = $trigger.attr('data-type'),
$value = $select.closest('tr').find('.conditional-logic-value'),
choices = [];


// populate choices
if( type == "true_false" )
{
choices = [
{ value : 1, label : acf.l10n.checked }
];

}
else if( type == "country" || type == "select" || type == "checkbox" || type == "radio" )
{
var field_choices = $trigger.find('.field_option-choices').val().split("\n");

if( field_choices )
{
for( var i = 0; i < field_choices.length; i++ )
{
var choice = field_choices[i].split(':');

var label = choice[0];
if( choice[1] )
{
label = choice[1];
}

choices.push({
'value' : $.trim( choice[0] ),
'label' : $.trim( label )
});

}
}

}


// create select
var $select = acf.helpers.create_field({
'type' : 'select',
'classname' : 'conditional-logic-value',
'name' : $value.attr('name'),
'value' : $value.val(),
'choices' : choices
});

$value.replaceWith( $select );

$select.trigger('change');

}
});

}

})(jQuery);
43 changes: 43 additions & 0 deletions fields/acf-country-v4.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,27 @@ function __construct( $settings )
function create_options( $field )
{
$key = $field['name'];
$countries = acf_country_helpers::get_countries();
array_walk($countries, function(&$value, $key) {
$value = $key . ' : ' . $value;
});
$choices = implode("\n", $countries);
?>
<tr class="field_option field_option_<?php echo $this->name; ?> hidden">
<td class="label">
<label for=""><?php _e("Choices",'acf'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'textarea',
'class' => 'textarea field_option-choices',
'name' => 'fields['.$key.'][choices]',
'value' => $choices,
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e('Allow Null?', 'acf'); ?></label>
Expand Down Expand Up @@ -235,6 +255,29 @@ function input_admin_enqueue_scripts()

}

/*
* field_group_admin_enqueue_scripts()
*
* This action is called in the admin_enqueue_scripts action on the edit screen where your field is edited.
* Use this action to add CSS + JavaScript to assist your create_field_options() action.
*
* $info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
* @type action
* @since 3.6
* @date 23/01/13
*/
function field_group_admin_enqueue_scripts()
{

// vars
$url = $this->settings['url'];
$path = $this->settings['path'];
$version = $this->settings['version'];

wp_register_script( 'acf-country-group', "{$url}assets/js/acf-country-group.js", array('acf-field-group'), $version );
wp_enqueue_script('acf-country-group');
}

}


Expand Down

0 comments on commit 97a7a68

Please sign in to comment.