Created
August 2, 2018 13:48
-
-
Save antishow/7e2aff5c92849e977f047d10f233268e to your computer and use it in GitHub Desktop.
Populate Grid Data repeater subfields with clones of all Card Layouts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Helper function to extract the Card Data field from the Card Grid field group | |
*/ | |
function get_grid_data_field() { | |
$card_grid_field_group = acf_get_field_group('group_card_grid'); | |
if (!$card_grid_field_group) { | |
return; | |
} | |
$card_grid_fields = acf_get_fields($card_grid_field_group['ID']); | |
$grid_data_field = array_filter($card_grid_fields, function($f) { | |
return $f['key'] === 'field_card_grid_data'; | |
}); | |
return array_pop($grid_data_field); | |
} | |
/** | |
* Returns an array describing a field that is a clone of the specified | |
* field group, ready to use in the Card Data repeater’s sub fields | |
*/ | |
function get_card_layout_clone($card_layout_field_group) { | |
if (!$card_layout_field_group) { | |
return false; | |
} | |
$grid_data_field = get_grid_data_field(); | |
if (!$grid_data_field) { | |
return false; | |
} | |
$base_group_key = str_replace('group_', '', $card_layout_field_group['key']); | |
$name = sprintf('_%s_fields', $base_group_key); | |
return [ | |
'menu_order' => 0, | |
'key' => sprintf('field_%s', uniqid()), | |
'label' => '', | |
'name' => $name, | |
'_name' => '', | |
'type' => 'clone', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => [ | |
[ | |
[ | |
'field' => 'field_card_grid_style', | |
'operator' => '==', | |
'value' => $base_group_key, | |
], | |
], | |
], | |
'parent' => $grid_data_field['ID'], | |
'wrapper' => [ | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
], | |
'clone' => [sprintf('group_%s', $base_group_key)], | |
'display' => 'group', | |
'layout' => 'block', | |
'required' => 0, | |
'prefix_label' => 0, | |
'prefix_name' => 0, | |
]; | |
} | |
/** | |
* When saving a field group, sync the Card Grid > Card Data field with | |
* the list of Card Layouts | |
*/ | |
add_action('acf/update_field_group', function($field_group) { | |
$grid_data_field = get_grid_data_field(); | |
$card_layouts = get_card_layouts(); | |
$existing_card_layouts = implode(',', array_map(function($l) { | |
return $l['key']; | |
}, $card_layouts)); | |
$active_card_layouts = implode(',', array_map(function($f) { | |
return $f['clone'][0]; | |
}, $grid_data_field['sub_fields'])); | |
//DONT UPDATE if the lists already match, or this will loop forever | |
if ($active_card_layouts === $existing_card_layouts) { | |
return; | |
} | |
write_log('GRID DATA FIELD BEFORE:'); | |
write_log($grid_data_field); | |
$new_grid_data_field = array_merge($grid_data_field, [ | |
'sub_fields' => array_map('get_card_layout_clone', $card_layouts), | |
]); | |
write_log('GRID DATA FIELD TO SAVE:'); | |
write_log($new_grid_data_field); | |
acf_update_field($new_grid_data_field); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment