Skip to content

Declare files fields as attachment IDs in wp bs wpml config - #134

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/wpml-config-attachment-ids
Oct 19, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/wpml-config-attachment-ids

Conversation

@Contributolo

@Contributolo Contributolo commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

wp bs wpml config (7.7.4) treats files as a value type, so WPML copies the stored attachment ID into the translation unchanged and every language shows the original attachment, including its alt text.

WPML documents a declaration for IDs inside block attributes: a key with type="post-ids" and a sub-type, here attachment (section "Automatically Converting IDs in Blocks" in Make Custom Gutenberg Blocks Translatable). WPML then replaces each ID with the attachment of the page language on render_block_data, and keeps the original ID when there is no translated attachment. With WPML Media Translation, a translated image has its own alt text.

What Blockstudio stores

  • A files field stores the attachment ID ("image":173), with multiple a list ("gallery":[64,99]). The editor writes IDs only (files.tsx onSelect, MediaPlaceholder likewise); returnFormat and returnSize only change what Block::render() hands to the template.
  • A field with multiple stores the ID itself, not a list, when one file is selected (media.length === 1 ? change(media[0].id)). In WPML 5.1.0, BlockAttributes::convertByPath() converts a scalar at the end of a path, and at a * level it converts both the entries of a list and a scalar. So multiple gets a * level, which covers both, and a single field gets a plain key.
  • The size selection is stored under <id>__size and stays undeclared.

Changes

  • files is no longer a value type. A files field becomes <key name="image" type="post-ids" sub-type="attachment" />, with multiple <key name="gallery"><key name="*" type="post-ids" sub-type="attachment" /></key>. Group prefixes and repeater rows work as for text fields. ID keys get no label, because WPML does not offer them for translation.
  • WPML reads ID keys for every block entry, whatever translate says (WPML_Gutenberg_Config_Option::update_from_config(); the example in the WPML docs uses translate="0" too). A block whose only declared fields are files keeps translate="0" and carries the ID keys inside the entry, so WPML still offers nothing from it for translation. It counts as a block without text fields in the CLI summary.
  • Conflict policy as in Generate the WPML language configuration for Blockstudio blocks #122: a files field agrees with another files field on the same storage key (with multiple on either, the key gets the * level). A files field and a field of another type (such as select or text), or a hidden files field, are reported in conflicts and not declared. Hidden files fields are not declared, like every hidden field.
  • Docs: dev/wpml (example, table, translate="0" note, conflict rule) and dev/cli; includes/llm regenerated. No changelog entry.

Output for a sample block

{
  "name": "my-theme/cards",
  "blockstudio": {
    "attributes": [
      { "id": "heading", "type": "text", "label": "Heading" },
      { "id": "gallery", "type": "files", "label": "Gallery", "multiple": true },
      {
        "id": "cards",
        "type": "repeater",
        "label": "Cards",
        "attributes": [
          { "id": "title", "type": "text", "label": "Title" },
          { "id": "image", "type": "files", "label": "Image" }
        ]
      }
    ]
  }
}

Before, gallery and image had no keys. Now:

<key name="heading" label="Heading" />
<key name="gallery">
	<key name="*" type="post-ids" sub-type="attachment" />
</key>
<key name="cards">
	<key name="*">
		<key name="title" label="Cards: Title" />
		<key name="image" type="post-ids" sub-type="attachment" />
	</key>
</key>

A block with only a files field (and value fields) changes from <gutenberg-block type="..." translate="0" /> to a translate="0" entry with the ID keys.

On the test theme (199 blocks), the generated file changes only by adding ID keys (58 keys); 88 blocks with text and 111 without stay the same, and the skipped and conflicts lists are unchanged.

Tests

  • WpmlConfigTest: new cases for single, multiple, group prefix, repeater rows, nested multiple in rows and hidden files; a block with only files fields (translate="0" with keys, valid document); files against files with and without multiple, files against select, text and hidden files, in the block, in extensions and in repeater rows. The attribute builder check now compares text and files paths (multiple from the registered attribute). Three existing expectations changed where they contained files fields.
  • WpmlConfigFilesRenderPathsTest: registers a block with single, multiple and repeater files fields through Build::init(), takes the ID paths from the generated configuration, converts the IDs on render_block_data the way WPML 5.1.0 BlockAttributes::convertByPath() walks a path, and renders through do_blocks(). The template receives the converted IDs for a single field, a list, rows, and a multiple field that stores one ID.

Results (WordPress Playground, PHP 8.3, WordPress 7.1.3, PHPUnit run inside a request, tests/unit/wpml):

  • Before the change: 29 tests, 9 failures, all of them the new or updated files expectations (for example "Failed asserting that 11 is identical to 21" in the render test).
  • After the change: 29 tests, 184 assertions, OK.
  • Also run: tests/unit/extensions (61 OK), tests/unit/field (123 OK), tests/unit/attribute-builder (93 OK), tests/unit/cli (1 OK).

Other checks: composer cs clean, PHPStan level 3 on the changed classes without errors, scripts/check-docs-content.mjs valid. wp bs wpml config run in Playground on the test theme. On a production theme with 25 Blockstudio blocks, the generated ID keys are identical to a hand-maintained wpml-config.xml that has been verified with WPML 5.1.0 and WPML Media Translation.

Files fields store attachment IDs. The generator now declares them with
type="post-ids" and sub-type="attachment", with a "*" level for fields
with multiple, so WPML replaces each ID with the attachment of the page
language on render_block_data. Blocks whose only declared fields are
files keep translate="0" and carry the ID keys, since WPML reads ID keys
regardless of translate. A files field and a field of another type on
one storage key are reported as a conflict.
@dnnsjsk

dnnsjsk commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Merged and released in 7.7.5 (86a311c1). Generated WPML configuration now declares files fields as attachment IDs, including multiple files, nested fields, and blocks with no translatable text. Regenerate your configuration with wp bs wpml config --file=wpml-config.xml.

Regression coverage and full CI passed.

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

Successfully merging this pull request may close these issues.

2 participants