Home Forums Gutenberg Pre-populate ACF fields in Blocks for users to customize?


Pre-populate ACF fields in Blocks for users to customize?

    My client uses the Events Calendar by Modern tribe and I would like to pull data from the event (ACF post_object) and prepopulate ACF fields in a gutenberg block. The user should be able to edit the content, headline, image, etc based on the original content from the event. This content does not need to be synced back to the event.

    Cannot load field data into ACF block fields at all. I have been able to get the ACF fields to work when filled in manually, but unable to dynamically update the default value or current value.

    Ideally, the user would select a single event from the Events Calendar post type (events_to_promote) and the Block would set the default value to the data set in the event. This could be customized and changed for that block.

    Here is what I have in my template to render the block. I have been able to get a variation of this code to work that is not dynamic, so I’m only including the first half where I set all the variables.

     * Block Name: Newsletter
     * This is the template that displays the newsletter block.
    // create id attribute for specific styling
    $id = 'newsletter-' . $block['id']; 
    $userEdited = false;
    $lcccdebug = false;
    // Declare variables
    $details_loaded = $url_loaded = $headline_loaded = $imageATTS_loaded = $imageSRC_loaded = $kicker_loaded = $description_loaded = $tickets_loaded = '';
    //get selected events
    $events = get_field('event_to_promote');
    if ($lcccdebug) {
        echo '<pre>';
        print_r( get_field('event_to_promote')  );
        echo '</pre>';
    //Update Posts Query
    if( $events ): 
        global $post; // Need to make sure we overwrite the global Post Object
    	$post = $events; // override $post
        setup_postdata( $post ); 
    // create align class ("alignwide") from block setting ("wide")
    $align_class = $block['align'] ? 'align' . $block['align'] : '';
    // Gather data from event and add to variables 
    if (tribe_is_event(get_the_ID())) { 
        global $details_loaded;
        $eventDate = tribe_get_start_date(get_the_ID(), false, 'l, F j');
        $eventTime = tribe_get_start_time(get_the_ID());
        $details_loaded = $eventDate . " @ " . $eventTime;
        if ($lcccdebug) {
            echo '$details_loaded ' . $details_loaded;
    if (the_permalink()) {
        global $url_loaded;
        $url_loaded = the_permalink();
        if ($lcccdebug) {
            echo '$url_loaded ' . $url_loaded;
    if (the_title()) {
        global $headline_loaded;
        $headline_loaded = the_title();
        if ($lcccdebug) {
            echo '$headline_loaded ' . $headline_loaded;
    if (get_post_thumbnail_id( get_the_ID() )) {
        global $imageID_loaded, $imageATTS_loaded, $imageSRC_loaded;
        $post_id = get_the_ID();
        $imageID_loaded = get_post_thumbnail_id( $post_id );
        $size = 'rss-featured';
        $imageATTS_loaded = wp_get_attachment_image_src( $imageID_loaded, $size );
        $imageSRC_loaded = $imageATTS_loaded[0];
        if ($lcccdebug) {
            echo '$imageID_loaded ' . $imageID_loaded;
            echo '$imageSRC_loaded ' . $imageSRC_loaded;
    if (get_field('kicker')) {
        global $kicker_loaded;
        $kicker_loaded = get_field('kicker', $post->ID);
        if ($lcccdebug) {
            echo '$kicker_loaded ' . $kicker_loaded;
    if (the_advanced_excerpt()) {
        global $description_loaded;
        $description_loaded = the_advanced_excerpt();
        if ($lcccdebug) {
            echo '$description_loaded ' . $description_loaded;
    if (get_field('thundertix_embed_code')) {
        global $tickets_loaded;
        $tickets_loaded = get_field('thundertix_embed_code', $post->ID);
    // Update ACF Fields
    if ($details_loaded) {
        update_field('custom-details', $details_loaded);
    if ($url_loaded) {
        update_field('custom-url', $url_loaded);
    if ($headline_loaded) {
        update_field('custom-headline', $headline_loaded);
    if ($imageID_loaded) {
        update_field('custom-photo', $imageID_loaded);
    if ($description_loaded) {
        update_field('custom-description', $description_loaded);
    if ($kicker_loaded) {
        update_field('custom-kicker', $kicker_loaded);
    // Get ACF Fields
    $button = get_field('custom-button');
    $kicker = get_field('custom-kicker');
    $headline = get_field('custom-headline');
    $imageID = get_field('custom-photo');
    $details = get_field('custom-details');
    $description = get_field('custom-description');
    $url = get_field('custom-url');
    $button = get_field('custom-button');
    // Image Definitions
    $size = 'rss-featured';
    $imageATTS = wp_get_attachment_image_src( $imageID, $size );
    $imageSRC = $imageATTS[0];

    Thanks for any assistance!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.