StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
794 stars 62 forks source link

Location and duplicate ACF groups #144

Closed jnaklaas closed 3 years ago

jnaklaas commented 3 years ago

I've got a number of ACF groups built with ACF Builder.

Most of them are linked directly to a page:

$mypage = new FieldsBuilder('mypage content');
$mypage->setLocation('post', '==', '7');
// add fields ...
return $mypage;

I also have a template for additional landingpages:

$landingspage = new FieldsBuilder('landingspage content');
$landingspage->setLocation('page_template', '==', 'views/template-landingspage.blade.php');
// add fields ...
return $landingspage;

Now, when I go to the "mypage" page (id 7) (or any other page with it's dedicated acfbuilder fields) in wp admin, and I change the page template from "default" to "landingspage" (or vice versa), I get the landingspage acf group AND two identical mypage acf groups.

Even when I change the location from the "mypage" fields as such:

$mypage
    ->setLocation('post', '==', '7')
    ->and('page_template', '==', 'default')

... things go wrong. Changing the template from "default" to "landingspage" shows the landingspage acf group but also the mypage acf group. I would expect only the landingspage acf group to be shown.

I suspect the issue would disappear once I save the page in wp admin. But the thing is, both acf groups have multiple required fields. So I can't save the template in the case of the two identical acf groups, and in any case, if I would fill out all required fields (in all acf groups) it would write useless stuff into the database. Not so clean.

The only way I found to get around this is by changing the template directly in the database. Which I don't want to do, obviously.

EDIT: I just noticed that each time I subsequently change the template, it adds the groups of the selected template to the groups already shown. So potentially ending up with even more groups than described above.

stevep commented 3 years ago

Hey @jnaklaas,

Sorry to hear you're having issues, but I'm not sure I'm completely understanding what is happening.

You have a set up page specific fields that you have show based on the page's ID? And then a set of template specific fields and you want both to show depending on which page you're editing and what template is selected?

And what is happening is the page based ones are showing up more than once? If you view the source do the fields have the same name attributes on the duplicated field's input tags?

One thing I notice, but not sure if it it matters is you have

$mypage->setLocation('post', '==', '7');

but should it be page instead of post?

$mypage->setLocation('page', '==', '7');
jnaklaas commented 3 years ago

Hi Stevep, thanks for replying.

Using 'post' or 'page' doesn't seem to make any difference for that matter.

So let me rephrase.

I created an ACF group using acf builder for most pages. Those pages use the default template, so I set location to the post id, for example $mypage->setLocation('post', '==', '7').

I also created an ACF group for so called "landingspages". Those pages use the "landingspage" template, so I set location to the template: $landingspage->setLocation('page_template', '==', 'views/template-landingspage.blade.php').

The issue occurs in wp admin, when selecting a different template for a page. What happens is, the acf groups that are already displayed, keep on being displayed while they should be replaced.

To elaborate, let's say I'm editing "mypage" in wp admin. Currently the page is using the default template. The acf group with location 'post', '==', '7' is displayed. After changing the template to "landingspage", the acf group with location 'post', '==', '7' as well as the acf group with location 'page_template', '==', 'views/template-landingspage.blade.php' is added to the admin panel. As a result, the acf group with location 'post', '==', '7' is displayed twice.

After switching back to the default template, again the acf group with location 'post', '==', '7' is added, now being displayed three times. Switching once more to the "landingspage" template, both acf groups are again injected in the wp admin page, now having 4 times the acf group with location 'post', '==', '7' and twice the acf group with the landingspage template location. This behaviour repeats each time I switch templates, resulting in even more duplicated acf groups.

Consequently, I can't save the page, since there are a number of required fields. Validation seems to happen on all acf groups, also on those who are not supposed to be there.

I tested and noticed this behaviour also in other web projects where I use acf builder. If I remove acf builder and only use the acf interface in wp admin, the issue does not occur.

stevep commented 3 years ago

@jnaklaas after trying to recreate it here, the issue seems to be the spaces in the builder's name.

If you change it from:

$mypage = new FieldsBuilder('mypage content');

to

$mypage = new FieldsBuilder('mypage_content');

Does that fix the issue for you?

jnaklaas commented 3 years ago

It certainly does! Thanks for getting back to me so quickly, and for developing acf builder :-)