Support

Account

Home Forums Add-ons Flexible Content Field Using acf/update_value in Flexible Content Field

Solved

Using acf/update_value in Flexible Content Field

  • I currently use the “acf/update_value” in a number of normal ACF fields to grab some info on save and then update a field (in my case, getting the URL to a vimeo thumbnail).

    For example, something like this:

    	// Fetch vimeo thumbnail on save - takes video ID as input
    	function rf_acf_update_vimeo_thumb( $value, $post_id, $field ) {
    		// only set a new value if the field is empty
    		if ( $value == '') {
    			// get video here
    			$vimeo_video_id = get_field('vimeo_video_id');
    			// fetch thumb url(s)
    			$vimeo_meta = file_get_contents("https://vimeo.com/api/v2/video/$vimeo_video_id.json");
    			$vimeo_meta = json_decode($vimeo_meta);
    			$vimeo_thumb = $vimeo_meta[0]->thumbnail_large;
    			// set value
    			$value = $vimeo_thumb;
    			return $value;
    		} else {
    			return $value;
    		}
    	}
    	
        add_filter('acf/update_value/name=thumbnail_url', 'rf_acf_update_vimeo_thumb', 10, 3);

    So just re-using this with “name=another_thumbnail_url” in the filter where my flexible content field has a field within a layout named “another_thumbnail_url” doesn’t seem to do anything.

    Is there another filter specifically for getting a field within a layout?

  • The first think you need to do us use the field key variant of the acf/update_value/ hook. The reason is that sub field, field names will be different.

    The second part will be the hard part. You need to get the field from the same row of the flex field as the field that you are filtering…. rather than try to explain

    
    add_filter('acf/update_value/key=field_604150e4d40c3', 'your_function_name', 20, 4);
    function your_function_name($value, $post_id, $field, $original) {
      $name = $field['name'];
      // $name will equal {FLEX FIELD NAME}_{INDEX}_{SUB FIELD NAME}
      // we need the the flex name and index
      // stip off this sub field name
      $name = str_replace('_this_field_name', '', $name);
      // append the other sub field name
      $name .= '_other_field_name';
      // use get_post_meta to get the value from the same row
      $other_value = get_post_meta($post_id, $name, true);
      
      // set value based on other value and return
      return $value;
    }
    

    Also, I noticed that you are using a priority of 10, you should use a priority > 10 to ensure this happens after acf has saved the other value as well as making sure that the other field appears before this field when editing the page. Otherwise you may be getting the old value, or no value at all on a new post.

  • I’m lost on finding they key here. I’ve turned on field visibility on the “edit field group” page and my flexible content field, at the top level is “field_6018cb496afa3”. Then the layouts themselves don’t display a field key for the layout, but within the layout, there are keys for each row. In my example, the video ID field is “field_602f31fa1de20”.

    If I setup a filter with add_filter('acf/update_value/key=field_6018cb496afa3', 'my_function_name', 20, 4); or add_filter('acf/update_value/key= field_602f31fa1de20', 'my_function_name', 20, 4); and in my function I just var_dump everything, I get no output, suggesting that my filter is not firing because I’m not looking for the right field… Where am I going wrong here?

  • Layouts do not have field keys and they do not appear in the acf input.

    This one should be working

    
    add_filter('acf/update_value/key= field_602f31fa1de20', 'my_function_name', 20, 4);
    

    except that I am seeing a space .../key= field_...
    if you copied and pasted that then the space is the problem.

  • Chasing my tail on this, php-fpm config had a typo in the logging setup, all my debugging was going nowhere… Thanks so much though, I’m very happy to now know how to fetch these values in flex content fields – very helpful!

    So this seems to work very well. For anyone finding this while searching, I use a lazy-loader (https://github.com/vb/lazyframe) for videos to keep page weight down (and we have some pages with 50+ videos) and for it to look nice and provide flexibility if the user wants to override the default vimeo thumb, I provide a field for the video thumbnail. The user only needs to enter the vimeo video ID.

    If the thumbnail/poster field isleft blank, I want to fetch the URL to the thumbnail from vimeo. This does that.

    	// Fetch vimeo thumbnail on save - takes video ID as input
    	add_filter('acf/update_value/key=field_60412898401c1', 'rf_acf_update_vimeo_thumb_inspo', 20, 4);	
    	function rf_acf_update_vimeo_thumb_inspo( $value, $post_id, $field, $original ) {
    		/* error_log( "in wfh thumb debug, value is $value", 0 );
    		error_log( "in wfh thumb debug, post_id is $post_id", 0 );
    		error_log( "in wfh thumb debug, field is " . print_r($field, true), 0 );
    		error_log( "in wfh thumb debug, original is $original", 0 ); */
    		
    		// only set a new value if the field is empty
    		if ( $value == '') {
    			// get url field's full name (includes flex name, row #, field name concatenated w/"_")
    			$field_name = $field['name'];
    			// strip this field's name from full name
    			$field_name = str_replace('_inspo_thumbnail_url', '', $field_name);
    			// append video ID field to stripped name
    			$field_name .= '_inspo_vimeo_video_id';
    			// use get_post_meta to get the value from the same row (inspo_vimeo_video_id)
      			$vimeo_video_id = get_post_meta($post_id, $field_name, true);
    			// get video here
    			$vimeo_meta = file_get_contents("https://vimeo.com/api/v2/video/$vimeo_video_id.json");
    			$vimeo_meta = json_decode($vimeo_meta);
    			$vimeo_thumb = $vimeo_meta[0]->thumbnail_large;
    			// set value
    			$value = $vimeo_thumb;
    			return $value;
    		} else {
    			error_log('$value was not empty',0);
    			return $value;
    		}
    	}
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.