Idea for improving 'bricks/query/run' - result from plain array

Hello,

I am building a custom query loop from a json object and I am missing some functionalities to easily do the implementation.

add_filter( 'bricks/query/run', function( $results, $query_obj ) {
    if ( $query_obj->object_type !== 'my_query_loop_type' ) {
	return $results;
    }
    
   // Here I have the data in an array format
    return [
                  ['name' => 'xxxx', 'url' => '....'],
                  ['name' => 'yyyy', 'url' => '....'],
    ];
}, 10, 2 );

I like to use the name and the url fields as a dynamic data {name}, {url}
Is there any way I can use the keys of the array as a normal dynamic fields?
Maybe to encapsulate the array in some object so the bricks builder will recognize {name} and {url}
Besides this, does someone knows how internally bricks handles dynamic data, so I better understand it and find a solution for the problem above ?

I found this post that uses echo Is there a way to use an Array for a Query Loop but it will be much better if instead of echoing a function to just type {name}

Also the ‘bricks/setup/control_options’ is missing an input fields feature

1 Like

To answer a part of my question, bricks is checking metadata for the custom fields. So the idea is to alter the default metadata and return the values from the array when needed. Bricks requires the fields to be prefixed with “cf_”.

Here is a solution so far:

add_filter( 'bricks/setup/control_options', 'bl_setup_query_controls');
function bl_setup_query_controls( $control_options ) {
    $control_options['queryTypes']['my_custom_loop'] = "My Custom loop";
    return $control_options;
};


add_filter( 'bricks/query/run', function( $results, $query_obj ) {
    if ( $query_obj->object_type !== 'my_custom_loop' ) {
	    return $results;
    }  
    return [
		['rest_email' => 'jon@test.com', 'rest_name' => 'Jon Doe'],
		['rest_email' => 'doe@test.com', 'rest_name' => 'Doe Jon'],
		['rest_email' => 'mon@test.com', 'rest_name' => 'Jon Jon Mon'],
	];
}, 10, 2 );


add_filter( 'default_post_metadata', 'custom_loop__post_meta_filter', 10, 5 );
function custom_loop__post_meta_filter( $value, $object_id, $meta_key, $single, $meta_type ){
	$loop_object = \Bricks\Query::get_loop_object();
 	foreach($loop_object as $key=>$value) {
		if ($meta_key == $key)
		   return $value;
	}
	
	return $value;
}

This will provide the keys from the array prefixed with “cf_” inside the builder in curly brackets, like this (the prefix is handled by Bricks and stripped away when passing to the wordpress funcitons):

The email is: {cf_rest_email}, the name is: {cf_rest_name}

The only thing that I am missing is to insert input fields in the custom loop popup.

2 Likes