Outputting Data from Cloneable Groups In Custom MB-Blocks

Support MB Blocks Outputting Data from Cloneable Groups In Custom MB-Blocks

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #33138
    Catharina von HobeCatharina von Hobe
    Participant

    I created a custom Block (the Fields) with the Builder and the Template in a separate PHP document.
    Inserting the Block in a Page works and the Static Code like Div's and other HTML-Tags are displayed correctly, so its configured correctly in the 'Field Group'/Block Settings.

    Since all my custom field's are inside a clonable Group in that Block, i dont really understand how I receive the data.

    I tried the 2 helper functions mb_the_block_field(''), and mb_get_block_field(''), but as far as i understood the MB Blocks extension description, these only output non-grouped fields.
    For Groups i need to use:

    $alternating_block_values = rwmb_meta( 'alternating-block_group' );
    if ( ! empty( $alternating_block_values ) ) {
        foreach (  $alternating_block_values as $alternating_block ) {
            $value = isset( $alternating_block[$sub_field_key] ) ? $alternating_block[$sub_field_key] : '';
            echo $value; // Display sub-field value
        }
    }

    because it separates the Values in the Array it catches from the Database.

    But Blocks arent saving their Data in the Database.
    And thus works only outside Blocks, also not catching the Data for the current Block like the Helper functions would.

    Now i dont really know how to output the correct Data for a clone inside of a Custom MB-Block.

    <hr />

    Further Informations:

    My Fields:

    • alternating-block_group = [group]
    • alternating-block_top-heading = [Text]
    • alternating-block_sub-heading = [Text]
    • alternating-block_textarea = [WYSIWYG Editor]
    • alternating-block_image = [Single Image]
    • alternating-block_image-handling = [Button Group]
    • alternating-block_image-position = [Button Group]

    Example of my Code for the Subheading and Text:

    <div>
      <h3>
        <?php mb_the_block_field('alternating-block_sub-heading'); ?>
      </h3>
      <p>
        <?php mb_the_block_field('alternating-block_textarea'); ?>
      </p>
    </div>
    #33148
    Long NguyenLong Nguyen
    Moderator

    Hi,

    To get a field value in the callback function or template, you can use the helper function mb_get_block_field(). For example:

    $alternating_block_values = mb_get_block_field( 'alternating-block_group' );
    if ( ! empty( $alternating_block_values ) ) {
        foreach (  $alternating_block_values as $alternating_block ) {
            $value = isset( $alternating_block['alternating-block_top-heading'] ) ? $alternating_block['alternating-block_top-heading'] : '';
            echo $value; // Display sub-field value
        }
    }

    Get more details on the documentation https://docs.metabox.io/extensions/mb-blocks/#render_callback

    #33152
    Catharina von HobeCatharina von Hobe
    Participant

    Thanks this works.
    And if i understood the coding correctly i need to copy that for every input or do i need this just once and it can catch every of my 6 other inputs. (excluded the group)

    #33153
    Catharina von HobeCatharina von Hobe
    Participant

    Ah nevermid i figured it out myself.
    I can just copy the echos inside the foreach.

    Thanks for the quick help

    #33154
    Catharina von HobeCatharina von Hobe
    Participant

    And how do i copy the static code for every clone?

    #33158
    Catharina von HobeCatharina von Hobe
    Participant

    Ok i also got the repeatin functionality for the static code working, but the images doesnt seem to work correctly this way.

    Do i need to change something for the images?

    <div id="<?= $id ?>" class="cvh--mb-block <?= $class ?>">
      <?php if ( ! empty( $alternating_block_values ) ) {
        foreach (  $alternating_block_values as $alternating_block ) { 
          // Image
          $img = isset( $alternating_block['alternating-block_image'] ) ? $alternating_block['alternating-block_image'] : '';
    ...
    ?>
          <div>
    
        ...
    
        <div  class="ct-div-block ">
          <img alt="" src="<?= $img['full_url'] ?>" class="ct-image cvh--post-rep-ctn-img">
       </div>
      </div>
    <?php }
    
    } ?>
    </div>
    
    #33161
    Long NguyenLong Nguyen
    Moderator

    Hi,

    To show the image subfield in a group, you can use the helper function RWMB_Image_Field::file_info(). For example:

    $image_id = isset( $alternating_block['alternating-block_image'] ) ? $alternating_block['alternating-block_image'] : '';
    $image = RWMB_Image_Field::file_info( $image_id, ['size' => 'thumbnail'] );
    echo '<img src="' . $image['url'] . '">';

    Get more details here https://docs.metabox.io/extensions/meta-box-group/#sub-field-values

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