acf-builder icon indicating copy to clipboard operation
acf-builder copied to clipboard

Transform acf-json to acf-builder

Open jnaklaas opened this issue 3 years ago • 4 comments

Is there by any chance a way to transform acf-json to acf-builder? I'm about to do a major update on a website with a ton of acf fields. Perhaps it might save some time if I could transform the existing fields to acf-builder.

I would write some nodejs to achieve this myself... but then I'm most likely not saving time :)

jnaklaas avatar Feb 12 '21 11:02 jnaklaas

Thats a pretty interesting idea. There is no way for a FieldsBuilder class to import JSON currently. Depending on how much work you have to do to the existing fields, I would probably just convert the ones you really needed as you went, especially if you feel like you're gonna have to change them often.

stevep avatar Feb 12 '21 14:02 stevep

Oh my the number of times I wish this existed. I'm leaving a comment to show my interest, but also might end up doing it 😂

davidwebca avatar Aug 18 '22 17:08 davidwebca

Is there an other way around ? I noticed that with huge number of field, acf-builder is slower then acf-json. Maybe there's a way that instead of using "acf_add_local_field_group" we create / update .json from local-json from build() to speed up the TTFB/server time?

voyou-sbeaulieu avatar Nov 09 '22 13:11 voyou-sbeaulieu

Hi just to add if this can help anyone, we change the acf_add_local_field_group and we now create a new json file from the build(). We do this only when options page is saved (as trigger) and not on all page load and this has really upgrade performance.

Here's the function

$local_json_path = function ($path) {
	// update path
	$path = config('theme.dir')  . '/app/acf-json';
	// return
	return $path;
};

add_filter('acf/settings/load_json', $local_json_path);

add_action('acf/save_post', function () {
	$screen = get_current_screen();
	if ($screen->id == $option_page_ID) { //This ID need to be you options page custom ID
		collect(glob(config('theme.dir') . '/app/fields/*.php'))->map(function ($field) {
			return require_once($field);
		})->map(function ($field) {
			if ($field instanceof FieldsBuilder) {
				$export = $field->build();
				if ($export && !empty($export)) {
					$path = config('theme.dir')  . '/app/acf-json/';
					file_put_contents($path . $export['key'] . '.json', json_encode($export, JSON_PRETTY_PRINT));
				}
			}
		});
	}
}, 20);

voyou-sbeaulieu avatar Nov 09 '22 16:11 voyou-sbeaulieu