Caption data lost if field not on the form or updated programmatically

Created on 1 May 2025, 4 months ago

Problem/Motivation

I ran a batch script recently to update several nodes with data for a new field. As the nodes were updated programmatically the caption data was deleted.

Steps to reproduce

I have two different scenarios to reproduce this issue.

The easiest one:

  1. Create a node with an image field and captions.
  2. Save some caption data.
  3. On the "Manage form display" tab move the field with captions to "Disabled".
  4. Edit and save one of the nodes that already has caption data and the data will be deleted.

I have also been able to reproduce the issue by updating a single node field through code. Load a node that already has caption data, update a different text field (for example) and save. The caption data is deleted. For testing we might not have to edit a field, just load the node and save.

$node->set('field_test', 'Test');
$node->save();

Proposed resolution

Preserve the caption data that is already there in image_field_caption_entity_update().

        // Preserve caption values if the node is updated programmatically or
        // with a form that does not include the field.
        if (empty($value['image_field_caption']['value']) && !empty($value['caption'])) {
          $value['image_field_caption']['value'] = $value['caption'];
          $value['image_field_caption']['format'] = $value['caption_format'];
        }
πŸ› Bug report
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States nathaniel

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024