Support

Account

Home Forums Add-ons Repeater Field Convert raw repeater data back into an array

Solved

Convert raw repeater data back into an array

  • Hi,
    I need to get the data stored in a block’s repeater field that is located in a different post. I’m able to do the first part using parse_blocks() to locate the specific block and get its raw data. The problem I’m having is that the repeater data is stored in its flat format and I need to get it back into an array so I can use it.

    I’ve been trying all kinds of functions that seem to be part of a solution but none have worked. The closest I’ve gotten so far is calling acf_setup_meta( $block['attrs']['data'] ) then I can use acf_get_meta_field to get the repeater field’s settings which I can pass to acf_get_field_type( 'repeater' )->load_field(), but that’s as far as I’ve gotten.

    Anyone know how to do this?

  • what does the data that needs to be converted actually look like?

  • @hube2 Here’s what the block’s data looks like from parse_blocks():

    Array (
       [blockName] => site/recipes
       [attrs]     => Array (
          [name] => site/recipes
          [data] => Array (
             [heading_level]                                   => h2
             [_heading_level]                                  => blocks-recipes-heading_level
             [items_per_page]                                  => 9
             [_items_per_page]                                 => blocks-recipes-items_per_page
             [show_pagination]                                 => 1
             [_show_pagination]                                => blocks-recipes-show_pagination
             [filter_categories_0_category]                    => easy_meal_categories
             [_filter_categories_0_category]                   => blocks-recipes-filter_categories-category
             [filter_categories_0_easy_meal_categories-value]  => 30-minutes-or-less
             [_filter_categories_0_easy_meal_categories-value] => blocks-recipes-filter_categories-easy_meal_categories-value
             [filter_categories_1_category]                    => consumer_symbols
             [_filter_categories_1_category]                   => blocks-recipes-filter_categories-category
             [filter_categories_1_consumer_symbols-value]      => dairy-free
             [_filter_categories_1_consumer_symbols-value]     => blocks-recipes-filter_categories-consumer_symbols-value
             [filter_categories]                               => 2
             [_filter_categories]                              => blocks-recipes-filter_categories
          )
          [mode] => preview
       )
       [innerBlocks]  => Array ()
       [innerHTML]    => EMPTY STRING
       [innerContent] => Array ()
    )
    

    filter_categories is a repeater containing a dropdown to select the category and another dropdown to select the value. Each category has its own set of values so there’s a conditional on each value dropdown to match the selected category. That’s why for the value the category name is a part of the key filter_categories_0_<category>-value. In the above code, there are 2 filters selected, easy_meal_categories = 30-minutes-or-less and consumer_symbols = dairy-free.

    I realize I could do this the hard way and parse the keys out, but that’s brittle and especially difficult if repeaters are nested.

  • That’s interesting and it will not be easy to convert that.

    An explanation
    this holds the field value [heading_level]
    this holds the ACF field key reference [_heading_level]
    In this case blocks-recipes-heading_level
    I am assuming this is constructed like "block-{$block_name}-{$field_name}"
    block- simply tells ACF that this field belongs to a block.

    On to the repeater
    This [filter_categories_0_category] the ‘category’ field from the 1st row of the the ‘filter_categories’ repeater. This index is constructed like this "{$repeater_field_name}_{$row_index}_{$sub_field_name}"

    In order to build an array you would need to loop over the data and parse the array index and create what ACF usually stores in an array. You would also need to format the values.

    Going to be honest, I have no idea how you’d do this, especially considering that you have multiple possible secondary sub fields dependent of the value of the first sub field.

  • Thanks to ACF support’s help, this has been solved!

    
    acf_setup_meta( $block['attrs']['data'], acf_get_block_id( $block['attrs']['data'] ), true );
    get_field( 'filter_categories' );
    

    The true in the call to acf_setup_meta() means it’s replacing the main meta data, which you might not want to do if you still need to access it. You can do it this way to preserve it:

    
    $block_id = acf_get_block_id( $block['attrs']['data'] );
    acf_setup_meta( $block['attrs']['data'], $block_id );
    get_field( 'filter_categories', $block_id );
    
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.