Skip to content

Connect block attributes with custom fields via UI #176

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

Merged
merged 37 commits into from
Jul 14, 2025

Conversation

cbravobernal
Copy link
Contributor

@cbravobernal cbravobernal commented Jun 12, 2025

What

⚠️ It needs to be loaded as a beta feature in their respective admin panel.
⚠️ Pattern overrides cannot be set up connected to fields. They are still not compatible on Core.
⚠️ There is some duplication both in code and UI with Core.

The PR allows to connect block attributes with custom fields via UI. Is still experimental, as I have not yet tested with any field type that can be created. In a future PR we can add an advanced view, using DataViews.

The image block will only search for image type fields, and all their attributes can be bound at once, just selecting the image field.

The paragraph, heading, and button blocks will only work with an initial set of items set in this variable:

const BLOCK_BINDINGS_RELATED_FIELD_TYPES = {
	'core/paragraph': {
		content: [ 'text', 'textarea', 'date_picker', 'number', 'range' ],
	},
	'core/heading': {
		content: [ 'text', 'textarea', 'date_picker', 'number', 'range' ],
	},
	'core/image': {
		id: [ 'image' ],
		url: [ 'image' ],
		title: [ 'image' ],
		alt: [ 'image' ],
	},
	'core/button': {
		url: [ 'url' ],
		text: [ 'text', 'checkbox', 'select', 'date_picker' ],
		linkTarget: [ 'text', 'checkbox', 'select' ],
		rel: [ 'text', 'checkbox', 'select' ],
	},
};

Screenshare

Screen.Recording.2025-07-08.at.18.04.35.mov

@cbravobernal cbravobernal self-assigned this Jun 12, 2025
@cbravobernal cbravobernal added the [Type] Enhancement New feature or request label Jun 12, 2025
@cbravobernal cbravobernal requested a review from ockham June 24, 2025 09:56
@cbravobernal cbravobernal force-pushed the update/connect-bindings-with-ui branch from 917b150 to 8d471bc Compare June 24, 2025 13:03
@cbravobernal cbravobernal added this to the 6.6.0 milestone Jun 27, 2025
@cbravobernal cbravobernal requested a review from priethor June 27, 2025 11:31
@cbravobernal cbravobernal marked this pull request as ready for review June 27, 2025 11:31
@cbravobernal cbravobernal requested a review from gziolo June 27, 2025 14:32
@ockham
Copy link
Contributor

ockham commented Jul 4, 2025

I think there's a minor UX glitch with regard to the tools panel and link/unlink button:

connect-fields

AFAIU, we should change two things here:

  • The panel's heading shouldn't change based on the link/unlink button; it shouldn't describe the link/unlink option, but the nature of the panel's controls (so probably "Attributes").
  • The panel's "vertical three dots" menu shouldn't change contextually; it should only apply to the whole Attributes panel, not its individual rows.

(This is based on how I observed the UX pattern to work e.g. for the existing margin panel in the style tab of the block inspector.)

@cbravobernal
Copy link
Contributor Author

  • The panel's "vertical three dots" menu shouldn't change contextually; it should only apply to the whole Attributes panel, not its individual rows.

But, in that case, if you have the four of them unlinked, and want to reset only the image title or alt, you will need to reset every of them at once and set one by one again, right?

It's better to keep consistency, but as we change the number of toolsitem, that is the default behaviour. I'll take a look at how is implemented for margin and styles.

Thanks @ockham!

@cbravobernal cbravobernal force-pushed the update/connect-bindings-with-ui branch from 63e9e91 to 7fa68ab Compare July 7, 2025 15:09
@cbravobernal
Copy link
Contributor Author

cbravobernal commented Jul 9, 2025

If I create a new CPT and corresponding field group, but forget to enable the "Allow Access to Value in Editor UI" toggle in a field's "Presentation" tab, I'm still able to connect blocks to that field in the editor. However, it doesn't show up on the frontend:

......
It works on the frontend once I enable those toggles.

I guess the solution is to disallow connecting to a field in the editor if its "Allow Access to Value in Editor UI" toggle is disabled?

Fixed in 89d69ff
It will only show fields with the toggle enabled.

@ockham
Copy link
Contributor

ockham commented Jul 9, 2025

BTW the date_picker field currently shows a date in the editor (when bound to a paragraph) that's different from what's set in SCF 🙈

image

My hunch is that it has to do with timezones -- one of them probably uses UTC, and the other one the local timezone 😬

@cbravobernal
Copy link
Contributor Author

BTW the date_picker field currently shows a date in the editor (when bound to a paragraph) that's different from what's set in SCF 🙈

image My hunch is that it has to do with timezones -- one of them probably uses UTC, and the other one the local timezone 😬

Ouch.

BTW the date_picker field currently shows a date in the editor (when bound to a paragraph) that's different from what's set in SCF 🙈

image My hunch is that it has to do with timezones -- one of them probably uses UTC, and the other one the local timezone 😬

Should we just take the formatted value from the server and print it as a string?

@ockham
Copy link
Contributor

ockham commented Jul 9, 2025

Should we just take the formatted value from the server and print it as a string?

Maybe 🤔

Plus I guess I need to file a bug report against Gutenberg, since it seems that dateI18n (JS) behaves differently from date_i18n (PHP).

@cbravobernal
Copy link
Contributor Author

Should we just take the formatted value from the server and print it as a string?

Maybe 🤔

Plus I guess I need to file a bug report against Gutenberg, since it seems that dateI18n (JS) behaves differently from date_i18n (PHP).

I have a prototype working, but is adding all values parsed by SCF to the types endpoint, ending with maybe too much information.

I will instead try to extend acf object data to include the formatted value.

@ockham
Copy link
Contributor

ockham commented Jul 10, 2025

Per discussion via DM, I've pushed a fix for the date issue: 718af07

@cbravobernal cbravobernal changed the title Experiment: Connect block attributes with custom fields via UI Connect block attributes with custom fields via UI Jul 11, 2025
@cbravobernal
Copy link
Contributor Author

Removing selected connection doesn't work properly:

.....
The editor remains in this weird state where it looks like the field is still connected with SCF field.

This one has been fixed.

@cbravobernal
Copy link
Contributor Author

For some reason, the panel moves in the sidebar after selecting the value:
Screenshot 2025-07-09 at 13 46 27 Screenshot 2025-07-09 at 13 45 56

That happened too in Gutenberg with the tab. It maybe a hook-related thing. I'll take a look at it.

Fixed in 4a0aeaf

@cbravobernal
Copy link
Contributor Author

Let's merge it and iterate it if needed.

@cbravobernal cbravobernal merged commit 2475c85 into trunk Jul 14, 2025
5 checks passed
@cbravobernal cbravobernal deleted the update/connect-bindings-with-ui branch July 14, 2025 12:37
@cbravobernal cbravobernal modified the milestones: 6.6.0, 6.5.2 Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants