Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badabingbreda/9d3a62cbc267f7fe6aebf50ceb919765 to your computer and use it in GitHub Desktop.
Save badabingbreda/9d3a62cbc267f7fe6aebf50ceb919765 to your computer and use it in GitHub Desktop.
Toolbox Example: Adding Relationship settings to Toolbox Field Module and Connector
<?php
add_filter( 'toolbox/helpers/settings/type=relationship' , 'my_relationship_settings' , 10, 2 );
function my_relationship_settings( $settings , $type = '' ) {
return array_merge($settings , array(
'relationship_subfield' => array(
'type' => 'text',
'label' => __( 'Subfield', 'textdomain' ),
'default' => '',
'placeholder' => __( 'Enter [0]subfieldname or leave empty to return all as objects', 'toolbox' ),
),
));
}
add_filter( 'toolbox/helpers/get_acf_field/type=relationship' , 'acf_return_relationship_subfield', 10, 5 );
function acf_return_relationship_subfield( $string , $field_object, $value, $atts, $postid ) {
if (!isset($atts['relationship_subfield'])) return $string;
if ( $atts['relationship_subfield'] != '' ) {
$re = '/^[([0-9]{1,})]([a-z_0-9]{1,})/m';
// matches[1] : fieldname
// matches[2] : index
//
preg_match( $re , $atts['relationship_subfield'] , $matches );
// get the first match only
if ( $matches ) {
// turn the relationship value into an array
$string = toolboxUtils::post_object_to_array( $string );
// get the field_object
$object = get_field_object( $matches[2] , $string[ $matches[1] ] );
// if fieldname isn't a gallery return the object
if ( empty( $object ) || ! isset( $object['type'] ) || 'gallery' != $object['type'] ) {
return get_field( $matches[2] , $string[ $matches[1] ] );
} elseif ( is_array( $object['value'] ) ) {
foreach ( $object['value'] as $key => $value ) {
$content[] = $value['id'];
}
}
return $content;
}
}
// else
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment