Getting the field data (text and images) for specific sections in cloned groups

Support Meta Box AIO Getting the field data (text and images) for specific sections in cloned groups

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #46426
    FredrikFredrik
    Participant

    I'm using Metabox with Bricks and I want a function to populate a page with the field from specific clones in a Metabox Custom Post.

    It works with this code for text:

    
    function get_metabox_cloned_field($post_id, $field_group, $field_name, $clone_index = 0) {
        $meta_value = get_post_meta($post_id, $field_group, true);
        
        if (!is_array($meta_value) || !isset($meta_value[$clone_index])) {
            return null;
        }
        
        $cloned_group = $meta_value[$clone_index];
        
        return isset($cloned_group[$field_name]) ? $cloned_group[$field_name] : null;
    }
    
    // Register the function as a Bricks dynamic data source
    add_filter('bricks/dynamic_data/register_sources', function($sources) {
        $sources['metabox_cloned_field'] = [
            'label' => 'Metabox Cloned Field',
            'callbackFunction' => function($post_id, $field_group, $field_name, $clone_index) {
                return get_metabox_cloned_field($post_id, $field_group, $field_name, $clone_index);
            },
        ];
        return $sources;
    });
    
    // Make sure the function works with Bricks dynamic data syntax
    add_filter('bricks/dynamic_data/render_content', function($content) {
        return preg_replace_callback('/\{echo:get_metabox_cloned_field\((.*?)\)\}/', function($matches) {
            $args = explode(',', $matches[1]);
            $args = array_map('trim', $args);
            $post_id = get_the_ID(); // Always use the current post ID
            $field_group = trim($args[1], "'\"");
            $field_name = trim($args[2], "'\"");
            $clone_index = isset($args[3]) ? intval($args[3]) : 0;
            
            return get_metabox_cloned_field($post_id, $field_group, $field_name, $clone_index);
        }, $content);
    }, 10, 1);
    

    I'm wondering if this will be sustainable when Metabox is updating or if there's a better approach?

    And I'm wondering if you can guide me on how to get the equivalent of images?

    Right now I got this code for images, but it doesn't work:

    
    function get_metabox_cloned_image($post_id, $field_group, $clone_index = 0, $image_index = 0, $size = 'full', $debug = false) {
        $meta_value = get_post_meta($post_id, $field_group, true);
        
        if ($debug) {
            echo '<pre>';
            print_r($meta_value);
            echo '</pre>';
            return;
        }
        
        if (!is_array($meta_value) || !isset($meta_value[$clone_index]['images'])) {
            return null;
        }
        
        $images = $meta_value[$clone_index]['images'];
        
        if (!isset($images[$image_index])) {
            return null;
        }
        
        $image_id = $images[$image_index];
        return wp_get_attachment_image_url($image_id, $size);
    }
    
    add_filter('bricks/dynamic_data/register_sources', function($sources) {
        $sources['metabox_cloned_image'] = [
            'label' => 'Metabox Cloned Image',
            'callbackFunction' => function($post_id, $field_group, $clone_index, $image_index, $size = 'full', $debug = false) {
                return get_metabox_cloned_image($post_id, $field_group, $clone_index, $image_index, $size, $debug);
            },
        ];
        return $sources;
    });
    
    add_filter('bricks/dynamic_data/render_content', function($content) {
        return preg_replace_callback('/\{echo:get_metabox_cloned_image\((.*?)\)\}/', function($matches) {
            $args = explode(',', $matches[1]);
            $args = array_map('trim', $args);
            $post_id = get_the_ID();
            $field_group = trim($args[1], "'\"");
            $clone_index = isset($args[2]) ? intval($args[2]) : 0;
            $image_index = isset($args[3]) ? intval($args[3]) : 0;
            $size = isset($args[4]) ? trim($args[4], "'\"") : 'full';
            $debug = isset($args[5]) ? (trim($args[5], "'\"") === 'true') : false;
            
            return get_metabox_cloned_image($post_id, $field_group, $clone_index, $image_index, $size, $debug);
        }, $content);
    }, 10, 1);
    
    #46436
    PeterPeter
    Moderator

    Hello Fredrik,

    It is sustainable to use the WordPress function get_post_meta() to get the field value from the table wp_postmeta when Meta Box is updated or even deactivated.

    Regarding the image, which is the field type that you are using?

    #46437
    FredrikFredrik
    Participant

    Good!

    It's Image advanced, where I need a function to grab either the 1st, 2nd, 3rd and so on of a specificed clone.

    #46450
    PeterPeter
    Moderator

    Hello,

    Can you please export the field group to a JSON file and share it here? I need to check the settings of group and image advanced fields to give a better suggestion to get the field value.
    Following the documentation https://docs.metabox.io/extensions/meta-box-builder/#export--import

    #46455
    FredrikFredrik
    Participant

    Sure, this is what it looks like in the code view

    
    <?php
    $groups = rwmb_meta( 'aspects' );
    foreach ( $groups as $group ) {
    	
    	// Field heading:
    	echo $group[ 'heading' ] ?? '';
    	
    	// Field accent_heading:
    	echo $group[ 'accent_heading' ] ?? '';
    	
    	// Field text:
    	<?php $values = $group[ 'text' ] ?? [];
    	<?php foreach ( $values as $value ) : ?>
    		<div class="my-content"><?= $value ?></div>
    	<?php endforeach ?>
    	
    	// Field images:
    	$image_ids = $group[ 'images' ] ?? [];
    	?>
    	<h3>Uploaded images</h3>
    	<ul>
    		<?php foreach ( $image_ids as $image_id ) : ?>
    			<?php $image = RWMB_Image_Field::file_info( $image_id, [ 'size' => 'thumbnail' ); ?>
    			<li><img src="<?= $image['url']; ?>"></li>
    		<?php endforeach ?>
    	</ul>
    	<?php
    
    }
    ?>
    
    #46461
    PeterPeter
    Moderator

    Hello,

    I see that the function get_metabox_cloned_image() to get the image advanced field value in a cloneable group is correct. But I'm not sure about the code in 2 filter hooks bricks/dynamic_data/register_sources and bricks/dynamic_data/render_content. Maybe you can ask the Bricks support to get further assistance.

    #46480
    FredrikFredrik
    Participant

    I forwarded this to Bricks Support and their response is that custom codiing is generally outside of their support scope, but that:

    But, if you have an image, and you call custom function {echo:get_my_image}, and this function returns ID of the image (media file), it will show it.

    So {echo:custom_image}

    Together with this function

    function custom_image() {
    return 162;
    }

    Will render the image with an id of 162.

    Does that help with figuring out the last bit for you?

    #46494
    PeterPeter
    Moderator

    Hello,

    As I mentioned above, I'm not sure how the filter of Bricks works on their side. If it is possible, you can try to use the theme code generated by Meta Box, use a WordPress theme (2021), add the code to the post template and see if the image advanced is outputted in the frontend.
    If it works properly, then it isn't an issue with Meta Box itself.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.