Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform acf-json to acf-builder #131

Open
jnaklaas opened this issue Feb 12, 2021 · 4 comments
Open

Transform acf-json to acf-builder #131

jnaklaas opened this issue Feb 12, 2021 · 4 comments

Comments

@jnaklaas
Copy link

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 :)

@stevep
Copy link
Member

stevep commented Feb 12, 2021

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.

@davidwebca
Copy link

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 😂

@voyou-sbeaulieu
Copy link

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
Copy link

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants