Hi,
I would like to know how could i use ACF to create reusable component fields that can be managed from one place, let’s say inside the options page.
I’ve tried using clone fields for that but the values don’t get synced between the main and the clones. No matter if the slug has a prefix or not.
Right now i’ve created a way in which i use ACF prepare_field filter. This works for simple fields but i’m not sure how could i use the same logic on for example repeater fields.
Here’s the code i’m running right now
add_filter('acf/prepare_field', function($field){
$current_screen = get_current_screen();
if($current_screen && $current_screen->id == 'toplevel_page_theme-settings') return $field;
$components = get_field('components', 'option');
foreach($components as $component){
if(array_key_exists($field['_name'], $component)){
if(isset($field['sub_fields'])){
foreach($field['sub_fields'] as $field_key => $sub_field){
foreach($component[$field['_name']] as $sub_component){
if(array_key_exists($sub_field['_name'], $sub_component)){
//$field['sub_fields'][$field_key]['value'] = $sub_component[$sub_field['_name']];
//consoleLog([$field, $sub_field, $sub_component[$sub_field['_name']]]);
}
}
}
}else{
$field['value'] = $component[$field['_name']];
}
}
}
return $field;
},10,3)
And here’s the ACF output for components field group
{
"key": "field_638edca18a922",
"label": "Components",
"name": "",
"aria-label": "",
"type": "tab",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"placement": "left",
"endpoint": 0
},
{
"key": "field_638edcad8a923",
"label": "Components",
"name": "components",
"aria-label": "",
"type": "flexible_content",
"instructions": "Reusable components that can be managed from one place",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"layouts": {
"layout_638edcb3c4b41": {
"key": "layout_638edcb3c4b41",
"name": "get_in_touch",
"label": "Get In Touch",
"display": "block",
"sub_fields": [
{
"key": "field_638edcc28a924",
"label": "Heading",
"name": "get_in_touch_heading",
"aria-label": "",
"type": "textarea",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"rows": 3,
"placeholder": "",
"new_lines": ""
},
{
"key": "field_638edcd28a925",
"label": "Button",
"name": "get_in_touch_button",
"aria-label": "",
"type": "link",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"return_format": "array"
}
],
"min": "",
"max": ""
},
"layout_638f209874700": {
"key": "layout_638f209874700",
"name": "icon_group",
"label": "Icon group",
"display": "block",
"sub_fields": [
{
"key": "field_638f20b774701",
"label": "Icon group",
"name": "icon_group",
"aria-label": "",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"layout": "table",
"min": 0,
"max": 0,
"collapsed": "",
"button_label": "Add Row",
"rows_per_page": 20,
"sub_fields": [
{
"key": "field_638f20ca74702",
"label": "Icon",
"name": "icon",
"aria-label": "",
"type": "image",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"return_format": "array",
"library": "all",
"min_width": "",
"min_height": "",
"min_size": "",
"max_width": "",
"max_height": "",
"max_size": "",
"mime_types": "",
"preview_size": "thumbnail",
"parent_repeater": "field_638f20b774701"
},
{
"key": "field_638f20da74703",
"label": "Background",
"name": "background",
"aria-label": "",
"type": "color_picker",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"enable_opacity": 0,
"return_format": "string",
"parent_repeater": "field_638f20b774701"
}
]
}
],
"min": "",
"max": ""
}
},
"min": "",
"max": "",
"button_label": "Add component"
}
For “Get In Touch” gield group it works but for “Icon Group” i’m not sure how to exactly achieve that.
I’m having a similar issue to this ticket. I’m using ACF and Timber with a renderCallback function. This works fine and seems to load the template on the front end but doesn’t seem to work in the editor. I get ‘Your site doesn’t include support for the “acf/[blockname]” block. You can leave this block intact or remove it entirely.’ errors in the editor even though the blocks load on the front-end. It even pulls in the field data that existed before I switched to block.json on the front-end only.
/button/block.json
{
"name": "acf/button",
"title": "Button",
"description": "A button block.",
"category": "dream",
"icon": "button",
"keywords": ["button", "buttons", "button group"],
"attributes": {
"blockId": {
"type": "string"
}
},
"usesContext": [ "buttonGroup/blockId" ],
"supports": {
"jsx": true,
"align": true,
"alignContent": true,
"alignText": true,
"anchor": true,
"className": true,
"multiple": true,
"html": false,
"inserter": true,
"color": {
"text": false,
"background": true,
"link": false,
"gradients": true,
"linkColor": true
},
"fontSize": true,
"fullHeight": false
},
"styles": [
{ "name": "default", "label": "Default", "isDefault": true },
{ "name": "other", "label": "Other" }
],
"editorScript": "file:./index.js",
"script": "file:./script.js",
"editorStyle": "file:./index.css",
"style": "file:./style.css",
"acf": {
"mode": "preview",
"renderCallback": "dream_block_render",
"postTypes": true,
"parent": "acf/button-group",
"reusable": true
}
}
I also get a js error in the console when editing pages whether or not blocks exist on the page:
acf-pro-blocks.min.js?ver=6.0.3:formatted:783 Uncaught TypeError: Cannot read properties of undefined (reading 'length')
at _ (acf-pro-blocks.min.js:783:51)
at Array.map (<anonymous>)
at acf-pro-blocks.min.js?ver=6.0.3:1419:24
at o (acf.min.js?ver=6.0.3:1:1417)
at Object.doAction (acf.min.js?ver=6.0.3:1:587)
at n.doAction (acf.min.js?ver=6.0.3:1:18745)
at post.php?post=1521&action=edit:3223:5
Looks like it may be related to the “attributes” key so maybe I’m just doing something wrong but I’m unsure.
Line 783 of acf-pro-blocks.min.js
for (const t in e.attributes)
0 === e.attributes[t].default.length && delete e.attributes[t].default;
e.supports.anchor && (e.attributes.anchor = {
type: "string"
});
## Issue
When duplicating an ACF block (registered with acf_register_block_type
) data between the blocks can switch place and/or regress. Repeater fields and expander toggles stop working. No javascript error is shown in console.
## Environment
PHP 7.4.21
WP 5.8.1
ACF Pro 5.10.2
## Reproduce
1. Create ACF Field Group
acf_add_local_field_group(array(
'key' => 'group_614c3a15115de',
'title' => 'Test Block',
'fields' => array(
array(
'key' => 'field_614c3c7d9b75e',
'label' => 'Test Field',
'name' => 'test_field',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array(
array(
array(
'param' => 'block',
'operator' => '==',
'value' => 'acf/test-block',
),
),
),
'menu_order' => 0,
'position' => 'acf_after_title',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'is_acf_component' => 0,
'acf_component_defaults' => array(
'repeatable' => '0',
'min' => '',
'max' => '',
'layout' => 'block',
'button_label' => '',
'appearances' => '',
),
));
2. Register ACF Block
acf_register_block_type(array(
'name' => 'test_block',
'title' => __('Test Block'),
'description' => __('Test Block'),
'render_template' => 'template-parts/content_builder-test.php',
'category' => 'formatting',
));
3. Create template file in render_template
location with the contents: <?php the_field('test_field');
3. Navigate to Widgets wp-admin/widgets.php
4. Add new ‘Test Block’
5. Duplicate ‘Test Block’ a few times.
6. Edit the contents of the fields, toggle between Edit/Preview.
## Expected Result
All fields and admin elements should work as normal.
## Actual Result
The preview does not align with the content, when switching back to edit, the value in the field may have changed too. The ‘Update’ button often is not triggered to an active state. When managing to save, reloading shows the widgets in a different state to when you left it.
## Extra
Copying the contents into a reusable block, editing then copy/pasting back in does not work. (Reusable blocks are not supported in the Widgets interface just yet.)
## Work around
Create all blocks from scratch within the widgets interface.
Thank you.
Issue
When duplicating an ACF block (registered with acf_register_block_type
) data between the blocks can switch place and/or regress. Repeater fields and expander toggles stop working. No javascript error is shown in console.
Environment
PHP 7.4.21
WP 5.8.1
ACF Pro 5.10.2
Reproduce
1. Create ACF Field Group
acf_add_local_field_group(array(
'key' => 'group_614c3a15115de',
'title' => 'Test Block',
'fields' => array(
array(
'key' => 'field_614c3c7d9b75e',
'label' => 'Test Field',
'name' => 'test_field',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array(
array(
array(
'param' => 'block',
'operator' => '==',
'value' => 'acf/test-block',
),
),
),
'menu_order' => 0,
'position' => 'acf_after_title',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'is_acf_component' => 0,
'acf_component_defaults' => array(
'repeatable' => '0',
'min' => '',
'max' => '',
'layout' => 'block',
'button_label' => '',
'appearances' => '',
),
));
2. Register ACF Block
acf_register_block_type(array(
'name' => 'test_block',
'title' => __('Test Block'),
'description' => __('Test Block'),
'render_template' => 'template-parts/content_builder-test.php',
'category' => 'formatting',
));
3. Create template file in render_template
location with the contents: <?php the_field('test_field');
3. Navigate to Widgets wp-admin/widgets.php
4. Add new ‘Test Block’
5. Duplicate ‘Test Block’ a few times.
6. Edit the contents of the fields, toggle between Edit/Preview.
Expected Result
All fields and admin elements should work as normal.
Actual Result
The preview does not align with the content, when switching back to edit, the value in the field may have changed too. The ‘Update’ button often is not triggered to an active state. When managing to save, reloading shows the widgets in a different state to when you left it.
Extra
Copying the contents into a reusable block, editing then copy/pasting back in does not work. (Reusable blocks are not supported in the Widgets interface just yet.)
Work around
Create all blocks from scratch within the widgets interface.
Thank you.
Let’s say I have a field group called “Call To Action” with the name “cta”. This group contains a Text field, a URL field, and a checkbox called “Open in new window”.
I also have a Home Page field group that needs this “Call To Action” group cloned into the Hero tab, the Content Teaser tab, and the Promo tab. If I choose “Seamless” as the cloning method, I will have 3 field groups labeled “Call To Action”, which is great. But these groups will all be named “cta” and have the exact same field key, which obviously will cause problems. If I choose “Group” as the cloning method, it will nest my “Call To Action” group into another group, which I do not want.
Cloning could be a very useful feature for creating reusable groups (that act like modules), but this limitation of not being able to specify a custom name when using “Seamless” makes it very limited. If I want all of my “Call To Action” fields inside a group with a unique name and key, I need to use the “Group” cloning mode and select each individual field to clone into it. Later, if I decide to add another field into the master “Call To Action” group, I would need to go to every clone instance and add that field into it. That really takes away a major advantage of creating reusable elements (being able to edit all instances in a single place).
If we could input a custom field name when using the “Seamless” cloning mode, that would make these fields so much more useful as a templating tool. Or maybe there’s a way to do this that I’m missing?
I have created a ACF block called “services” that contains a repeater field and a few other sub fields to display my customer’s services.
When I choose to “add to reusable blocks” I get a little bit of a strange behavior.
Reusable blocks add a wrapper around the original block. So your original block is nested inside of this new container
My original block is visible with the content that I originally added. However when I deselect my block and attempt to save the reusable block, I see that the reusable block has a second version of my repeater fields, empty and ready to fill in.
In case that is unclear, I’ll describe it in this way: two versions of the ACF field group are being added. One at the level of reusable block, and one at the level of the custom block.
To replicate:
I have a field group that I include in other field groups as a Clone field. For example, one of my field groups is called “Page Banner”. In every field group I want a page banner, I include the “Page Banner” field group as a Clone field.
My question is, in the “Page Banner” field group’s Rules, what can I set the Rules to so that it doesn’t appear on any page? (Unless I add it as a Clone field of course.) The default rule is “Show this field group if <Post Type> <is equal to> <Post>”. But I don’t want this group appearing on every “post” post type. So what can I set the rules to so that it doesn’t appear anywhere, unless I “embed” it as a Clone field?
Hello everyone !
New here, I was pretty sure this was not a bug, so I posted in “Feature Requests”, but just tell me if there is a better Forum Category for this post !
So I’m trying new acf guidelines with my team, and part of it is to have a “Common Fields” fields group, wich contains group type fields (hard to follow ? I know, sorry), one for each of our many reusable design components around the websites. After that we use clone everywhere we need these components.
Anyway, enough spaghetti context, this group field called here “Common Fields”, is not meant to be used as it is in any way, so I wish I could assign it to nothing, but I can’t. (or didn’t find the way)
Thanks for reading,
hugo
Hi, hello everybody, i have an issue and i can´t get how to solve. The thing is this:
– All fields and groups are created using php (no admin, no json)
– Inside a no active group i put all my reusable fields that will be used on other parts with the field “clone”
– I have also a flexible field in a group that uses those cloned fieds.
Ok, so the key names of those fields became longer and complicated, let´s say something like this:
“acf-key__flexible_content_rows_key__layout_flexible_row__content_key__layout_html_row__content_key__r_builder_classes_group-key__classes__content_row__container”
Where:
key__flexible_content_rows is the flexible field
key__layout_flexible_row__content is the layout inside that flexible
key__layout_html_row__content the field inside layout (this is type clone)
key__r_builder_classes_group A field type “group” (this is the cloned)
key__classes__content_row__container and last, this is the field itself (this is inside the cloned)
As you can see i have:
flexible->layout->field (clone)->field (group)->field (text)
And i´m tring to set defaults for that last text field… and i can´t 🙁
I tried with:
add_filter(‘acf/load_field/key=KEY’, ‘my_acf_load_field’);
But as you can imagine, the KEY it´s imposible, i tried many things and nothing, i cant set default using Key and that filter.
Is there any thing i´m missing for this cloned defaults?
I don´t want to set defaults directly on the cloned fields i want to set diferent defaults depending on where i´m cloning that field.
I´m clear or is too confused?
Thanks in advance! Sorry my english 🙁
Hi there,
i’m working a lot with the flexible Content field on different projects and created reusable “Modules” which i use in this projects.
However, i often don’t need some Modules of my Library for the current project, so an option to deactivate a Flexible Module/Layout would be nice. So the Module is still there, to be activated when needed, but doesn’t clutter the CMS for the customer. You can deactivate the whole Flexible-Field-Group, but thats not whats needed here.
Another great improvement would be Ajax-Saving the Flexible Fields on creation. Everytime we save, the Page is refreshed now, which closes all Flexibles Fields. When i have to change something in a Layout-Field, i first have to find my way through the fields to reach it, which gets tedious after a while.
best regards,
Alex
Really not sure what’s going on here. I am trying to to an import from my local install to live site and am running into very strange problems with some flexible content fields. I have a number of layouts, most of which contain some repeaters. None of the sub fields for the repeaters are importing, and only the first 7 our of 11 layouts are importing.
Now I already had this field group set up on the live site with 7 layouts setup. But after doing some reworking locally, I deleted the old field group and tried to import. The strange thing is that the 7 out of 11 layouts that are actually importing are the same layouts that were in the old field group. None of the new layouts are importing, and again none of the sub fields on any repeaters are being imported.
I am also using the following extension, but I’m not sure if this is related to the problem: https://github.com/Hube2/acf-reusable-field-group-field
Attached is the json for the field group I am having trouble with. Has anyone experienced something like this before?
Looking to create a simple field type that is just a grouping of multiple sub fields — similar to the “Repeater field”, except that it doesn’t need to repeat.
The reason I want this is for general organization. It’ll give me more flexibility with the layouts that I’m trying to create.
I started to build this with the “Repeater field” sourcecode, and had everything was displaying as I wanted it, but the saving was not working. I suspect that using the repeater field is probably overkill, so maybe I’m just over-complicating this.
I should also note that I was able to accomplish the same layout I am looking for by using the “Reusable field” addon (https://github.com/tybruffy/ACF-Reusable-Field-Group), however, since the fields aren’t actually contained in the same field group, there is no way to use them in conjunction with conditional logic, and this plugin breaks when trying to use it inside of a Repeater field.
Any ideas on how I can accomplish this would be great!
The problem:
I have a content box module where I can add one or several content boxes. But I want these nested in various layouts. Such as a fullwidth layout or a fullheight layout for example.
But whenever I create a new layout type in Flexible Content I have to set up the Content Box module (field group) again although they share the same settings and characteristics.
The question:
Is it possible to create a module (field-group) that is reusable and edited separately so whenever I change any settings for the module I just have to change it i one place?
Hi,
Is it possible not to display a label in ACF? On a field by field basis.
I’m using the reusable field group plugin (https://github.com/tybruffy/ACF-Reusable-Field-Group) and it’s duplicating labels.
Thanks,
Andy.
I’m interested in the possibility of defining a set of custom fields in php and then adding them to various different groups as needed. For example, I need an address field in a lot of different groups and it has lots of sub-fields and layouts. This would also help maintain consistency of formatting and naming when using those fields on the frontend.
The method for generating theme fields I have seen so far in the forums is based on exporting to php, then including that code.
However, that produces (depending on the complexity) a somewhat massive multi-dimensional array that is very hard to read or edit.
The only problem with my idea is that I don’t know anything about the unique field keys that are generated. I couldn’t find the function that generates them, and I don’t know if the unique character string has any relationship to the rest of the field definition (is it a hash of some of the array elements, for example?)
Can anyone tell me if they’ve done this, and if so how and what issues should I be aware of? Or what is the best method you can suggest for defining reusable fields?
I think it’d be awesome to register a field group, but to be able to use that group within another group elsewhere.
Example:
I’ve got various similar field groups for various post types, it’s not appropriate to have one field group to cover them all, however there is a small sub-set of reused fields.
Rather than having to duplicate this sub-set of fields it’d be awesome if they could be register as a group of their own, but then called in.
That way they’re registered just once, so updating them in the future is easy.
I currently achieve this by registering the fields in code… but I dislike this method vs the ACF field builder.
Say I have a select field with a list of colors to choose from. I’d like to be able to reuse the list of colors in multiple custom field groups. The point being it’d be managed by one instance.
As far as I can tell, the only way to do this now is to just create a duplicate select field (which is a pain because I have to manually create them all). And then if they ever change, I need to go edit them all. Ideally, I could create once, reuse wherever in different custom field groups, edit from one instance.
This doesn’t seem to warrant creating a custom field type, but it’s somewhat related. Perhaps a field type of custom field group. Then it would let me select from the saved field groups and just bring in an instance of that group. Is this a possibility?